}
/** {@inheritDoc} */
@Override
protected int run(List<String> nonFlagArgs) throws Exception {
final KijiColumnName column = mColumnURI.getColumns().get(0);
final KijiTableLayout layout = mTable.getLayout();
final CellSchema cellSchema = layout.getCellSchema(column);
final EntityId entityId =
ToolUtils.createEntityIdFromUserInputs(mEntityId, layout);
final KijiTableWriter writer = mTable.openTableWriter();
try {
if (cellSchema.getType() == SchemaType.COUNTER) {
if (-1 == mTimestamp) {
try {
long value = Long.parseLong(mJsonValue);
writer.put(entityId, column.getFamily(), column.getQualifier(), value);
} catch (NumberFormatException nfe) {
LOG.error("Could not parse value flag to a long: " + nfe.getMessage());
return FAILURE;
}
} else {
LOG.error("Counters do not support writing to a specific timestamp."
+ " Remove the \"timestamp\" flag.");
return FAILURE;
}
} else {
// Get writer schema.
// Otherwise, set writer Schema in mSchema in preparation to write an Avro record.
if (null != mSchemaFlag) {
try {
LOG.debug("Schema is " + mSchemaFlag);
mSchema = new Schema.Parser().parse(mSchemaFlag);
} catch (AvroRuntimeException are) {
LOG.error("Could not parse writer schema: " + are.toString());
return FAILURE;
}
} else {
try {
mSchema = layout.getSchema(column);
} catch (Exception e) {
LOG.error(e.getMessage());
return FAILURE;
}
}
Preconditions.checkNotNull(mSchema);
// Create the Avro record to write.
GenericDatumReader<Object> reader = new GenericDatumReader<Object>(mSchema);
Object datum = reader.read(null, new DecoderFactory().jsonDecoder(mSchema, mJsonValue));
// Write the put.
if (-1 == mTimestamp) {
writer.put(entityId, column.getFamily(), column.getQualifier(), datum);
} else {
writer.put(entityId, column.getFamily(), column.getQualifier(), mTimestamp, datum);
}
}
} finally {
ResourceUtils.closeOrLog(writer);