依赖:
4.0.0 com.atlxl Hbase01 1.0-SNAPSHOT org.apache.hbase hbase-server 1.3.1 org.apache.hbase hbase-client 1.3.1 commons-collections commons-collections 3.1 commons-configuration commons-configuration 1.5 commons-codec commons-codec 1.10 commons-lang commons-lang 2.5
代码:
package com.atlxl;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import static com.sun.xml.internal.fastinfoset.alphabet.BuiltInRestrictedAlphabets.table;public class TestHbase { private static Admin admin = null; private static Connection connection = null; private static Configuration configuration =null; static { //Hbase配置文件// HBaseConfiguration configuration = new HBaseConfiguration(); configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "192.168.192.102"); //获取连接对象 try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } private static void close(Connection conn,Admin admin) { if (conn!=null) { try { conn.close(); } catch (IOException e) { e.printStackTrace(); } } if (admin!=null) { try { admin.close(); } catch (IOException e) { e.printStackTrace(); } } } //判断表是否存在 public static boolean tableExist(String tableName) throws IOException { //获取HBase管理员对象// HBaseAdmin admin = new HBaseAdmin(configuration); //执行// boolean tableExists = admin.tableExists(tableName); boolean tableExists = admin.tableExists(TableName.valueOf(tableName)); //关闭资源 admin.close(); return tableExists; } //创建表 public static void createTable(String tableName, String... cfs) throws IOException { if (tableExist(tableName)) { System.out.println("表已存在!!"); return; } //创建表描述器 HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); //添加列族 for (String cf : cfs) { //创建列描述器 HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf); hTableDescriptor.addFamily(hColumnDescriptor); } //创建表的操作 admin.createTable(hTableDescriptor); System.out.println("表创建成功!"); } //删除表 public static void deleteTable(String tableName) throws IOException { if (!tableExist(tableName)) { return; } //删除表之前先使表不可用(下线) admin.disableTable(TableName.valueOf(tableName)); //执行删除操作 admin.deleteTable(TableName.valueOf(tableName)); System.out.println("表已删除!!"); } //增//改 public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException { //获取表对象 Table table = connection.getTable(TableName.valueOf(tableName));// HTable table = new HTable(configuration, TableName.valueOf(tableName)); //创建Put对象 Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value)); //执行添加操作 table.put(put); } //删 public static void delete(String tableName, String rowKey, String cf, String cn) throws IOException { //获取table对象 Table table = connection.getTable(TableName.valueOf(tableName)); //创建delete对象 Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));// delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn)); //执行删除操作 table.delete(delete); table.close(); } //查 //全表扫描 public static void scanTable(String tableName) throws IOException { //获取table对象 Table table = connection.getTable(TableName.valueOf(tableName)); //构建扫描器 Scan scan = new Scan();// scan.setStartRow()// scan.setStopRow() ResultScanner results = table.getScanner(scan); //遍历数据并打印 for (Result result : results) { Cell[] cells = result.rawCells(); for (Cell cell : cells) { System.out.println("RK:" + Bytes.toString( CellUtil.cloneRow(cell)) + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell))); } } table.close(); } //获取指定列族:列的数据 public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException { //获取表对象 Table table = connection.getTable(TableName.valueOf(tableName)); //创建一个Get对象 Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn)); //指定版本// get.setMaxVersions(); //指定列族// get.addFamily(); //获取数据的操作 Result result = table.get(get); Cell[] cells = result.rawCells(); for (Cell cell : cells) { System.out.println("RK:" + Bytes.toString( CellUtil.cloneRow(cell)) + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell))); } table.close(); } public static void main(String[] args) throws IOException {// System.out.println(tableExist("student"));// System.out.println(tableExist("staff")); //调用创建表// createTable("staff", "info"); //调用删除表// deleteTable("staff"); //判断表是否存在// System.out.println(tableExist("staff")); //插入数据// putData("student", "1002", "info", "name", "zhangsan");// putData("student", "1002", "info", "sex", "female");// putData("student", "1002", "info", "age", "18");// putData("student", "1003", "info", "name", "lisi");// putData("student", "1003", "info", "sex", "male");// putData("student", "1004", "info", "age", "18");// putData("student", "1004", "info", "name", "wanwu");// delete("student", "1001", "info", "sex");// scanTable("student"); getData("student", "1003", "info", "name"); close(connection, admin); }}