Library Version 12.1.3.0

LOB Example
Prev | Chapter 14. Using Large Objects | Next

## LOB Example

The following example writes and then reads a LOB value. Notice that the object is never actually materialized within the application; instead, the value is streamed directly from the file system to the store. On reading from the store, this simple example merely counts the number of bytes retrieved from the store.

Also, this example only catches the bare minimum `IOException`. In a real-world example, you should at least catch and decide what to do with `PartialLOBException`, which can be thrown by `KVStore.getLOB()`. It indicates that you have only retrieved a portion of the requested object. Also, `RequestTimeoutException` can be thrown on the put operation if each chunk is not successfully read or written within the amount of time.

    package kvstore.lobExample;

    import java.io.File;
    import java.io.IOException;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.Arrays;
    import java.util.concurrent.TimeUnit;

    import oracle.kv.Consistency;
    import oracle.kv.Durability;
    import oracle.kv.KVStore;
    import oracle.kv.KVStoreConfig;
    import oracle.kv.KVStoreFactory;
    import oracle.kv.Key;
    import oracle.kv.Version;
    import oracle.kv.lob.InputStreamVersion;

    ...

    // Skipping configuring and opening the store
    // LOB interfaces are accessed directly off the
    // store handle.

    ...

    // Use key "/test/lob/1.lob" to store the jpeg object.
    final Key key = Key.createKey(Arrays.asList("test", "lob", "1.lob"));

    ...

    File lobFile = new File("test.jpg");
    FileInputStream fis = new FileInputStream(lobFile);

    // The image file is streamed from the filesystem into
    // the store without materializing it within the
    // application. A medium level of durability is
    // used for this put operation. A timeout value
    // of 5 seconds is set in which each chunk of the LOB
    // must be written, or the operation fails with a
    // RequestTimeoutException.
    Version version = store.putLOB(key, fis,
            Durability.COMMIT_WRITE_NO_SYNC,
            5, TimeUnit.SECONDS);

    // Now read the LOB. It is streamed from the store, without
    // materialization within the application code. Here, we only
    // count the number of bytes retrieved.
    //
    // We use the least stringent consistency policy available for
    // the read. Each LOB chunk must be read within a 5 second window
    // or a RequestTimeoutException is thrown.
    InputStreamVersion istreamVersion =
         store.getLOB(key,
                      Consistency.NONE_REQUIRED,
                      5, TimeUnit.SECONDS);

     InputStream stream = istreamVersion.getInputStream();
     int byteCount = 0;
     while (stream.read() != -1) {
         byteCount++;
     }
     System.out.println(byteCount);

Prev | Up | Next
LOB APIs | Home | Appendix A. Third Party Licenses