Java API操作hbase

本文提供了使用Java进行HBase表管理及数据操作的具体示例代码,包括表的创建、删除、数据插入、批量写入、单条记录删除及数据查询等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近用到hbase,由于hbase是Java语言开发的,用Java操作hbase比较方便,提供的方法很多。下面是一些操作例子。
创建/删除表(DDL):

HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);

if (hBaseAdmin.tableExists(tableName)) {

hBaseAdmin.disableTable(tableName);

hBaseAdmin.deleteTable(tableName);

System.out.println(tableName + " is exist,detele....");

}

HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);

tableDescriptor.addFamily(new HColumnDescriptor(“info”)); hBaseAdmin.createTable(tableDescriptor);

hBaseAdmin.disableTable(tablename);

hBaseAdmin.deleteTable(tablename);

添加:

HTable hTable = new HTable(configuration, table);

Put pt = new Put(Bytes.toBytes(rowKey));

pt.add(Bytes.toBytes(columnFamily),Bytes.toBytes(identifier),Bytes.toBytes(data));

hTable.put(pt);

HTable table = new HTable(conf, tableName);

List puts = new ArrayList(10000);

for(int i = 1;i<=1000000;i++) {

Put put = new Put(Bytes.toBytes("rk"+i));

put.add(Bytes.toBytes(“info”),Bytes.toBytes(“id”),Bytes.toBytes(“rk”+i)); put.add(Bytes.toBytes(“info”),Bytes.toBytes(“name”),Bytes.toBytes(“stu_”+i));

puts.add(put);

if(i%10000 == 0) {

    table.put(puts);

    puts = new ArrayList<Put>(10000);

}

}

删除:

HTable table = new HTable(conf, tableName);

Delete delete = new Delete(Bytes.toBytes(rowKey));

table.delete(delete);

List delList = new ArrayList();

For(String row : rows) {

Delete del = new Delete(row.getBytes());

delist.add(del);

}

Table.delete(delList);

查询:

String tableName = “student”;

HTable table = new HTable(conf, tableName);

Get get = new Get(“rk999900”.getBytes());

Result result = table.get(get);

for(KeyValue kv : result.raw()) {

System.out.print("列名:" + new String(kv.getQualifier()) + " ");

System.out.println("值:" + new String(kv.getValue()));

}

String tableName = “student”;

HTable table = new HTable(conf, tableName);

Scan scan = new Scan(“rk100”.getBytes(),”rk900”.getBytes());

ResultScanner scanner = table.getScanner(scan);

for(Result rs : scanner) {

for(KeyValue kv : rs.raw()) {

    System.out.print("列:" + new String(kv.getQualifier()) + " ");

    System.out.println("值:" + new String(kv.getValue()));

}

}

其中,有些方法被注解掉了,我在寻找最新写法。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值