hbase的访问方式
1、native java api:最常规和高效的访问方式;
2、hbase shell:hbase的命令行工具,最简单的接口,适合hbase管理使用;
3、thrift gateway:利用thrift序列化技术,支持c++,php,python等多种语言,适合其他异构系统在线访问hbase表数据;
4、rest gateway:支持rest 风格的http api访问hbase, 解除了语言限制;
5、mapreduce:直接使用mapreduce作业处理hbase数据;
6、使用pig/hive处理hbase数据。
常用java api的用法:
1、加载配置
configuration config = hbaseconfiguration.create(); //可以自定义配置,也可以从自定义配置文件中读取 /*config.set("hbase.zookeeper.property.clientport", "4181"); config.set("hbase.zookeeper.quorum", "hadoop.datanode5.com,hadoop.datanode2.com,hadoop.datanode3.com"); config.set("hbase.master", "hadoop.datanode3.com\\:600000");*/
2、表的创建、表信息修改、表删除
hbaseadmin admin = new hbaseadmin(config); //创建表 htabledescriptor htd = new htabledescriptor(tablename); htd.addfamily(new hcolumndescriptor("cf1")); htd.addfamily(new hcolumndescriptor("cf2")); admin.createtable(htd); //修改表信息 admin.disabletable(tablename); // modifying existing columnfamily admin.modifycolumn(tablename, new hcolumndescriptor("cf1")); admin.enabletable(tablename); //删除表 admin.disabletable(bytes.tobytes(tablename)); admin.deletetable(bytes.tobytes(tablename));
3、添加记录
/** 在多次使用时,建议用htablepool htable table = new htable(config, tablename); => htablepool pool = new htablepool(config, 1000); htableinterface table = pool.gettable(tablename);*/ htable table = new htable(config, tablename); /** * 在插入操作时,默认不适用任何缓存 * 可自定义使用缓存,以及缓存大小 * 每个任务最后需要手工调用 flushcommits(); */ /*table.setautoflush(false); table.setwritebuffersize(1024);*/ put put1 = new put(bytes.tobytes(rowkey)); if (ts == 0) { put1.add(bytes.tobytes(family), bytes.tobytes(qualifier), bytes.tobytes(value)); } else { //自定义版本时,从自定义的版本号,类型为long put1.add(bytes.tobytes(family), bytes.tobytes(qualifier), ts,bytes.tobytes(value)); } table.put(put1); //table.flushcommits();
4、查询,根据rowkey查询
get get1 = new get(bytes.tobytes(rowkey)); result result = table.get(get1); system.out.println("get result:" + bytes.tostring(result.getvalue(bytes.tobytes(family), bytes.tobytes(qualifier)))); result[] result = table.get(list<get>);//查询指定rowkey的多条记录
5、查询,指定条件和rowkey区间查询
scan scan = new scan(); //默认缓存大小为1,设置成一个合理的值,可以减少scan过程中next()的时间开销,代价是客户端的内存 scan.setcaching(500); scan.setcacheblocks(false); //根据startrowkey、endrowkey查询 //scan scan = new scan(bytes.tobytes("startrowkey"), bytes.tobytes("endrowkey")); //rowkey之外的过滤条件,在list中可以add; /**list<filter> filters = new arraylist<filter>(); filter filter = new singlecolumnvaluefilter("familyname".getbytes(), "qualifiername".getbytes(), compareop.equal, bytes.tobytes("value")); filters.add(filter); scan.setfilter(new filterlist(filters));*/ resultscanner scanner = table.getscanner(scan); system.out.println("scan result list:"); for (result result : scanner) { system.out.println(bytes.tostring(result.getrow())); system.out.println(bytes.tostring(result.getvalue(bytes.tobytes("data"), bytes.tobytes("data1")))); system.out.println(bytes.tostring(result.getvalue(bytes.tobytes("data"), bytes.tobytes("data2")))); } scanner.close();
总结
以上所述是小编给大家介绍的hbase访问方式之java api,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
Algebra:Chapter 0 - 预备知识: 集合论和categories
springboot + vue上传图片在服务器并实现在线预览
请谨慎使用增强for循环,刚接触Java值得一看(手动狗头)
【Nginx】还不会使用Nginx解决跨域问题?肝这一篇就够了!!
网友评论