Sample Writer client example

This sample program shows how to write records to the Record Store.

The SampleWriter.java class is an example of how to use the core and client utility classes to write records. The sample Java program creates one record and writes it to the Record Store.

The code works as follows:

  1. The PROPERTY_ID variable uses the setting of the Record Store instance idPropertyName configuration property, which is used to identify the records.
    public static final String PROPERTY_ID = "Endeca.FileSystem.Path";
  2. A sample record is created with the Record class and added to the records 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. Using the RecordStoreLocator utility class, a connection is made to the Record Store Server.
    ServiceAddress address = new ServiceAddress(iasHost, iasPort, contextPath);
      RecordStoreLocator locator = RecordStoreLocator.create(address, "rs1");
      RecordStore recordStore = locator.getService();
  4. In a try block, a READ_WRITE transaction was created by the RecordStore.startTransaction() core method and the RecordStoreWriter.createWriter() method is used to create a writer. This example writer writes a maximum of 100 records per transfer.
    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. The writer first writes a "Delete All" record, then writes the sample record, and finally closes the writer. Note that the record is written twice (the first time as part of a collection and the second as an individual record), in order to demonstrate both methods.
    System.out.println("Writing records ...");
      writer.deleteAll();
      writer.write(records);
      writer.close();
  6. The client program uses the RecordStore.commitTransaction() core method to commit the write transaction.
    System.out.println("Committing transaction ...");
      recordStore.commitTransaction(tId);
    			
      System.out.println("DONE");

After the transaction is committed, the Record Store contains a new record generation.

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