View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.omid.examples;
19  
20  import org.apache.omid.transaction.HBaseTransactionManager;
21  import org.apache.omid.transaction.TTable;
22  import org.apache.omid.transaction.Transaction;
23  import org.apache.omid.transaction.TransactionManager;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.hadoop.hbase.client.Put;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * ****************************************************************************************************
32   *
33   * Example code which demonstrates an atomic write into two different rows in HBase
34   *
35   * ****************************************************************************************************
36   *
37   * After building the package with 'mvn clean package' find the resulting examples-<version>-bin.tar.gz file in the
38   * 'examples/target' folder. Copy it to the target host and expand with 'tar -zxvf examples-<version>-bin.tar.gz'.
39   *
40   * Make sure that 'hbase-site.xml' and 'core-site.xml' are either in classpath (see run.sh) or explicitly referenced in
41   * configuration file. If a secure HBase deployment is needed, make sure to specify the principal (user) and keytab file.
42   *
43   * The example requires a user table to perform transactional read/write operations. A table is already specified in
44   * the default configuration, and can be created with the following command using the 'hbase shell':
45   *
46   * <pre>
47   * create 'MY_TX_TABLE', {NAME => 'MY_CF', VERSIONS => '2147483647', TTL => '2147483647'}
48   * </pre>
49   *
50   * Make sure that the principal/user has RW permissions for the given table using also the 'hbase shell':
51   * <pre>
52   * grant '<principal/user>', 'RW', 'MY_TX_TABLE'
53   * </pre>
54   *
55   * Alternatively, a table with a column family already created can be used by specifying the table name and column
56   * family identifiers using the command line arguments (see details also in 'run.sh') If a table namespace is required,
57   * specify it like this: 'namespace:table_name'
58   *
59   * Finally, run the example using the 'run.sh' script without arguments or specifying the necessary configuration
60   * parameters.
61   */
62  public class BasicExample {
63  
64      private static final Logger LOG = LoggerFactory.getLogger(BasicExample.class);
65  
66      public static void main(String[] args) throws Exception {
67  
68          LOG.info("Parsing command line arguments");
69          String userTableName = "MY_TX_TABLE";
70          if (args != null && args.length > 0 && StringUtils.isNotEmpty(args[0])) {
71              userTableName = args[0];
72          }
73          byte[] family = Bytes.toBytes("MY_CF");
74          if (args != null && args.length > 1 && StringUtils.isNotEmpty(args[1])) {
75              family = Bytes.toBytes(args[1]);
76          }
77          LOG.info("Table '{}', column family '{}'", userTableName, Bytes.toString(family));
78  
79          byte[] exampleRow1 = Bytes.toBytes("EXAMPLE_ROW1");
80          byte[] exampleRow2 = Bytes.toBytes("EXAMPLE_ROW2");
81          byte[] qualifier = Bytes.toBytes("MY_Q");
82          byte[] dataValue1 = Bytes.toBytes("val1");
83          byte[] dataValue2 = Bytes.toBytes("val2");
84  
85          LOG.info("Creating access to Omid Transaction Manager & Transactional Table '{}'", userTableName);
86          try (TransactionManager tm = HBaseTransactionManager.newInstance();
87               TTable txTable = new TTable(userTableName))
88          {
89              Transaction tx = tm.begin();
90              LOG.info("Transaction {} STARTED", tx);
91  
92              Put row1 = new Put(exampleRow1);
93              row1.add(family, qualifier, dataValue1);
94              txTable.put(tx, row1);
95              LOG.info("Transaction {} trying to write a new value in [TABLE:ROW/CF/Q] => {}:{}/{}/{} = {} ",
96                       tx, userTableName, Bytes.toString(exampleRow1), Bytes.toString(family),
97                       Bytes.toString(qualifier), Bytes.toString(dataValue1));
98  
99              Put row2 = new Put(exampleRow2);
100             row2.add(family, qualifier, dataValue2);
101             txTable.put(tx, row2);
102             LOG.info("Transaction {} trying to write a new value in [TABLE:ROW/CF/Q] => {}:{}/{}/{} = {} ",
103                      tx, userTableName, Bytes.toString(exampleRow2), Bytes.toString(family),
104                      Bytes.toString(qualifier), Bytes.toString(dataValue2));
105 
106             tm.commit(tx);
107             LOG.info("Transaction {} COMMITTED", tx);
108         }
109 
110     }
111 
112 }