BLOB Tool Library

Library allows to work with BLOBs in streaming mode in different databases using the same API. Supports transparent compression of BLOB data.

License

License

GroupId

GroupId

com.alexkasko.springjdbc
ArtifactId

ArtifactId

blob-tool
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

BLOB Tool Library
Library allows to work with BLOBs in streaming mode in different databases using the same API. Supports transparent compression of BLOB data.
Project URL

Project URL

https://github.com/alexkasko/blob-tool
Source Code Management

Source Code Management

https://github.com/alexkasko/blob-tool

Download blob-tool

How to add to project

<!-- https://jarcasting.com/artifacts/com.alexkasko.springjdbc/blob-tool/ -->
<dependency>
    <groupId>com.alexkasko.springjdbc</groupId>
    <artifactId>blob-tool</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.alexkasko.springjdbc/blob-tool/
implementation 'com.alexkasko.springjdbc:blob-tool:1.0'
// https://jarcasting.com/artifacts/com.alexkasko.springjdbc/blob-tool/
implementation ("com.alexkasko.springjdbc:blob-tool:1.0")
'com.alexkasko.springjdbc:blob-tool:jar:1.0'
<dependency org="com.alexkasko.springjdbc" name="blob-tool" rev="1.0">
  <artifact name="blob-tool" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.alexkasko.springjdbc', module='blob-tool', version='1.0')
)
libraryDependencies += "com.alexkasko.springjdbc" % "blob-tool" % "1.0"
[com.alexkasko.springjdbc/blob-tool "1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework : spring-jdbc jar 3.1.0.RELEASE
cglib : cglib jar 2.2.2
log4j : log4j jar 1.2.16

provided (3)

Group / Artifact Type Version
org.iq80.snappy : snappy jar 0.3
org.tukaani : xz jar 1.0
postgresql : postgresql jar 9.0-801.jdbc4

test (7)

Group / Artifact Type Version
junit : junit jar 4.8.2
org.springframework : spring-test jar 3.1.0.RELEASE
com.h2database : h2 jar 1.3.165
com.oracle » ojdbc6 jar 11.2.0.1.0
net.sourceforge.jtds : jtds jar 1.2.4
javax.inject : javax.inject jar 1
commons-io : commons-io jar 2.4

Project Modules

There are no modules declared in this project.

Tools for working with BLOBs in different databases

Library allows to work with BLOBs in streaming mode (particularly - write to BLOB as to OutputStream) in different databases using the same API (created initially for PostgreSQL). Supports transparent compression of BLOB data.

Depends on spring-jdbc.

Library is available in Maven central:

<dependency>
    <groupId>com.alexkasko.springjdbc</groupId>
    <artifactId>blob-tool</artifactId>
    <version>1.0</version>
</dependency>

Javadocs for the latest release are available here.

Usage example with Spring and PostgreSQL

Blob tool setup:

@Bean
public BlobTool blobTool() {
    return new PostgresBlobTool(dataSource(), compressor());
}

Create BLOB and write data into it:

OutputStreamBlob blob = null;
try {
    // create server-side BLOB
    blob = blobTool.create();
    // use OutputSteam from BLOB
    createHugeReport(blob.outputStream());
    // save BLOB id in application table for subsequent use
    save(new AppObj(foo, bar, blob.getId()));
} finally {
    // release DB resources
    closeQuietly(blob);
}

Read data from BLOB:

InputStreamBlob blob = null;
try {
    // open server-side BLOB by ID
    blob = blobTool.load(id);
    // read stored data
    processStoredData(blob.inputStream());
} finally {
    // release DB resources
    closeQuietly(blob);
}

Working with BLOBs through their IDs

Library was initially created for PostgreSQL and uses it's approach when long BLOB ID (OID in postgres) is stored in application table and blob data should be accessed using this ID explicitly. For other RDBMSes that doesn't allow direct control over BLOB ID (when BLOB itself is "stored" in application table) library uses additional tables like this:

create table blob_storage (
    id int primary key,
    data blob
);

Streaming BLOBs in JDBC

This library uses BLOBs exclusively in streaming mode without loading them into memory on any operation.

Most RDBMSes support streaming read of BLOB data through JDBC API. But proper streaming write to BLOBs is less supported. To write into server-side BLOBs in streaming mode using OutputStream you may use this JDBC method Blob#setBinaryStream.

Besides its usage is not intuitive (set method returns OutputStream):

Blob b = ...
OutputStream blobStream = b.setBinaryStream(1);

it's also (among popular RDBMSes) implemented properly (with real server-side BLOB) only in PostgreSQL and in Oracle.

This library contains different implementations for RDBMSes that support server side streaming write and for those that does not. Fallback implementation (for MSSQL, MySQL etc.) uses temporary file for writing to BLOB as to OutputStream and then flushes data to server. This method may not suit highload applications, but may be useful e.g. for using H2 database instead of PostgreSQL or Oracle in testing/prototyping.

BLOBs compression

Library supports transparent compression of BLOB data. Supported compression methods:"

  • GzipCompressor: GZIP compression using JDK GZIP implementation
  • SnappyCompressor: Snappy compression
  • XzCompressor: LZMA2 compression
  • NoCompressor: compression is disabled

These libraries are required for Snappy and XZ compression:

<dependency>
    <groupId>org.iq80.snappy</groupId>
    <artifactId>snappy</artifactId>
    <version>0.3</version>
</dependency>
<dependency>
    <groupId>org.tukaani</groupId>
    <artifactId>xz</artifactId>
    <version>1.0</version>
</dependency>

License information

This project is released under the Apache License 2.0

Changelog

1.0 (2013-03-15)

  • initial public version

Versions

Version
1.0