Java api配置_java api操作

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 puts = new ArrayList();

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();

}

}

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值