Java操作HBase

java

HBaseUtils工具类,实现了对hbase的put/get/scan/delete操作,直接操作对象即可

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 的全部内容, 来源链接: utcz.com/z/393569.html

回到顶部