Skip to content

Commit 1ff8b4c

Browse files
tomekl007pedja4
authored andcommitted
Bael 655 (eugenp#1256)
BAEL-655 hbase
1 parent 693f7b9 commit 1ff8b4c

5 files changed

Lines changed: 295 additions & 0 deletions

File tree

‎hbase/pom.xml‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>parent-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>hbase</artifactId>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<configuration>
19+
<source>1.8</source>
20+
<target>1.8</target>
21+
</configuration>
22+
</plugin>
23+
</plugins>
24+
</build>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.apache.hbase</groupId>
29+
<artifactId>hbase-client</artifactId>
30+
<version>${hbase.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.hbase</groupId>
34+
<artifactId>hbase</artifactId>
35+
<version>${hbase.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>junit</groupId>
39+
<artifactId>junit</artifactId>
40+
<version>${junit.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<properties>
46+
<hbase.version>1.3.0</hbase.version>
47+
<junit.version>4.12</junit.version>
48+
</properties>
49+
50+
51+
</project>
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.baeldung.hbase;
2+
3+
4+
import com.google.protobuf.ServiceException;
5+
import org.apache.hadoop.conf.Configuration;
6+
import org.apache.hadoop.fs.Path;
7+
import org.apache.hadoop.hbase.HBaseConfiguration;
8+
import org.apache.hadoop.hbase.MasterNotRunningException;
9+
import org.apache.hadoop.hbase.client.HBaseAdmin;
10+
11+
import java.io.IOException;
12+
13+
//install hbase locally & hbase master start
14+
public class HbaseClientExample {
15+
16+
public static void main(String[] args) throws IOException, ServiceException {
17+
new HbaseClientExample().connect();
18+
}
19+
20+
private void connect() throws IOException, ServiceException {
21+
Configuration config = HBaseConfiguration.create();
22+
23+
String path = this.getClass().getClassLoader().getResource("hbase-site.xml").getPath();
24+
25+
config.addResource(new Path(path));
26+
27+
try {
28+
HBaseAdmin.checkHBaseAvailable(config);
29+
} catch (MasterNotRunningException e) {
30+
System.out.println("HBase is not running." + e.getMessage());
31+
return;
32+
}
33+
34+
HBaseClientOperations HBaseClientOperations = new HBaseClientOperations();
35+
HBaseClientOperations.run(config);
36+
}
37+
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
3+
<configuration>
4+
<property>
5+
<name>hbase.zookeeper.quorum</name>
6+
<value>localhost</value>
7+
</property>
8+
<property>
9+
<name>hbase.zookeeper.property.clientPort</name>
10+
<value>2181</value>
11+
</property>
12+
</configuration>

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
<module>handling-spring-static-resources</module>
5353
<module>hazelcast</module>
54+
<!--<module>hbase</module>-->
5455
<module>httpclient</module>
5556
<module>hystrix</module>
5657

0 commit comments

Comments
 (0)