// Add it to the Hive
product = hive.addResource(product);
//Now create a SecondaryIndex
SecondaryIndex nameIndex = new SecondaryIndex("name", Types.VARCHAR);
//Add it to the Hive
nameIndex = hive.addSecondaryIndex(product, nameIndex);
//Note: SecondaryIndexes are identified by ResourceName.IndexColumnName
//Now lets add a product to the hive.
Product spork = new Product(23, "Spork", "Cutlery");
//First we have to add a primary index entry in order to get allocated to a data node.
//While it is possible to write a record to multiple locations within the Hive, the default implementation
//inserts a single copy.
hive.directory().insertPrimaryIndexKey(spork.getType());
//Next we insert the record into the assigned data node
Collection<SimpleJdbcDaoSupport> sporkDaos = hive.connection().daoSupport().get(spork.getType(), AccessType.ReadWrite);
PreparedStatementCreatorFactory stmtFactory =
new PreparedStatementCreatorFactory(productInsertSql, new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR});
Object[] parameters = new Object[]{spork.getId(), spork.getName(), spork.getType()};
for (JdbcDaoSupport dao : sporkDaos)
dao.getJdbcTemplate().update(stmtFactory.newPreparedStatementCreator(parameters));
//Update the resource id so that the hive can locate it
hive.directory().insertResourceId(resourceName, spork.getId(), spork.getType());
//Finally we update the SecondaryIndex
hive.directory().insertSecondaryIndexKey(resourceName, "name", spork.getName(), spork.getId());
//Retrieve spork by Primary Key
sporkDaos = hive.connection().daoSupport().get(spork.getType(), AccessType.ReadWrite);
parameters = new Object[]{spork.getId()};
//Here I am taking advantage of the fact that I know there is only one copy.
Product productA = (Product) Atom.getFirst(sporkDaos).getJdbcTemplate().queryForObject(selectProductById, parameters, new ProductRowMapper());
//Make sure its a spork
Assert.assertEquals(spork.getName(), productA.getName());
//Retrieve the spork by Name
sporkDaos = (Collection<SimpleJdbcDaoSupport>) hive.connection().daoSupport().get(resourceName, nameIndex.getName(), spork.getName(), AccessType.Read);
parameters = new Object[]{spork.getName()};
Product productB = (Product) Atom.getFirst(sporkDaos).getJdbcTemplate().queryForObject(selectProductByName, parameters, new ProductRowMapper());
//Make sure its a spork
Assert.assertEquals(spork.getId(), productB.getId());