zoukankan      html  css  js  c++  java
  • 【原创】大叔经验分享(3)hbase client 如何选择

    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更简单;

  • 相关阅读:
    QT中PRO文件写法的详细介绍,很有用,很重要!
    What is the difference between authorized_key and known_host file for SSH
    SHELL DATE 命令详解
    Sample program to use PC/SC API.
    如何让用户关闭客户端IE时,触发Session_End事件
    Sql Create Function简单例子
    Css2.0实现圆角边框
    运算符重载的一个例子
    HTML获取URL传递的参数
    C#中自定义属性的例子
  • 原文地址:https://www.cnblogs.com/barneywill/p/10109258.html
Copyright © 2011-2022 走看看