Hbase Api简单操作(Java)_javaapi操作hbase

Put put = new Put(Bytes.toBytes("row1")); // co PutExample-3-NewPut Create put with specific row.
//设置导入指定列族:列,值
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
  Bytes.toBytes("val100")); // co PutExample-4-AddCol1 Add a column, whose name is "colfam1:qual1", to the put.
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
  Bytes.toBytes("val200")); // co PutExample-4-AddCol2 Add another column, whose name is "colfam1:qual2", to the put.
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"),
        Bytes.toBytes("val300"));
put.addColumn(Bytes.toBytes("colfam2"), Bytes.toBytes("qual1"),
        Bytes.toBytes("val300"));

//put入数据
table.put(put); // co PutExample-5-DoPut Store row with column into the HBase table.
//关闭表
table.close(); // co PutExample-6-DoPut Close table and connection instances to free resources.
//关闭连接
connection.close();
// ^^ PutExample
//关闭
helper.close();
// vv PutExample

}
}
// ^^ PutExample


### 2.查询(get)



package client;

// cc GetExample Example application retrieving data from HBase
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import util.HBaseHelper;

public class GetExample {

public static void main(String[] args) throws IOException {
// vv GetExample
Configuration conf = HBaseConfiguration.create(); // co GetExample-1-CreateConf Create the configuration.

conf.addResource("hdfs-site.xml");

// ^^ GetExample
HBaseHelper helper = HBaseHelper.getHelper(conf);
if (!helper.existsTable("testtable")) {
  helper.createTable("testtable", "colfam1");
}
// vv GetExample
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable")); // co GetExample-2-NewTable Instantiate a new table reference.

Get get = new Get(Bytes.toBytes("row1")); // co GetExample-3-NewGet Create get with specific row.

get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co GetExample-4-AddCol Add a column to the get.

Result result = table.get(get); // co GetExample-5-DoGet Retrieve row with selected columns from HBase.

byte[] val = result.getValue(Bytes.toBytes("colfam1"),
  Bytes.toBytes("qual1")); // co GetExample-6-GetValue Get a specific value for the given column.

System.out.println("Value: " + Bytes.toString(val)); // co GetExample-7-Print Print out the value while converting it back.

table.close(); // co GetExample-8-Close Close the table and connection instances to free resources.
connection.close();
// ^^ GetExample
helper.close();

}
}


### 3.删除操作(delete)



package client;

// cc DeleteExample Example application deleting data from HBase
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import util.HBaseHelper;

public class DeleteExample {

public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();

HBaseHelper helper = HBaseHelper.getHelper(conf);
helper.dropTable("testtable");
helper.createTable("testtable", 100, "colfam1", "colfam2");
helper.put("testtable",
  new String[] { "row1" },
  new String[] { "colfam1", "colfam2" },
  new String[] { "qual1", "qual1", "qual2", "qual2", "qual3", "qual3" },
  new long[]   { 1, 2, 3, 4, 5, 6 },
  new String[] { "val1", "val1", "val2", "val2", "val3", "val3" });
System.out.println("Before delete call...");
helper.dump("testtable", new String[]{ "row1" }, null, null);

Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));

// vv DeleteExample
Delete delete = new Delete(Bytes.toBytes("row1")); // co DeleteExample-1-NewDel Create delete with specific row.

delete.setTimestamp(1); // co DeleteExample-2-SetTS Set timestamp for row deletes.

delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co DeleteExample-3-DelColNoTS Delete the latest version only in one column.
delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), 3); // co DeleteExample-4-DelColTS Delete specific version in one column.

delete.addColumns(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co DeleteExample-5-DelColsNoTS Delete all versions in one column.
delete.addColumns(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), 2); // co DeleteExample-6-DelColsTS Delete the given and all older versions in one column.

delete.addFamily(Bytes.toBytes("colfam1")); // co DeleteExample-7-AddCol Delete entire family, all columns and versions.
delete.addFamily(Bytes.toBytes("colfam1"), 3); // co DeleteExample-8-AddCol Delete the given and all older versions in the entire column family, i.e., from all columns therein.

table.delete(delete); // co DeleteExample-9-DoDel Delete the data from the HBase table.

// ^^ DeleteExample
table.close();

最后

image.png

9-DoDel Delete the data from the HBase table.

// ^^ DeleteExample
table.close();

最后

[外链图片转存中…(img-AxrOv38K-1716575277485)]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值