Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions hbase/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hbase</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<hbase.version>1.3.0</hbase.version>
<junit.version>4.12</junit.version>
</properties>


</project>
193 changes: 193 additions & 0 deletions hbase/src/main/java/org/baeldung/hbase/HBaseClientOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package org.baeldung.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class HBaseClientOperations {
private final static byte[] cellData = Bytes.toBytes("cell_data");

/**
* Drop tables if this value is set true.
*/
static boolean INITIALIZE_AT_FIRST = true;

/**
* <table>
* <tr>
* <tb>Row1</tb> <tb>Family1:Qualifier1</tb> <tb>Family1:Qualifier2</tb>
* </tr>
* <tr>
* <tb>Row2</tb> <tb>Family1:Qualifier1</tb> <tb>Family2:Qualifier3</tb>
* </tr>
* <tr>
* <tb>Row3</tb> <tb>Family1:Qualifier1</tb> <tb>Family2:Qualifier3</tb>
* </tr>
* </table>
*/
private final TableName table1 = TableName.valueOf("Table1");
private final String family1 = "Family1";
private final String family2 = "Family2";

private final byte[] row1 = Bytes.toBytes("Row1");
private final byte[] row2 = Bytes.toBytes("Row2");
private final byte[] row3 = Bytes.toBytes("Row3");
private final byte[] qualifier1 = Bytes.toBytes("Qualifier1");
private final byte[] qualifier2 = Bytes.toBytes("Qualifier2");
private final byte[] qualifier3 = Bytes.toBytes("Qualifier3");

private void createTable(Admin admin) throws IOException {
HTableDescriptor desc = new HTableDescriptor(table1);
desc.addFamily(new HColumnDescriptor(family1));
desc.addFamily(new HColumnDescriptor(family2));
admin.createTable(desc);
}

private void delete(Table table) throws IOException {
final byte[] rowToBeDeleted = Bytes.toBytes("RowToBeDeleted");
System.out.println("\n*** DELETE ~Insert data and then delete it~ ***");

System.out.println("Inserting a data to be deleted later.");
Put put = new Put(rowToBeDeleted);
put.addColumn(family1.getBytes(), qualifier1, cellData);
table.put(put);

Get get = new Get(rowToBeDeleted);
Result result = table.get(get);
byte[] value = result.getValue(family1.getBytes(), qualifier1);
System.out.println("Fetch the data: " + Bytes.toString(value));
assert Arrays.equals(cellData, value);

System.out.println("Deleting");
Delete delete = new Delete(rowToBeDeleted);
delete.addColumn(family1.getBytes(), qualifier1);
table.delete(delete);

result = table.get(get);
value = result.getValue(family1.getBytes(), qualifier1);
System.out.println("Fetch the data: " + Bytes.toString(value));
assert Arrays.equals(null, value);

System.out.println("Done. ");
}

private void deleteTable(Admin admin) throws IOException {
if (admin.tableExists(table1)) {
admin.disableTable(table1);
admin.deleteTable(table1);
}
}

private void filters(Table table) throws IOException {
System.out.println("\n*** FILTERS ~ scanning with filters to fetch a row of which key is larget than \"Row1\"~ ***");
Filter filter1 = new PrefixFilter(row1);
Filter filter2 = new QualifierFilter(CompareOp.GREATER_OR_EQUAL, new BinaryComparator(
qualifier1));

List<Filter> filters = Arrays.asList(filter1, filter2);

Scan scan = new Scan();
scan.setFilter(new FilterList(Operator.MUST_PASS_ALL, filters));

try (ResultScanner scanner = table.getScanner(scan)) {
int i = 0;
for (Result result : scanner) {
System.out.println("Filter " + scan.getFilter() + " matched row: " + result);
i++;
}
assert i == 2 : "This filtering sample should return 1 row but was " + i + ".";
}
System.out.println("Done. ");
}

private void get(Table table) throws IOException {
System.out.println("\n*** GET example ~fetching the data in Family1:Qualifier1~ ***");

Get g = new Get(row1);
Result r = table.get(g);
byte[] value = r.getValue(family1.getBytes(), qualifier1);

System.out.println("Fetched value: " + Bytes.toString(value));
assert Arrays.equals(cellData, value);
System.out.println("Done. ");
}

private void put(Admin admin, Table table) throws IOException {
System.out.println("\n*** PUT example ~inserting \"cell-data\" into Family1:Qualifier1 of Table1 ~ ***");

// Row1 => Family1:Qualifier1, Family1:Qualifier2
Put p = new Put(row1);
p.addImmutable(family1.getBytes(), qualifier1, cellData);
p.addImmutable(family1.getBytes(), qualifier2, cellData);
table.put(p);

// Row2 => Family1:Qualifier1, Family2:Qualifier3
p = new Put(row2);
p.addImmutable(family1.getBytes(), qualifier1, cellData);
p.addImmutable(family2.getBytes(), qualifier3, cellData);
table.put(p);

// Row3 => Family1:Qualifier1, Family2:Qualifier3
p = new Put(row3);
p.addImmutable(family1.getBytes(), qualifier1, cellData);
p.addImmutable(family2.getBytes(), qualifier3, cellData);
table.put(p);

admin.disableTable(table1);
try {
HColumnDescriptor desc = new HColumnDescriptor(row1);
admin.addColumn(table1, desc);
System.out.println("Success.");
} catch (Exception e) {
System.out.println("Failed.");
System.out.println(e.getMessage());
} finally {
admin.enableTable(table1);
}
System.out.println("Done. ");
}

public void run(Configuration config) throws IOException {
try (Connection connection = ConnectionFactory.createConnection(config)) {

Admin admin = connection.getAdmin();
if (INITIALIZE_AT_FIRST) {
deleteTable(admin);
}

if (!admin.tableExists(table1)) {
createTable(admin);
}

Table table = connection.getTable(table1);
put(admin, table);
get(table);
scan(table);
filters(table);
delete(table);
}
}

private void scan(Table table) throws IOException {
System.out.println("\n*** SCAN example ~fetching data in Family1:Qualifier1 ~ ***");

Scan scan = new Scan();
scan.addColumn(family1.getBytes(), qualifier1);

try (ResultScanner scanner = table.getScanner(scan)) {
for (Result result : scanner)
System.out.println("Found row: " + result);
}
System.out.println("Done.");
}
}
38 changes: 38 additions & 0 deletions hbase/src/main/java/org/baeldung/hbase/HbaseClientExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.baeldung.hbase;


import com.google.protobuf.ServiceException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import java.io.IOException;

//install hbase locally & hbase master start
public class HbaseClientExample {

public static void main(String[] args) throws IOException, ServiceException {
new HbaseClientExample().connect();
}

private void connect() throws IOException, ServiceException {
Configuration config = HBaseConfiguration.create();

String path = this.getClass().getClassLoader().getResource("hbase-site.xml").getPath();

config.addResource(new Path(path));

try {
HBaseAdmin.checkHBaseAvailable(config);
} catch (MasterNotRunningException e) {
System.out.println("HBase is not running." + e.getMessage());
return;
}

HBaseClientOperations HBaseClientOperations = new HBaseClientOperations();
HBaseClientOperations.run(config);
}

}
12 changes: 12 additions & 0 deletions hbase/src/main/resources/hbase-site.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.zookeeper.quorum</name>
<value>localhost</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
</configuration>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

<module>handling-spring-static-resources</module>
<module>hazelcast</module>
<!--<module>hbase</module>-->
<module>httpclient</module>
<module>hystrix</module>

Expand Down