hbase使用笔记
背景:由于项目需要,最近要用到hbase,虽然尽管hbase使用的接口还算简单,但是第一次接触使用,各种配置相关的东西以及中间遇到了一些环境问题真是有些麻烦
最最常用的指令:
1.查看hbase有什么表 list
2.创建一个表: create '表名', ‘字段1’, ‘字段二’,。。。
3.往表中插入一条数据:put '表名',‘rowkey’,‘字段一:’,‘字段一栏目一名称’,‘字段一栏目一’,.....
4.查看整个表某列的内容:scan '表名',‘行名称’,‘列名称’
5.获得某行记录:get ‘表名’,'rowkey'
6.查看表中记录总数:count '表名'
调用hbase接口应该注什么?
1.初始化配置(如):
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "10.XXX.1.XXX,10.XXX.XXX.43,10.XXX.XXX.44");
conf.set("hbase.rootdir", "hdfs://10.XXX.4.XXX:9000/user/hbase");
conf.set("hbase.cluster.distributed", "true");
conf.set("hbase.zookeeper.property.clientPort", "XXX");
conf.set("hbase.master.port", "XXX");
conf.set("hbase.master.info.port", "XXX");
conf.set("hbase.client.write.buffer", "52428800");
conf.set("hbase.zookeeper.property.dataDir", "/data/home/zookeeper");
conf.set("hbase.tmp.dir", "/data/home/hbase_tmp");
conf.set("zookeeper.znode.parent", "/hbase");
conf.set("hbase.hregion.max.filesize", "1073741824");
conf.set("hbase.regionserver.handler.count", "20");
conf.set("zookeeper.session.timeout", "180000");
这个相当于hbase客户端程序的site.xml配置文件
读写注意事项:
读写接口中传的参数都是二进制数据流,调用的时候注意转换
- Source Code
public static void readHbase(String table, String rowKey, String outfile, Configuration conf) throws IOException {
byte[] ret = null;
HTable htable = new HTable(conf, table);
Get g = new Get(Bytes.toBytes(rowKey));
System.out.println(g.toString());
Result m = htable.get(g);
ret = m.getValue("tarj_value".getBytes(), "Candy".getBytes());
File out_file = new File(outfile);
OutputStream out = null;
try {
out = new FileOutputStream(out_file);
out.write(ret);
out.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
public static void writeHbase(String table, String rowKey, String infile, Configuration conf) throws IOException {
File file = new File(infile);
InputStream in = null;
long filelength = file.length();
byte[] filecontent = null;
try {
in = new FileInputStream(file);
filecontent = new byte[(int)filelength];
in.read(filecontent);
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
Put put_rows = new Put(rowKey.getBytes());
put_rows.add("tarj_value".getBytes(), "Candy".getBytes(), filecontent);
HTable htable = new HTable(conf, table);
htable.put(put_rows);
}