欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java操作HBase

程序员文章站 2022-04-08 17:51:52
HBaseUtils工具类,实现了对hbase的put/get/scan/delete操作,直接操作对象即可 ......

hbaseutils工具类,实现了对hbase的put/get/scan/delete操作,直接操作对象即可,源码地址:。

 

直接上示例代码:

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