このサンプル・プログラムは、レコード・ストアへのレコードの書込み方法を示します。
SampleWriter.javaクラスは、レコードを書き込むためのコアおよびクライアント・ユーティリティ・クラスの使用方法の例です。このサンプルJavaプログラムは、1レコードを作成し、それをレコード・ストアに書き込みます。
コードは次のように機能します。
トランザクションがコミットされると、レコード・ストアに新しいレコード生成が含まれます。
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; } }