次の例では、LOB値を書き込んだ後に読み取ります。オブジェクトがアプリケーション内で実際にマテリアライズ化されることはありませんが、かわりに、値がファイルシステムからストアに直接ストリームされることに注意してください。この簡単な例では、ストアからの読取り時に、ストアから取得したバイト数を単にカウントします。
また、この例では最低限のIOException
のみをキャッチします。実際の運用では、少なくともKVStore.getLOB()
がスローするPartialLOBException
をキャッチして、その処理を決定しておく必要があります。これは、リクエストしたオブジェクトの一部のみが取得されたことを示します。また、各チャンクの読取りまたは書込みが時間内に正しく行われないと、格納操作でRequestTimeoutException
がスローされる可能性があります。
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 ... // 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);