1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.omid.examples;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.hadoop.hbase.client.Connection;
22 import org.apache.hadoop.hbase.client.ConnectionFactory;
23 import org.apache.hadoop.hbase.client.Put;
24 import org.apache.hadoop.hbase.util.Bytes;
25 import org.apache.omid.transaction.HBaseTransactionManager;
26 import org.apache.omid.transaction.TTable;
27 import org.apache.omid.transaction.Transaction;
28 import org.apache.omid.transaction.TransactionManager;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class BasicExample {
65
66 private static final Logger LOG = LoggerFactory.getLogger(BasicExample.class);
67
68 public static void main(String[] args) throws Exception {
69
70 LOG.info("Parsing command line arguments");
71 String userTableName = "MY_TX_TABLE";
72 if (args != null && args.length > 0 && StringUtils.isNotEmpty(args[0])) {
73 userTableName = args[0];
74 }
75 byte[] family = Bytes.toBytes("MY_CF");
76 if (args != null && args.length > 1 && StringUtils.isNotEmpty(args[1])) {
77 family = Bytes.toBytes(args[1]);
78 }
79 LOG.info("Table '{}', column family '{}'", userTableName, Bytes.toString(family));
80
81 byte[] exampleRow1 = Bytes.toBytes("EXAMPLE_ROW1");
82 byte[] exampleRow2 = Bytes.toBytes("EXAMPLE_ROW2");
83 byte[] qualifier = Bytes.toBytes("MY_Q");
84 byte[] dataValue1 = Bytes.toBytes("val1");
85 byte[] dataValue2 = Bytes.toBytes("val2");
86
87 LOG.info("Creating access to Omid Transaction Manager & Transactional Table '{}'", userTableName);
88 try (TransactionManager tm = HBaseTransactionManager.newInstance();
89 Connection conn = ConnectionFactory.createConnection();
90 TTable txTable = new TTable(conn, userTableName))
91 {
92 Transaction tx = tm.begin();
93 LOG.info("Transaction {} STARTED", tx);
94
95 Put row1 = new Put(exampleRow1);
96 row1.addColumn(family, qualifier, dataValue1);
97 txTable.put(tx, row1);
98 LOG.info("Transaction {} trying to write a new value in [TABLE:ROW/CF/Q] => {}:{}/{}/{} = {} ",
99 tx, userTableName, Bytes.toString(exampleRow1), Bytes.toString(family),
100 Bytes.toString(qualifier), Bytes.toString(dataValue1));
101
102 Put row2 = new Put(exampleRow2);
103 row2.addColumn(family, qualifier, dataValue2);
104 txTable.put(tx, row2);
105 LOG.info("Transaction {} trying to write a new value in [TABLE:ROW/CF/Q] => {}:{}/{}/{} = {} ",
106 tx, userTableName, Bytes.toString(exampleRow2), Bytes.toString(family),
107 Bytes.toString(qualifier), Bytes.toString(dataValue2));
108
109 tm.commit(tx);
110 LOG.info("Transaction {} COMMITTED", tx);
111 }
112
113 }
114
115 }