java api操作

时间:2022-09-01 15:52:04
java api操作
导入开发包
将hbase安装包中lib下包导入java项目
 
创建表
 
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum",
"CentOS01:2181,CentOS02:2181,CentOS03:2181");
 
HBaseAdmin admin = new HBaseAdmin(conf);
 
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("tabe"));
HColumnDescriptor hcd_fam1 = new HColumnDescriptor("fam1");
hcd_fam1.setMaxVersions(3);
HColumnDescriptor hcd_fam2 = new HColumnDescriptor("fam2");
htd.addFamily(hcd_fam1);
htd.addFamily(hcd_fam2);
 
admin.createTable(htd);
 
admin.close();
 
 
插入数据
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","CentOS01:2181,CentOS02:2181,CentOS03:2181");
 
HTable table = new HTable(conf,"tabe");
Put put = new Put(Bytes.toBytes("row1"));
put.add(Bytes.toBytes("fam1"),Bytes.toBytes("col1"),Bytes.toBytes("val1"));
put.add(Bytes.toBytes("fam1"),Bytes.toBytes("col2"),Bytes.toBytes("val2"));
put.add(Bytes.toBytes("fam2"),Bytes.toBytes("col3"),Bytes.toBytes("val3"));
table.put(put);
 
table.close();
 
 
 
**javaapi操作hbase时,入口类为HTable,此对象创建时需要扫描.META表,以及其他操作,这非常耗时,所以,应该将该对象设置为单例,复用该对象,如果需要多个HTable对象,应该使用HTable
Pool,通过对象池复用对象。
HTablePool pool = new HTablePool(conf,10);//不知道为什么过时了?
**hbase所有修改数据的操作都保证了行级别的原子性,
 
试验:一次插入100万条数据
HTable table = new HTable(conf,"tabx");
List<Put> puts = new ArrayList<Put>();
for(int i=1;i<=1000000;i++){
Put put = new Put(Bytes.toBytes("row"+i));
put.add(Bytes.toBytes("fam1"),Bytes.toBytes("col1"),Bytes.toBytes("val"+i))
puts.add(put);
 
if(i % 10000 == 0){
table.put(puts);
puts = new ArrayList<Put>();
}
}
table.put(puts);
table.close();
 
 
获取数据
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","CentOS01:2181,CentOS02:2181,CentOS03:2181");
 
HTable table = new HTable(conf,"tabe");
Get get = new Get(Bytes.toBytes("row1"));
Result result = table.get(get);
byte [] bs = result.getValue(Bytes.toBytes("fam1"),Bytes.toBytes("col1"));
String str = Bytes.toString(bs);
System.out.println(str);
 
table.close();
 
 
获取数据集
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","CentOS01:2181,CentOS02:2181,CentOS03:2181");
 
HTable table = new HTable(conf,"tabe");
Scan scan = new Scan(Bytes.toBytes("row1"));
ResultScanner scanner = table.getScanner(scan);
Iterator it = scanner.iterator();
while(it.hasNext()){
Result result = (Result) it.next();
byte [] bs = result.getValue(Bytes.toBytes("fam1"),Bytes.toBytes("col1"));
String str = Bytes.toString(bs);
System.out.println(str);
}
table.close();
 
 
删除数据
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","CentOS01:2181,CentOS02:2181,CentOS03:2181");
 
HTable table = new HTable(conf,"tabe");
Delete delete = new Delete(Bytes.toBytes("row1"));
table.delete(delete);
table.close();
 
 
删除表
 
//1.创建配置对象
HBaseConfiguration conf = new HBaseConfiguration();
conf.set("hbase.zookeeper.quorum", "CentOS01");
//2.创建HBaseAdmin对象
HBaseAdmin admin = new HBaseAdmin(conf);
//3.删除表
admin.disableTable(Bytes.toBytes("tab1"));
admin.deleteTable(Bytes.toBytes("tab1"));
//4.关闭连接
admin.close();