Hbase Java API

1.maven依赖

引入CDH中央仓库,以及hbase-client的依赖

 <repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
        </repository>
    </repositories>
     <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>

2.连接Hbase

public class TestHbase {
    
    /**
     * 1.创建表
     */
    @Test
    public  void creatTable() throws IOException {
        Configuration config = HBaseConfiguration.create();
        //指定zookeeper的地址和端口,这里使用主机名,需要在 C:\Windows\System32\drivers\etc目录下hosts添加映射,例如:在hosts最后一行添加 192.168.2.2 heikn02 (其中的ip是hbase的服务器)
        config.set("hbase.zookeeper.quorum","heikn02");
        config.set("hbase.zookeeper.property.clientPort","2181");
        System.out.println("开始连接hbase");
        Connection connect = ConnectionFactory.createConnection(config);
        System.out.println(connect.isClosed());
        Admin admin = connect.getAdmin();
        TableName tableName = TableName.valueOf("user1");
        if(!admin.tableExists(tableName)){
            System.out.println("创建表开始=========================");
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            hTableDescriptor.addFamily(new HColumnDescriptor("base_info"));
            admin.createTable(hTableDescriptor);
            System.out.println("创建表完成===========================");
        }else {
            System.out.println("表存在");
        }
    }

    /**
     * 2.获取表的信息
     */
    @Test
    public void tableInfo() throws Exception{
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum","heikn02");
        config.set("hbase.zookeeper.property.clientPort","2181");
        Connection connect = ConnectionFactory.createConnection();
        TableName tableName = TableName.valueOf("user1");
        //获取表
        Table table = connect.getTable(tableName);
        //获取表的描述器
        HTableDescriptor tableDescriptor = table.getTableDescriptor();
        //获取表的列族信息
        HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
        for(HColumnDescriptor hColumnDescriptor : columnFamilies){
            byte[] name = hColumnDescriptor.getName();
            String value = Bytes.toString(name);
            System.out.println("列名:" + value);
        }
    }

    /**
     * 3.添加数据-PUT
     */
    @Test
    public void put() throws Exception{
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum","heikn02");
        config.set("hbase.zookeeper.property.clientPort","2181");

        TableName tableName = TableName.valueOf("user1");
        Connection conn = ConnectionFactory.createConnection(config);
        //获取表
        Table table = conn.getTable(tableName);
        //准备数据
        String rowkey = "rokey_10";
        Put zhangsan = new Put(Bytes.toBytes(rowkey));
        zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("张三"));
        zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("sex"), Bytes.toBytes("1"));
        zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("address"), Bytes.toBytes("北京市"));
        zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("birthday"), Bytes.toBytes("2014-07-10"));
        //添加数据
        table.put(zhangsan);
        table.close();
    }

    /**
     * 4.获取数据 通过rowkey查询  GET
     */
    @Test
    public void get() throws Exception{
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum","heikn02");
        config.set("hbase.zookeeper.property.clientPort","2181");

        Connection connection = ConnectionFactory.createConnection(config);
        TableName tableName = TableName.valueOf("user1");
        Table table = connection.getTable(tableName);

        //准备条件
        String rowkey = "rokey_10";
        Get get = new Get(Bytes.toBytes(rowkey));
        Result result = table.get(get);
        List<Cell> cells = result.listCells();
        for (Cell cell : cells){
            System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("family:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }

    }

    /**
     * 5.获取数据 全表扫描 Scan
     */
    @Test
    public void scan() throws Exception{
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum","heikn02");
        config.set("hbase.zookeeper.property.clientPort","2181");
        Connection connection = ConnectionFactory.createConnection(config);
        TableName tableName = TableName.valueOf("user1");
        Table table = connection.getTable(tableName);

        Scan scan = new Scan();

        //5.1只取info列族
        //scan.addFamily(Bytes.toBytes("info"));

        //5.2只取info列族下 age列
        //scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));

        //5.3查询指定范围的rowkey
        //scan.setStartRow(Bytes.toBytes("pk1"));
        //scan.setStartRow(Bytes.toBytes("pk3"));

        //5.4单个Filter设置
        //Filter filter = new PrefixFilter(Bytes.toBytes("p"));
        //scan.setFilter(filter);

        //5.5多个Filter设置
        // 多个条件是and  还是or可以设置
        //FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        //Filter filter1 = new PrefixFilter(Bytes.toBytes("p"));
        //scan.setFilter(filter1);
        //Filter filter2 = new PrefixFilter(Bytes.toBytes("j"));
        //scan.setFilter(filter2);
        //filterList.addFilter(filter1);
        //filterList.addFilter(filter2);
        //scan.setFilter(filterList);

        ResultScanner scanner = table.getScanner(scan);
        Result result = null;
        while ((result = scanner.next()) != null){
            List<Cell> cells = result.listCells();
            for (Cell cell : cells){
                System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)));
                System.out.println("family:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值