We have some examples in the documentation on using TransactionContext and TransactionAwareHTable, but we don't explain how to get a TransactionSystemClient that's needed to interact with the Tephra server.
We need to add something along the lines of the following:
If you are using Google Guice, you can make use of the Guice modules that we provide for dependency injection, which will help you get a fully configured TransactionServiceClient instance:
Injector injector = Guice.createInjector(
new ConfigModule(conf),
new ZKModule(),
new DiscoveryModules().getDistributedModules(),
new TransactionModules().getDistributedModules(),
new TransactionClientModule()
);
ZKClientService zkClient = injector.getInstance(ZKClientService.class);
zkClient.startAndWait();
TransactionServiceClient client = injector.getInstance(TransactionServiceClient.class);
You can do this during your application startup and reuse the same TransactionServiceClient instance across all application threads.
Then, within each application thread, you can use a TransactionContext instance, along with TransactionAwareHTable instances to interact with HBase:
Configuration conf = HBaseConfiguration.create();
HConnection conn = HConnectionManager.createConnection(conf);
TransactionAwareHTable txTable = new TransactionAwareHTable(conn.getTable("mytable"));
TransactionContext txContext = new TransactionContext(client, txTable);
try {
txContext.start();
txTable.put(...); // perform normal operations
...
txContext.finish();
} catch (TransactionFailureException tfe) {
txContext.abort();
}
|