The below Java program uses the HBase 2.0 API and performs the following:
Reference:
https://hbase.apache.org/apidocs/
- Create Table
- Put Row
- Get Row
- Scan Rows
- Delete Column
- Disable Table
- Delete Table
package nag.arvind.gudiseva; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; 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.util.Bytes; import java.io.IOException; /** * Created by Nag Arvind Gudiseva on 6/4/2015. */public class EmployeeHBase { Connection connection; public EmployeeHBase(Connection connection) { this.connection = connection; } void createTable (String tableStr, String colStr1, String colStr2) throws IOException { HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableStr)); HColumnDescriptor columnFamily1 = new HColumnDescriptor(colStr1); tableDescriptor.addFamily(columnFamily1); HColumnDescriptor columnFamily2 = new HColumnDescriptor(colStr2); tableDescriptor.addFamily(columnFamily2); HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); System.out.println("Creating table ..."); admin.createTable(tableDescriptor); System.out.println(tableDescriptor.toString()); System.out.println("... table created!"); admin.close(); } void putRow(String tableStr, String rowStr, String colStr, String qlfrStr, String valueStr) throws IOException { Table table = connection.getTable(TableName.valueOf(tableStr)); System.out.println("Connecting to table ..."); Put p = new Put(Bytes.toBytes(rowStr)); p.addColumn(Bytes.toBytes(colStr), Bytes.toBytes(qlfrStr), Bytes.toBytes(valueStr)); table.put(p); System.out.println("... Added records to table!"); table.close(); } void getRow(String tableStr, String rowStr, String colStr, String qlfrStr) throws IOException { Table table = connection.getTable(TableName.valueOf(tableStr)); System.out.println("Connecting to table ..."); Get g = new Get(Bytes.toBytes(rowStr)); Result r = table.get(g); byte [] value = r.getValue(Bytes.toBytes(colStr), Bytes.toBytes(qlfrStr)); String valueStr = Bytes.toString(value); System.out.println("... GET: " + valueStr); table.close(); } void scanRows(String tableStr, String colStr, String qlfrStr) throws IOException { Table table = connection.getTable(TableName.valueOf(tableStr)); System.out.println("Connecting to table ..."); Scan s = new Scan(); s.addColumn(Bytes.toBytes(colStr), Bytes.toBytes(qlfrStr)); ResultScanner scanner = table.getScanner(s); try { for (Result row : scanner) { System.out.println("... Found row: " + row); } } finally { scanner.close(); } table.close(); } void deleteColumn (String tableStr, String colStr) throws IOException { HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); System.out.println("Deleting Column ..."); admin.deleteColumn(tableStr, colStr); System.out.println("... Column " + colStr + " from table " + tableStr + " deleted!"); admin.close(); } void disableTable (String tableStr) throws IOException { HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); System.out.println("Disabling table ..."); admin.disableTable(tableStr); System.out.println("... " + tableStr + " table disabled!"); admin.close(); } void deleteTable (String tableStr) throws IOException { HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); System.out.println("Deleting table ..."); admin.deleteTable(tableStr); System.out.println("... " + tableStr + " table deleted!"); admin.close(); } public static void main(String[] args) throws IOException { Configuration config = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(config); RegionLocator regionLocator = conn.getRegionLocator(TableName.valueOf("Employee")); // For future implementation EmployeeHBase employeeHBase = new EmployeeHBase(conn); employeeHBase.createTable("Employee", "personal", "official"); employeeHBase.putRow("Employee", "emp102", "official", "name", "Arvind"); employeeHBase.getRow("Employee", "emp102", "official", "name"); employeeHBase.scanRows("Employee", "official", "name"); employeeHBase.deleteColumn("Employee", "official"); employeeHBase.disableTable("Employee"); employeeHBase.deleteTable("Employee"); regionLocator.close(); conn.close(); } }
Reference:
https://hbase.apache.org/apidocs/
No comments:
Post a Comment