From 1ff8b4c69c26de7e5d496f4c85e4ec5b8749a5e5 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 6 Mar 2017 11:34:27 +0100 Subject: [PATCH] Bael 655 (#1256) BAEL-655 hbase --- hbase/pom.xml | 51 +++++ .../baeldung/hbase/HBaseClientOperations.java | 193 ++++++++++++++++++ .../baeldung/hbase/HbaseClientExample.java | 38 ++++ hbase/src/main/resources/hbase-site.xml | 12 ++ pom.xml | 1 + 5 files changed, 295 insertions(+) create mode 100644 hbase/pom.xml create mode 100644 hbase/src/main/java/org/baeldung/hbase/HBaseClientOperations.java create mode 100644 hbase/src/main/java/org/baeldung/hbase/HbaseClientExample.java create mode 100644 hbase/src/main/resources/hbase-site.xml diff --git a/hbase/pom.xml b/hbase/pom.xml new file mode 100644 index 0000000000..2382e47af2 --- /dev/null +++ b/hbase/pom.xml @@ -0,0 +1,51 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + hbase + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + org.apache.hbase + hbase-client + ${hbase.version} + + + org.apache.hbase + hbase + ${hbase.version} + + + junit + junit + ${junit.version} + test + + + + + 1.3.0 + 4.12 + + + + \ No newline at end of file diff --git a/hbase/src/main/java/org/baeldung/hbase/HBaseClientOperations.java b/hbase/src/main/java/org/baeldung/hbase/HBaseClientOperations.java new file mode 100644 index 0000000000..c78eacc834 --- /dev/null +++ b/hbase/src/main/java/org/baeldung/hbase/HBaseClientOperations.java @@ -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; + + /** + * + * + * Row1Family1:Qualifier1Family1:Qualifier2 + * + * + * Row2Family1:Qualifier1Family2:Qualifier3 + * + * + * Row3Family1:Qualifier1Family2:Qualifier3 + * + *
+ */ + 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 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."); + } +} \ No newline at end of file diff --git a/hbase/src/main/java/org/baeldung/hbase/HbaseClientExample.java b/hbase/src/main/java/org/baeldung/hbase/HbaseClientExample.java new file mode 100644 index 0000000000..07cb7df480 --- /dev/null +++ b/hbase/src/main/java/org/baeldung/hbase/HbaseClientExample.java @@ -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); + } + +} \ No newline at end of file diff --git a/hbase/src/main/resources/hbase-site.xml b/hbase/src/main/resources/hbase-site.xml new file mode 100644 index 0000000000..895529161c --- /dev/null +++ b/hbase/src/main/resources/hbase-site.xml @@ -0,0 +1,12 @@ + + + + + hbase.zookeeper.quorum + localhost + + + hbase.zookeeper.property.clientPort + 2181 + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 72099fc584..9c24200a0b 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ handling-spring-static-resources hazelcast + httpclient hystrix