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

このサンプル・プログラムは、レコード・ストアへのレコードの書込み方法を示します。

SampleWriter.javaクラスは、レコードを書き込むためのコアおよびクライアント・ユーティリティ・クラスの使用方法の例です。このサンプルJavaプログラムは、1レコードを作成し、それをレコード・ストアに書き込みます。

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

  1. PROPERTY_ID変数は、レコードの特定に使用されるレコード・ストア・インスタンスidPropertyName構成プロパティの設定を使用します。
    public static final String PROPERTY_ID = "Endeca.FileSystem.Path";
  2. Recordクラスを持つサンプル・レコードが作成され、Collectionレコードに追加されます。
    Collection<Record> records = new LinkedList<Record>();
      Record record = new Record();
      record.addPropertyValue(new PropertyValue(PROPERTY_ID, "id1"));
      record.addPropertyValue(new PropertyValue("property.name", "property.value"));
      records.add(record);
  3. RecordStoreLocatorユーティリティ・クラスを使用すると、レコード・ストア・サーバーへの接続が作成されます。
    ServiceAddress address = new ServiceAddress(iasHost, iasPort, contextPath);
      RecordStoreLocator locator = RecordStoreLocator.create(address, "rs1");
      RecordStore recordStore = locator.getService();
  4. tryブロックでは、RecordStore.startTransaction()コア・メソッドによってREAD_WRITEトランザクションが作成され、RecordStoreWriter.createWriter()メソッドを使用してライターが作成されます。このライターの例は、1回の転送につき、最大100レコードを書き込みます。
    try {
      System.out.println("Setting record store configuration ...");
      recordStore.setConfiguration(config);
    
      System.out.println("Starting a new transaction ...");
      tId = recordStore.startTransaction(TransactionType.READ_WRITE);
    
      RecordStoreWriter writer = RecordStoreWriter.createWriter(recordStore, tId, 100);
    ...
  5. ライターは、最初に「すべて削除」レコード、次にサンプル・レコードを書き込み、最後にライターを終了します。レコードは、両方のメソッドを示すために2回(初回はコレクションの一部として、2回目は個別のレコードとして)書き込まれます。
    System.out.println("Writing records ...");
      writer.deleteAll();
      writer.write(records);
      writer.close();
  6. クライアント・プログラムは、RecordStore.commitTransaction()コア・メソッドを使用して、書込みトランザクションをコミットします。
    System.out.println("Committing transaction ...");
      recordStore.commitTransaction(tId);
    			
      System.out.println("DONE");

トランザクションがコミットされると、レコード・ストアに新しいレコード生成が含まれます。

SampleWriter.java

package com.endeca.eidi.recordstore.sample;

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

/**
 * SampleWriter is an example of how to use the Record Store core and client
 * utility classes to write records. It creates records and writes them to the
 * Record Store.
 */
public class SampleWriter {

	// This should match the idPropertyName in your record store configuration.
	public static final String ID_PROPERTY_NAME = "Endeca.Id";

	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_WRITE);

			RecordStoreWriter writer = RecordStoreWriter.createWriter(recordStore, transactionId);

			System.out.println("Writing records ...");

			// Start by deleting all records in the new Record Store generation.
			// This should be done when doing a baseline write to the Record Store.
			// It should not be done when doing an incremental import into the Record
			// Store.
			writer.deleteAll();

			// Write a record to the Record Store
			writer.write(createRecord(
					ID_PROPERTY_NAME, "record1", 
					"fruit", "apple",
					"color", "red"));
			
			// Write another record to the Record Store
			writer.write(createRecord(
					ID_PROPERTY_NAME, "record2", 
					"fruit", "banana",
					"color", "yellow"));
			
			// Close the RecordStoreWriter. This will flush the client
			// side record buffer.
			writer.close();

			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();
				}
			}
		}
	}

	private static Record createRecord(String... nameValuePairs) {
		if (nameValuePairs.length % 2 != 0) {
			throw new IllegalArgumentException("Missing property value for property " + 
					nameValuePairs[nameValuePairs.length-1]);
		}
		Record record = new Record();
		for (int i = 0; i < nameValuePairs.length; i = i + 2) {
			record.addPropertyValue(new PropertyValue(nameValuePairs[i], nameValuePairs[i + 1]));
		}
		return record;
	}
}