リーダー・クライアントの例のサンプル

このサンプル・プログラムは、レコード・ストアからのレコードの読取り方法を示します。

SampleReader.javaクラスは、レコードを読み取るためのコアおよびクライアント・ユーティリティ・クラスの使用方法の例です。このサンプル・プログラムは、最後にコミットされた生成のIDを取得して、そのレコードをレコード・ストアから読み取ります。

コードは次のように機能します。

  1. RecordStoreLocatorユーティリティ・クラスを使用すると、レコード・ストア・サーバーへの接続が作成されます。
    ServiceAddress address = new ServiceAddress(iasHost, iasPort, contextPath); 
      RecordStoreLocator locator = RecordStoreLocator.create(address, "rs1");
      RecordStore recordStore = locator.getService();
  2. tryブロックでは、RecordStore.startTransaction()コア・メソッドは、READトランザクションを作成し、次にRecordStore.getLastCommittedGenerationId()コア・メソッドはレコード・ストアにコミットされた最後の生成のIDを取得します。
    TransactionId tId = null;
      try {
        System.out.println("Starting a new transaction ...");
        tId = recordStore.startTransaction(TransactionType.READ);
    
        System.out.println("Getting the last committed generation ...");
        GenerationId gId = recordStore.getLastCommittedGenerationId(tId);
  3. ベースライン・リーダーを作成するには、RecordStoreReader.createBaselineReader()ユーティリティ・メソッドを使用します。リーダーは、1回の転送につき最大100レコードを転送します。
    System.out.println("Reading records ...");
      RecordStoreReader reader = RecordStoreReader.createBaselineReader(recordStore, tId, gId, 100);
      int count = 0;
  4. whileループでは、hasNext()メソッドは、リーダーに読み取ることができる別のレコードがあるかどうかをテストします。trueの場合、next()メソッドはそのレコードを取得し、レコードは書きだされて、レコードの読取り数が1増加します。読み取るレコードがなくなると、close()メソッドはリーダーを終了し、レコードの数が出力されます。
    while (reader.hasNext()) {
      Record record = reader.next();
      System.out.println("  RECORD: " + record);
      count++;
      }
    reader.close();
    System.out.println(count + " record(s) read");
  5. クライアント・プログラムは、RecordStore.commitTransaction()コア・メソッドを使用して、読取りトランザクションをコミットします。
    System.out.println("Committing transaction ...");
      recordStore.commitTransaction(tId);
    
      System.out.println("DONE");

SampleReader.java

package com.endeca.eidi.recordstore.sample;

import com.endeca.eidi.EidiConstants;
import com.endeca.eidi.record.Record;
import com.endeca.eidi.recordstore.GenerationId;
import com.endeca.eidi.recordstore.RecordStore;
import com.endeca.eidi.recordstore.RecordStoreException;
import com.endeca.eidi.recordstore.RecordStoreLocator;
import com.endeca.eidi.recordstore.RecordStoreReader;
import com.endeca.eidi.recordstore.TransactionId;
import com.endeca.eidi.recordstore.TransactionType;
import com.endeca.eidi.service.ServiceAddress;

/**
 * SampleReader is an example of how to use the Record Store core and client
 * utility classes to read records. It gets the ID of the last-committed
 * generation and reads its records from the Record Store.
 */
public class SampleReader {

	public static void main(String[] args) {
		if (args.length != 2 && args.length != 3) {
			System.out.println("Usage: <ias host> <ias port> [ias context path]");
			System.exit(-1);
		}

		String iasHost = args[0];
		int iasPort = Integer.parseInt(args[1]);
		String contextPath = (args.length == 3) ? args[2] : EidiConstants.DEFAULT_CONTEXT_PATH;

		ServiceAddress address = new ServiceAddress(iasHost, iasPort, contextPath);
		RecordStoreLocator locator = RecordStoreLocator.create(address, "rs1");
		RecordStore recordStore = locator.getService();

		TransactionId transactionId = null;
		try {
			System.out.println("Starting a new transaction ...");
			transactionId = recordStore.startTransaction(TransactionType.READ);

			System.out.println("Getting the last committed generation ...");
			GenerationId gId = recordStore.getLastCommittedGenerationId(transactionId);

			System.out.println("Reading records ...");
			RecordStoreReader reader = RecordStoreReader.createBaselineReader(recordStore, transactionId,
					gId);
			int count = 0;
			while (reader.hasNext()) {
				Record record = reader.next();
				System.out.println("  RECORD: " + record);
				count++;
			}
			reader.close();
			System.out.println(count + " record(s) read");

			System.out.println("Committing transaction ...");
			recordStore.commitTransaction(transactionId);

			System.out.println("DONE");
		} catch (RecordStoreException exception) {
			exception.printStackTrace();
			if (transactionId != null) {
				try {
					recordStore.rollbackTransaction(transactionId);
				} catch (RecordStoreException anotherException) {
					System.out.println("Failed to roll back transaction.");
					anotherException.printStackTrace();
				}
			}
		}
	}
}