1+ package org .baeldung .hbase ;
2+
3+ import org .apache .hadoop .conf .Configuration ;
4+ import org .apache .hadoop .hbase .HColumnDescriptor ;
5+ import org .apache .hadoop .hbase .HTableDescriptor ;
6+ import org .apache .hadoop .hbase .TableName ;
7+ import org .apache .hadoop .hbase .client .*;
8+ import org .apache .hadoop .hbase .filter .*;
9+ import org .apache .hadoop .hbase .filter .CompareFilter .CompareOp ;
10+ import org .apache .hadoop .hbase .filter .FilterList .Operator ;
11+ import org .apache .hadoop .hbase .util .Bytes ;
12+
13+ import java .io .IOException ;
14+ import java .util .Arrays ;
15+ import java .util .List ;
16+
17+ public class HBaseClientOperations {
18+ private final static byte [] cellData = Bytes .toBytes ("cell_data" );
19+
20+ /**
21+ * Drop tables if this value is set true.
22+ */
23+ static boolean INITIALIZE_AT_FIRST = true ;
24+
25+ /**
26+ * <table>
27+ * <tr>
28+ * <tb>Row1</tb> <tb>Family1:Qualifier1</tb> <tb>Family1:Qualifier2</tb>
29+ * </tr>
30+ * <tr>
31+ * <tb>Row2</tb> <tb>Family1:Qualifier1</tb> <tb>Family2:Qualifier3</tb>
32+ * </tr>
33+ * <tr>
34+ * <tb>Row3</tb> <tb>Family1:Qualifier1</tb> <tb>Family2:Qualifier3</tb>
35+ * </tr>
36+ * </table>
37+ */
38+ private final TableName table1 = TableName .valueOf ("Table1" );
39+ private final String family1 = "Family1" ;
40+ private final String family2 = "Family2" ;
41+
42+ private final byte [] row1 = Bytes .toBytes ("Row1" );
43+ private final byte [] row2 = Bytes .toBytes ("Row2" );
44+ private final byte [] row3 = Bytes .toBytes ("Row3" );
45+ private final byte [] qualifier1 = Bytes .toBytes ("Qualifier1" );
46+ private final byte [] qualifier2 = Bytes .toBytes ("Qualifier2" );
47+ private final byte [] qualifier3 = Bytes .toBytes ("Qualifier3" );
48+
49+ private void createTable (Admin admin ) throws IOException {
50+ HTableDescriptor desc = new HTableDescriptor (table1 );
51+ desc .addFamily (new HColumnDescriptor (family1 ));
52+ desc .addFamily (new HColumnDescriptor (family2 ));
53+ admin .createTable (desc );
54+ }
55+
56+ private void delete (Table table ) throws IOException {
57+ final byte [] rowToBeDeleted = Bytes .toBytes ("RowToBeDeleted" );
58+ System .out .println ("\n *** DELETE ~Insert data and then delete it~ ***" );
59+
60+ System .out .println ("Inserting a data to be deleted later." );
61+ Put put = new Put (rowToBeDeleted );
62+ put .addColumn (family1 .getBytes (), qualifier1 , cellData );
63+ table .put (put );
64+
65+ Get get = new Get (rowToBeDeleted );
66+ Result result = table .get (get );
67+ byte [] value = result .getValue (family1 .getBytes (), qualifier1 );
68+ System .out .println ("Fetch the data: " + Bytes .toString (value ));
69+ assert Arrays .equals (cellData , value );
70+
71+ System .out .println ("Deleting" );
72+ Delete delete = new Delete (rowToBeDeleted );
73+ delete .addColumn (family1 .getBytes (), qualifier1 );
74+ table .delete (delete );
75+
76+ result = table .get (get );
77+ value = result .getValue (family1 .getBytes (), qualifier1 );
78+ System .out .println ("Fetch the data: " + Bytes .toString (value ));
79+ assert Arrays .equals (null , value );
80+
81+ System .out .println ("Done. " );
82+ }
83+
84+ private void deleteTable (Admin admin ) throws IOException {
85+ if (admin .tableExists (table1 )) {
86+ admin .disableTable (table1 );
87+ admin .deleteTable (table1 );
88+ }
89+ }
90+
91+ private void filters (Table table ) throws IOException {
92+ System .out .println ("\n *** FILTERS ~ scanning with filters to fetch a row of which key is larget than \" Row1\" ~ ***" );
93+ Filter filter1 = new PrefixFilter (row1 );
94+ Filter filter2 = new QualifierFilter (CompareOp .GREATER_OR_EQUAL , new BinaryComparator (
95+ qualifier1 ));
96+
97+ List <Filter > filters = Arrays .asList (filter1 , filter2 );
98+
99+ Scan scan = new Scan ();
100+ scan .setFilter (new FilterList (Operator .MUST_PASS_ALL , filters ));
101+
102+ try (ResultScanner scanner = table .getScanner (scan )) {
103+ int i = 0 ;
104+ for (Result result : scanner ) {
105+ System .out .println ("Filter " + scan .getFilter () + " matched row: " + result );
106+ i ++;
107+ }
108+ assert i == 2 : "This filtering sample should return 1 row but was " + i + "." ;
109+ }
110+ System .out .println ("Done. " );
111+ }
112+
113+ private void get (Table table ) throws IOException {
114+ System .out .println ("\n *** GET example ~fetching the data in Family1:Qualifier1~ ***" );
115+
116+ Get g = new Get (row1 );
117+ Result r = table .get (g );
118+ byte [] value = r .getValue (family1 .getBytes (), qualifier1 );
119+
120+ System .out .println ("Fetched value: " + Bytes .toString (value ));
121+ assert Arrays .equals (cellData , value );
122+ System .out .println ("Done. " );
123+ }
124+
125+ private void put (Admin admin , Table table ) throws IOException {
126+ System .out .println ("\n *** PUT example ~inserting \" cell-data\" into Family1:Qualifier1 of Table1 ~ ***" );
127+
128+ // Row1 => Family1:Qualifier1, Family1:Qualifier2
129+ Put p = new Put (row1 );
130+ p .addImmutable (family1 .getBytes (), qualifier1 , cellData );
131+ p .addImmutable (family1 .getBytes (), qualifier2 , cellData );
132+ table .put (p );
133+
134+ // Row2 => Family1:Qualifier1, Family2:Qualifier3
135+ p = new Put (row2 );
136+ p .addImmutable (family1 .getBytes (), qualifier1 , cellData );
137+ p .addImmutable (family2 .getBytes (), qualifier3 , cellData );
138+ table .put (p );
139+
140+ // Row3 => Family1:Qualifier1, Family2:Qualifier3
141+ p = new Put (row3 );
142+ p .addImmutable (family1 .getBytes (), qualifier1 , cellData );
143+ p .addImmutable (family2 .getBytes (), qualifier3 , cellData );
144+ table .put (p );
145+
146+ admin .disableTable (table1 );
147+ try {
148+ HColumnDescriptor desc = new HColumnDescriptor (row1 );
149+ admin .addColumn (table1 , desc );
150+ System .out .println ("Success." );
151+ } catch (Exception e ) {
152+ System .out .println ("Failed." );
153+ System .out .println (e .getMessage ());
154+ } finally {
155+ admin .enableTable (table1 );
156+ }
157+ System .out .println ("Done. " );
158+ }
159+
160+ public void run (Configuration config ) throws IOException {
161+ try (Connection connection = ConnectionFactory .createConnection (config )) {
162+
163+ Admin admin = connection .getAdmin ();
164+ if (INITIALIZE_AT_FIRST ) {
165+ deleteTable (admin );
166+ }
167+
168+ if (!admin .tableExists (table1 )) {
169+ createTable (admin );
170+ }
171+
172+ Table table = connection .getTable (table1 );
173+ put (admin , table );
174+ get (table );
175+ scan (table );
176+ filters (table );
177+ delete (table );
178+ }
179+ }
180+
181+ private void scan (Table table ) throws IOException {
182+ System .out .println ("\n *** SCAN example ~fetching data in Family1:Qualifier1 ~ ***" );
183+
184+ Scan scan = new Scan ();
185+ scan .addColumn (family1 .getBytes (), qualifier1 );
186+
187+ try (ResultScanner scanner = table .getScanner (scan )) {
188+ for (Result result : scanner )
189+ System .out .println ("Found row: " + result );
190+ }
191+ System .out .println ("Done." );
192+ }
193+ }
0 commit comments