package com.ccse.hbase; import java.io.IOException; 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.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class HBaseTest { //声明静态配置 static Configuration cfg = HBaseConfiguration.create(); static { cfg.setStrings("hbase.zookeeper.quorum", "chaoren1"); } //创建一张表,通过HBaseAdmin HTableDescriptor来创建 public static void create(String tableName, String... columnFamilies) throws IOException { //管理HBase的,主要负责DLL的操作 HBaseAdmin admin = new HBaseAdmin(cfg); if (admin.tableExists(tableName)) { System.out.println("Table Exists"); System.exit(0); } else { HTableDescriptor tableDesc = new HTableDescriptor(tableName); for (String columnFamily : columnFamilies) { tableDesc.addFamily(new HColumnDescriptor(columnFamily)); } admin.createTable(tableDesc); System.out.println("create table success!"); System.out.println("表是否存在:" + admin.tableExists(tableName)); admin.close(); } } //添加一条数据,通过HTable Put为已经存在的表添加数据 public static void put(String tableName, String row, String columnFamily, String column, String data) throws IOException { //管理表中数据的,主要负责DML操作 HTable table = new HTable(cfg, tableName); Put put1 = new Put(Bytes.toBytes(row)); put1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data)); table.put(put1); System.out.println("put '" + row + "','" + columnFamily + ":" + column + "','" + data + "'"); table.close(); } public static void get(String tableName, String row) throws IOException { HTable table = new HTable(cfg, tableName); Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); System.out.println("Get:" + result); System.out.println("result:" + Bytes.toString(result.getValue(Bytes.toBytes("row1"), Bytes.toBytes("cl1")))); table.close(); } //显示所有数据,通过HTable Scan来获取已有表的信息 public static void scan(String tableName) throws IOException { HTable table = new HTable(cfg, tableName); Scan scan = new Scan(); ResultScanner rs = table.getScanner(scan); for (Result r : rs) { System.out.println(r); } } //删除表 public static boolean delete(String tableName) throws IOException { HBaseAdmin admin = new HBaseAdmin(cfg); if (admin.tableExists(tableName)) { try { admin.disableTable(tableName); admin.deleteTable(tableName); } catch (Exception e) { e.printStackTrace(); return false; } } admin.close(); return true; } public static void main(String[] args) { String tableName = "hbase_tb"; String columnFamily = "cf"; try { HBaseTest.create(tableName, columnFamily); HBaseTest.put(tableName, "row1", columnFamily, "cl1", "data"); HBaseTest.get(tableName, "row1"); HBaseTest.scan(tableName); if (HBaseTest.delete(tableName)) { System.out.println("Delete table " + tableName + " success!"); } } catch (Exception e) { e.printStackTrace(); } } }