【原创】大叔经验分享(3)hbase client 如何选择

时间:2023-01-06 23:10:14

java中访问hbase有两种方式,一种是hbase自带的client,一种是通过hbase thrift

1 hbase client示例

        Configuration conf = HBaseConfiguration.create();

        conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "2181");

        conf.set(HConstants.ZOOKEEPER_QUORUM, "zk_host");

        Connection connection = ConnectionFactory.createConnection(conf);

        Table profileTable = connection.getTable(TableName.valueOf("test_table"));

 

        Result result = profileTable.get(new Get(Bytes.toBytes("test_rowkey")));

        System.out.println(new String(result.getValue(Bytes.toBytes("test_cf"), Bytes.toBytes("test_column"))));

这里只需要配置zookeeper,访问的过程是先通过zookeeper找hmaster,然后通过hmaster定位到一个region server,然后访问region server,所以需要客户端到hbase所有节点(包括zookeeper、hmaster、region server)可达,即host能ping通;

 

2 hbase thrift示例

        TTransport transport = null;

        try {

            transport = new TSocket(host, port, timeout);

            transport.open();

            TProtocol protocol = new TBinaryProtocol(transport);

            THBaseService.Iface client = new THBaseService.Client(protocol);

            ByteBuffer table = ByteBuffer.wrap(tableName.getBytes());

            TGet get = new TGet(ByteBuffer.wrap(rowKey.getBytes()));

            try {

                TResult result = client.get(table, get);

                return result;

            } catch (Exception e) {

                throw new RuntimeException(e);

            }

        } catch (Exception e) {

            throw new RuntimeException(e);

        } finally {

            transport.close();

        }

这里只需要配置hbase thrift server的ip和端口,没有任何其他依赖;单点问题可以通过加load balancer来解决;

 

综上,如果不需要配host,两种都可以,如果需要配host,使用thrift更简单;