栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java通过thrift连接hbase查询数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java通过thrift连接hbase查询数据

文章目录
      • 1、依赖(thrift,非zk)
      • 2、连接
      • 3、查询
      • 4、数据筛选
        • 4.1 根据时间戳筛选
        • 4.2 根据行键筛选
      • 5、字段绑定

1、依赖(thrift,非zk)

参考:https://blog.csdn.net/chinabestchina/article/details/105720328


    org.apache.hbase
    hbase-client
    2.0.0-beta-1



    org.apache.hbase
    hbase-common
    2.0.0-beta-1



    org.apache.hbase
    hbase-thrift
    1.0.0

PS. 测试了hbase-thrift 2.0.0版本,会报错,所以暂时先用着1.0.0的

2、连接
    private static void connect() throws TTransportException {
        // 创建连接 需要的参数 ip,端口,超时时间(可选)
        transport = new TSocket(host, port, timeout);
        TProtocol protocol = new TBinaryProtocol(transport);
        client = new THBaseService.Client(protocol);
        logger.info(host + port +timeout);
        transport.open();
    }
3、查询
public static void scan(THBaseService.Iface client,String table) {
    try {
        //批量查询
        TScan scan = new TScan();
        List resultList = client.getScannerResults(
            ByteBuffer.wrap(table.getBytes(StandardCharsets.UTF_8)), scan, 100000
        );
        resultList.forEach(r -> {
            r.getColumnValues().forEach(c -> {
                // getRow       行键(r)
                // getFamily    列簇(c)
                // getQualifier 列名(c)
                // getValue     值(c)
            });
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
4、数据筛选 4.1 根据时间戳筛选
public static void scan(THBaseService.Iface client,String table) {
    try {
        long gap = 24 * 60 * 60 * 1000;
        long now = new Date().getTime();
        //批量查询
        TScan scan = new TScan();
        // 设置时间戳 一天间隔
        long startTime = now - gap;
        long endTime = now;
        scan.setTimeRange(new TTimeRange(startTime, endTime));
        List resultList = client.getScannerResults(
            ByteBuffer.wrap(table.getBytes(StandardCharsets.UTF_8)), scan, 100000
        );
        // resultList.forEach(...);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
4.2 根据行键筛选

行键规则:字段phone + 下划线 + new Date().getTime() (反转) + 下划线 + 随机字符

public static void scan(THBaseService.Iface client,String table) {
    try {
        String phone = "123456";
        long gap = 24 * 60 * 60 * 1000;
        long now = new Date().getTime();
        //批量查询
        TScan scan = new TScan();
        // 一天间隔
        String startTime = phone + "_" + String.valueOf(now - gap);
        String endTime = phone + "_" + String.valueOf(now);
        // 设置行键
        scan.setStartRow(Bytes.toBytes(reverse(startTime));
        scan.setStopRow(Bytes.toBytes(reverse(endTime)));
        List resultList = client.getScannerResults(
            ByteBuffer.wrap(table.getBytes(StandardCharsets.UTF_8)), scan, 100000
        );
        // resultList.forEach(...);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private static String reverse(String str) {
    return new StringBuffer(str).reverse().toString();
}
5、字段绑定

由于当前hbase上只设置了一个列簇,一列,所以需要解析数据绑定字段

    
    public static Map setFields(String[] fieldsArray, String line) {
        Map map = new HashMap<>();
        try {
            // StringUtils.split 分割会不完全
            String[] l = line.split("\|");
            for (int i = 0; i < l.length; i++) {
                // 转码(避免中文乱码)
                byte[] bytes = l[i].getBytes(StandardCharsets.UTF_8);
                map.put(fieldsArray[i], new String(bytes));
            }
        } catch (Exception e) {
            System.out.println("setFields error" + e.message);
            e.printStackTrace();
        }
        return map;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847707.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号