// This is the default kiji instance.
final String uri = "kiji://.env/default";
final KijiURI kijiURI = KijiURI.newBuilder(uri).build();
// Open the kiji specified.
Kiji kiji = Kiji.Factory.open(kijiURI);
// Always surround with a try {} finally{} so the kiji gets released,
// no matter what happens.
try {
// ----- Create a kiji table. -----
// First, we need to create a table layout.
final KijiTableLayout layout =
KijiTableLayout.newLayout(
KijiTableLayout.readTableLayoutDescFromJSON(
new FileInputStream(tableDesc)));
// Create the kiji table.
kiji.createTable(tableName, layout);
// Get a handle to the table.
KijiTable table = kiji.openTable(tableName);
// Get the entity ID, according to this table, of the user we are
// demonstrating with.
EntityId entityId = table.getEntityId(userId);
// ----- Write a row to the table. -----
// Get a TableWriter for our table.
KijiTableWriter tableWriter = table.openTableWriter();
// Surround with a try/finally so the tablewriter gets closed.
try {
System.out.println("Putting user " + username + " into table.");
tableWriter.put(entityId, "info", "name", username);
// Flush the write to the table, since this is a demo and
// we are not concerned about efficiency, we just want to
// show that the cell got written successfully.
tableWriter.flush();
} finally {
tableWriter.close();
}
// ----- Read a row from the table. -----
// Get a TableReader for our table.
KijiTableReader tableReader = table.openTableReader();
// Surround with a try/finally so the tablereader gets closed.
try {
// Build a DataRequest for the row we want.
KijiDataRequest dataRequest = KijiDataRequest.create("info", "name");
KijiRowData result = tableReader.get(entityId, dataRequest);
String name = result.getMostRecentValue("info", "name").toString();
System.out.println("Read username " + name + " from table.");
} finally {
tableReader.close();
}
} finally {
kiji.release();
}
}