首页 > 编程语言 > 详细

Java操作HBase

时间:2019-04-02 23:12:07      阅读:164      评论:0      收藏:0      [点我收藏+]

HBaseUtils工具类,实现了对hbase的put/get/scan/delete操作,直接操作对象即可,源码地址:https://gitee.com/cnsugar/common-hbase

 

直接上示例代码:

@Test
public void testCreateTable() {
    HBaseUtils.createTable("SUGAR_TEST");
}

@Test
public void testPut() {
    TestEntity entity = new TestEntity();
    entity.setServiceId(10002L);
    entity.setDataType(2);
    entity.setOnTime(new Date());
    entity.setServiceCode("A2000100010001");
    entity.setRow("0266e33d15264525258891");
    HBaseUtils.put(entity);
}

@Test
public void testPutMap() {
    Map<String, Object> map = new HashMap<>();
    map.put("SERVICE_ID", 10003L);
    map.put("DATA_TYPE", 2);
    map.put("AT_TIME", null);
    map.put("SERVICE_CODE", "A3000100010001");
    map.put("ROW", "0266e33d15264525258892");
    HBaseUtils.putMap(map, "SUGAR_TEST");
}

@Test
public void testGet() {
    TestEntity entity = HBaseUtils.get("0266e33d15264525258890", TestEntity.class);
    System.out.println(entity);
}

@Test
public void testGetMap() {
    Map<String, Object> map = HBaseUtils.get("SUGAR_TEST", "0266e33d15264525258890", new IValueMapper() {
        @Override
        public Object mapValue(String column, byte[] value) {
            if (value == null || value.length == 0) {
                return null;
            }
            switch (column) {
                case "AT_TIME":
                    return new Date(Bytes.toLong(value));
                case "DEST_PORT":
                case "DATA_TYPE":
                    return Bytes.toInt(value);
                case "SERVICE_ID":
                    return Bytes.toLong(value);
                default:
                    return Bytes.toString(value);
            }
        }
    });
    System.out.println(map);
}

@Test
public void testScan() {
    List<TestEntity> list = HBaseUtils.scan(new Scan(), TestEntity.class);
    System.out.println(list);
}

@Test
public void testScanMap() {
    List<Map<String, Object>> list = HBaseUtils.scan("SUGAR_TEST", new Scan(), new IValueMapper() {
        @Override
        public Object mapValue(String column, byte[] value) {
            if (value == null || value.length == 0) {
                return null;
            }
            switch (column) {
                case "AT_TIME":
                    return new Date(Bytes.toLong(value));
                case "DEST_PORT":
                case "DATA_TYPE":
                    return Bytes.toInt(value);
                case "SERVICE_ID":
                    return Bytes.toLong(value);
                default:
                    return Bytes.toString(value);
            }
        }
    });
    System.out.println(list);
}

  

 

Java操作HBase

原文:https://www.cnblogs.com/cnsugar/p/10646035.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!