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.HBaseOmidClientConfiguration;
21  import org.apache.omid.transaction.HBaseTransactionManager;
22  import org.apache.omid.transaction.RollbackException;
23  import org.apache.omid.transaction.TTable;
24  import org.apache.omid.transaction.Transaction;
25  import org.apache.omid.transaction.TransactionManager;
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.hadoop.hbase.client.Put;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import java.io.IOException;
33  
34  import static org.apache.omid.tso.client.OmidClientConfiguration.ConnType.DIRECT;
35  
36  /**
37   * ****************************************************************************************************************
38   *
39   *  This example code demonstrates different ways to configure the Omid client settings for HBase
40   *
41   * ****************************************************************************************************************
42   *
43   * Please @see{BasicExample} first on how to use with all default settings
44   *
45   */
46  public class ConfigurationExample {
47      private static final Logger LOG = LoggerFactory.getLogger(ConfigurationExample.class);
48  
49      public static void main(String[] args) throws Exception {
50  
51          LOG.info("Parsing command line arguments");
52          String userTableName = "MY_TX_TABLE";
53          if (args != null && args.length > 0 && StringUtils.isNotEmpty(args[0])) {
54              userTableName = args[0];
55          }
56          byte[] family = Bytes.toBytes("MY_CF");
57          if (args != null && args.length > 1 && StringUtils.isNotEmpty(args[1])) {
58              family = Bytes.toBytes(args[1]);
59          }
60          LOG.info("Table '{}', column family '{}'", userTableName, Bytes.toString(family));
61  
62          ConfigurationExample example = new ConfigurationExample();
63  
64          // -----------------------------------------------------------------------------------------------------------
65          // Omid client settings configuration through the 'hbase-omid-client-config.yml' configuration file
66          // -----------------------------------------------------------------------------------------------------------
67          // The HBaseOmidClientConfiguration loads defaults from 'default-hbase-omid-client-config.yml'
68          // and then also applies settings from 'hbase-omid-client-config.yml' if it's available in the classpath.
69          // In the code snippet below, the user settings are loaded from the 'hbase-omid-client-config.yml' file in
70          // the /conf directory that is included in the example classpath (See run.sh.) You can modify the Omid client
71          // settings there or you can place your own 'hbase-omid-client-config.yml' file with all your custom settings
72          // in the application classpath.
73  
74          example.doWork(userTableName, family, new HBaseOmidClientConfiguration());
75  
76          // -----------------------------------------------------------------------------------------------------------
77          // Omid client settings configuration from application code
78          // -----------------------------------------------------------------------------------------------------------
79          // You can also configure Omid programmatically from your code. This is useful for example in unit tests.
80          // The HBaseOmidClientConfiguration still loads defaults from 'default-hbase-omid-client-config.yml' first,
81          // and then applies settings from 'hbase-omid-client-config.yml' if it's available and then use explicit
82          // settings in the code. An example of an explicit Omid client configuration in code is shown below.
83  
84          HBaseOmidClientConfiguration omidClientConfiguration = new HBaseOmidClientConfiguration();
85          omidClientConfiguration.setConnectionType(DIRECT);
86          omidClientConfiguration.setConnectionString("localhost:54758");
87          omidClientConfiguration.setRetryDelayInMs(3000);
88  
89          example.doWork(userTableName, family, omidClientConfiguration);
90      }
91  
92      private void doWork(String userTableName, byte[] family, HBaseOmidClientConfiguration configuration)
93              throws IOException, RollbackException, InterruptedException {
94  
95          byte[] exampleRow1 = Bytes.toBytes("EXAMPLE_ROW1");
96          byte[] exampleRow2 = Bytes.toBytes("EXAMPLE_ROW2");
97          byte[] qualifier = Bytes.toBytes("MY_Q");
98          byte[] dataValue1 = Bytes.toBytes("val1");
99          byte[] dataValue2 = Bytes.toBytes("val2");
100 
101         LOG.info("Creating access to Omid Transaction Manager & Transactional Table '{}'", userTableName);
102         try (TransactionManager tm = HBaseTransactionManager.newInstance(configuration);
103              TTable txTable = new TTable(userTableName))
104         {
105             for (int i = 0; i < 100; i++) {
106                 Transaction tx = tm.begin();
107                 LOG.info("Transaction #{} {} STARTED", i, tx);
108 
109                 Put row1 = new Put(exampleRow1);
110                 row1.add(family, qualifier, dataValue1);
111                 txTable.put(tx, row1);
112                 LOG.info("Transaction {} trying to write a new value in [TABLE:ROW/CF/Q] => {}:{}/{}/{} = {} ",
113                          tx, userTableName, Bytes.toString(exampleRow1), Bytes.toString(family),
114                          Bytes.toString(qualifier), Bytes.toString(dataValue1));
115 
116                 Put row2 = new Put(exampleRow2);
117                 row2.add(family, qualifier, dataValue2);
118                 txTable.put(tx, row2);
119                 LOG.info("Transaction {} trying to write a new value in [TABLE:ROW/CF/Q] => {}:{}/{}/{} = {} ",
120                          tx, userTableName, Bytes.toString(exampleRow2), Bytes.toString(family),
121                          Bytes.toString(qualifier), Bytes.toString(dataValue2));
122 
123                 tm.commit(tx);
124                 LOG.info("Transaction #{} {} COMMITTED", i, tx);
125             }
126         }
127 
128     }
129 
130 }