Java操作Hbase

启动docker容器

docker run -d -h myhbase -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16020:16020 -p 16030:16030 --name hbase1.3 harisekhon/hbase:1.3

配置hosts文件

xx.xx.xx.xx myhabse
1. 引入hbase依赖
        
            org.apache.hbase
            hbase-server
            1.3.1
        
        
            org.apache.hbase
            hbase-client
            1.3.1
        

如果测试HADOOP_HOME的错误请下载hadoop-common,并配置hadoop.home.dir
https://blog.csdn.net/liu16659/article/details/84069297

java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set.
2.编写测试类
public class HbaseClientTest {

    public static Configuration conf;
    @Before
    public void beforeInit() {
        System.setProperty("hadoop.home.dir","D:\\program files\\hadoop-common-2.2.0-bin-master");
        //使用HbaseConfiguration的单例方法实例化
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","XXX");
        conf.set("hbase.zookeeper.property.clientPort","2181");
    }

    /**
     * 表是否存在
     */
    @Test
    public void isTableExist() throws IOException {
        String tableName = "student";
        //在Hbase中管理、访问表需要先创建HBaseAdmin对象
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        System.out.println(admin.tableExists(tableName));
    }

    /**
     * 创建表
     * @throws IOException
     */
    @Test
    public void createTable() throws IOException {

        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        String tableName = "student";

        //创建表属性对象,表名需要转字节
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
        List columnFamily = new ArrayList<>();
        columnFamily.add("info");
        //创建多个列族
        for(String cf : columnFamily) {
            descriptor.addFamily(new HColumnDescriptor(cf));
        }

        //根据对表的配置,创建表
        admin.createTable(descriptor);
        System.out.println("表" + tableName + "创建成功!");
    }

    /**
     * 删除表
     */

    @Test
    public void dropTable() throws IOException {
        String tableName = "student";
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }

    /**
     * 向表中插入数据
     */
    @Test
    public void addRowData() throws IOException {
        String tableName = "student";

        //创建HTable对象
        HTable hTable = new HTable(conf,tableName);
        //向表中插入数据
        Put put = new Put(Bytes.toBytes("1001"));
        //向Put对象中组装数据
        put.add(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("male"));
        hTable.put(put);
        hTable.close();
        System.out.println("插入数据成功");
    }

    /**
     * 删除多行数据
     */
    @Test
    public void deleteMultiRow() throws IOException {
        String tableName = "student";
        HTable hTable = new HTable(conf,tableName);
        List rows = new ArrayList<>();
        rows.add("1001");
        List deleteList = new ArrayList<>();
        for(String row : rows) {
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteList.add(delete);
        }

        hTable.delete(deleteList);
        hTable.close();
    }

    /**
     * 获取所有数据
     */
    @Test
    public void getAllRows() throws IOException {
        String tableName = "student";
        HTable hTable = new HTable(conf,tableName);
        //得到用于扫描的region对象
        Scan scan = new Scan();
        //使用HTable得到resultscanner实现类的对象
        ResultScanner resultScanner = hTable.getScanner(scan);
        for(Result result :resultScanner) {
            Cell[] cells  = result.rawCells();
            for(Cell cell : cells) {
                //得到rowkey
                System.out.println("行键: " + Bytes.toString(CellUtil.cloneRow(cell)));
                //得到列族
                System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("列: "+ Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

    /**
     * 获取某一行数据
     */
    @Test
    public void getRow() throws IOException {
        String tableName = "student";
        HTable hTable = new HTable(conf,tableName);
        Get get = new Get(Bytes.toBytes("1001"));
        //get.setMaxVersions(); // 显示所有版本
       //get.setTimeStamp();// 显示指定时间戳的版本

        Result result = hTable.get(get);
        for(Cell cell : result.rawCells()) {
            System.out.println("行键: " + Bytes.toString(result.getRow()));
            System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列: "+ Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("时间戳: " + cell.getTimestamp());
        }
    }

    /**
     * 获取某一行指定"列族:列"的数据
     */
    @Test
    public void getRowQualifier() throws IOException {
        String tableName = "student";
        HTable hTable = new HTable(conf,tableName);
        Get get = new Get(Bytes.toBytes("1001"));
        get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("sex"));
        Result result = hTable.get(get);
        for(Cell cell : result.rawCells()) {
            System.out.println("行键: " + Bytes.toString(result.getRow()));
            System.out.println("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列: "+ Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }
}

你可能感兴趣的:(Java操作Hbase)