Java操作HBase

HBaseUtils工具类,实现了对hbase的put/get/scan/delete操作,直接操作对象即可,源码地址:https://gitee.com/cnsugar/common-hbase。 直接上示例代码: @Test public void testCreate...

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);
}

  

  • 发表于 2019-04-03 09:00
  • 阅读 ( 170 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除