HBASE数据库API操作

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import static  org.apache.hadoop.hbase.util.Bytes.toBytes;
import java.io.IOException;
import java.util.ArrayList;

public class HBASEDemo1 {

    //创建并获取带有hbase配置的configuration 对象
    private static Configuration conf = HBaseConfiguration.create();
    //获取操作的表名
    public static TableName tableName =  TableName.valueOf("table_2");

    public static void createTable(){
        Connection conn = null;
        HBaseAdmin admin = null;
        try {
            //获取zk连接
            conn = ConnectionFactory.createConnection(conf);
//            获取表管理对象
            admin = (HBaseAdmin)conn.getAdmin();
//            创建表描述对象
            HTableDescriptor table = new HTableDescriptor(tableName);
//          创建列族对象
            HColumnDescriptor cf1 = new HColumnDescriptor(toBytes("cf1"));
//            设置列族的最大版本
            cf1.setMaxVersions(3);
            HColumnDescriptor cf2 = new HColumnDescriptor(toBytes("cf2"));
            cf2.setMaxVersions(2);
//            把列族添加到表对象中
            table.addFamily(cf1);
            table.addFamily(cf2);
            //判断表是否存在
            if(admin.tableExists(tableName)){
                System.out.println("表已经存在");
                return;
            }
            //创建表
            admin.createTable(table);
            System.out.println("表创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(conn,null, admin);
        }
    }

    //添加数据到表
    public  static void putTable(){
        Connection conn = null;
        HTable table = null;
        try{
//            通过zookeeper跟hbase服务相连
            conn = ConnectionFactory.createConnection(conf);
            table = (HTable) conn.getTable(tableName);

            Put put = new Put(toBytes("id01"));
            put.addColumn(toBytes("cf1"), toBytes("name"), toBytes("aaa"));
            put.addColumn(toBytes("cf1"), toBytes("age"), toBytes("10"));
            put.addColumn(toBytes("cf2"), toBytes("cert_type"), toBytes("100"));
            put.addColumn(toBytes("cf2"), toBytes("cert_no"), toBytes("NO100"));

            table.put(put);
            System.out.println("put success");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
        }
    }

    //添加多行数据
    private static void putsTable(){
        Connection conn = null;
        HTable table = null;
        try {
            conn = conn = ConnectionFactory.createConnection(conf);
            table = (HTable) conn.getTable(tableName);

            ArrayList<Put> puts = new ArrayList<>();

            Put put1 = new Put(toBytes("id02"));
            put1.addColumn(toBytes("cf1"),toBytes("name"),toBytes("haha"));
            put1.addColumn(toBytes("cf1"),toBytes("address"),toBytes("shanghai"));
            put1.addColumn(toBytes("cf2"),toBytes("gender"),toBytes("male"));

            Put put2 = new Put(toBytes("rowId007"));
            put2.addColumn(toBytes("cf1"),toBytes("name"),toBytes("daneimitan007"));
            put2.addColumn(toBytes("cf2"),toBytes("job"),toBytes("private"));

            puts.add(put1);
            puts.add(put2);
            table.put(puts);
            System.out.println("添加多行成功");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(conn,table,null);
        }
    }

    //获取一行数据
    private  static  void  getTable(){
        Connection conn = null;
        HTable table = null;
        try {
            conn = ConnectionFactory.createConnection(conf);
            table = (HTable)conn.getTable(tableName);
            //创建一行的get对象
            Get get = new Get(toBytes("rowId007"));
            //查询指定的列族
            get.addColumn(toBytes("cf1"),toBytes("name"));
            get.setMaxVersions(2);
            //获取结果集
            Result result = table.get(get);
            //打印结果
            printResult(result);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            close(conn,table,null);
        }
    }

    //查询多行数据
    private  static  void  scanTable(){
        Connection conn = null;
        HTable table = null;
        try {
            conn = ConnectionFactory.createConnection(conf);
            table = (HTable)conn.getTable(tableName);
            //创建scan 对象
            Scan scan = new Scan();
            //添加scan的条件  指定查询 列族为 cf1
            //scan.addColumn(toBytes("cf1"), toBytes("name"));
            //根据startrow stoprow 查询区间  查询 id01-id02 但是不包含id02
            //scan.setStartRow(toBytes("id01"));
            //startrow < stoprow, 查询结果不包含stoprow
            //scan.setStopRow(toBytes("id02"));

            //不加条件全表扫描
            scan.setMaxVersions(3);
            //获取多行数据结果集
            ResultScanner scanner = table.getScanner(scan);
            for (Result result: scanner) {
                printResult(result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(conn,table,null);
        }
    }

    //删除指定行

    public static void deleteRow(){
        Connection conn = null;
        HTable table = null;
        try{
            //通过zookeeper跟hbase服务相连
            conn = ConnectionFactory.createConnection(conf);
            table = (HTable) conn.getTable(tableName);

            Delete de = new Delete(toBytes("id02"));
            table.delete(de);
            System.out.println("####删除成功####");

        }catch(Exception e){
            e.printStackTrace();
        }finally{
           close(conn,table,null);
        }

    }

    //打印结果集中的一行数据
    private static void printResult(Result result){
        //将结果集分成多个列的结果集
        Cell[] rawCells = result.rawCells();
        for(Cell cell : rawCells){
            StringBuilder sb = new StringBuilder();
            sb.append(new String("行键=>"));
            sb.append(new String(CellUtil.cloneRow(cell))).append("\t列族=>");
            sb.append(new String(CellUtil.cloneFamily(cell))).append("\tKEY:")
                    .append(new String(CellUtil.cloneQualifier(cell))).append("\t时间戳=>")
                    .append(cell.getTimestamp()).append("\t值=>")
                    .append(new String(CellUtil.cloneValue(cell)));
            System.out.println(sb.toString());
        }
    }

    /**
     * 删除某一行的列
     */
    public static void deleteQualifier(){
        Connection conn = null;
        HTable table = null;
        try{
//            通过zookeeper跟hbase服务相连
            conn = ConnectionFactory.createConnection(conf);
            table = (HTable) conn.getTable(tableName);

            Delete delete = new Delete(toBytes("id02"));
//            删除指定列最新的版本数据
//            delete.addColumn(toBytes("cf_1"), toBytes("age"));
//            删除指定列匹配时间戳的版本数据,如果匹配不上,不删除
//            delete.addColumn(toBytes("cf_1"), toBytes("age"),1531451908268L);
//            删除指定列当前时间戳及之前的所有版本数据
//            delete.addColumns(toBytes("cf_1"), toBytes("age"), 1531452313838L);
//            删除指定列所有数据
            delete.addColumns(toBytes("cf1"), toBytes("age"));
            table.delete(delete);
            System.out.println("delete row success");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            close(conn,table,null);
        }
    }

    /**
     * 删除表的列族
     */
    public static void deleteColumnFamily(){
        Connection conn = null;
        HBaseAdmin admin = null;
        try{
//            通过zookeeper跟hbase服务相连
            conn = ConnectionFactory.createConnection(conf);
//            获取表的管理者对象
            admin = (HBaseAdmin) conn.getAdmin();

            admin.deleteColumn(tableName, toBytes("cf2"));
            System.out.println("delete columnfamily seccess");

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            close(conn,null,admin);
        }
    }

    /**
     * 删表
     */
    public static void dropTable(){
        Connection conn = null;
        HBaseAdmin admin = null;
        try{
//            通过zookeeper跟hbase服务相连
            conn = ConnectionFactory.createConnection(conf);
//            获取表的管理者对象
            admin = (HBaseAdmin) conn.getAdmin();
//            先将表下线
            admin.disableTable(tableName);
//            再删除表
            admin.deleteTable(tableName);

            System.out.println("drop table success");

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            close(conn,null,admin);
        }
    }

    private static void close(Connection conn, HTable table, HBaseAdmin admin) {
        try{
            if(null != admin){
                admin.close();
            }
            if(table != null){
                table.close();
            }
            if(conn != null){
                conn.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        createTable();
        putTable();
        putsTable();
//        getTable();
//        deleteRow();
//        deleteQualifier();
//        deleteColumnFamily();
        scanTable();
//        dropTable();
    }
}

posted @ 2018-07-15 15:20  马鞍山  阅读(306)  评论(0编辑  收藏  举报