HBase Java API 插入数据主要有以下几种方法,样例代码

以下是HBase Java API 插入数据的样例代码,包括使用Put类、Batch操作、HTableInterface类和HTable类:

1.Put类

// 创建HBase Configuration对象
Configuration conf = HBaseConfiguration.create();

// 获取HBase表连接
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("table_name"));

// 创建Put对象
Put put = new Put(Bytes.toBytes("row_key"));

// 添加列数据
put.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_name"), Bytes.toBytes("value"));

// 插入数据
table.put(put);

// 关闭连接
table.close();
connection.close();

2.Batch操作

// 创建HBase Configuration对象
Configuration conf = HBaseConfiguration.create();

// 获取HBase表连接
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("table_name"));

// 创建Put对象
Put put1 = new Put(Bytes.toBytes("row_key1"));
put1.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_name"), Bytes.toBytes("value1"));

Put put2 = new Put(Bytes.toBytes("row_key2"));
put2.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_name"), Bytes.toBytes("value2"));

// 创建Put列表
List<Put> puts = new ArrayList<Put>();
puts.add(put1);
puts.add(put2);

// 批量插入数据
table.put(puts);

// 关闭连接
table.close();
connection.close();

3.HTableInterface类

// 创建HBase Configuration对象
Configuration conf = HBaseConfiguration.create();

// 获取HBase表连接
Connection connection = ConnectionFactory.createConnection(conf);
HTableInterface table = connection.getTable("table_name");

// 创建Put对象
Put put = new Put(Bytes.toBytes("row_key"));

// 添加列数据
put.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_name"), Bytes.toBytes("value"));

// 插入数据
table.put(put);

// 关闭连接
table.close();
connection.close();

4.HTable类

// 创建HBase Configuration对象
Configuration conf = HBaseConfiguration.create();

// 获取HBase表连接
Connection connection = ConnectionFactory.createConnection(conf);
HTable table = new HTable(conf, "table_name");

// 创建Put对象
Put put = new Put(Bytes.toBytes("row_key"));

// 添加列数据
put.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_name"), Bytes.toBytes("value"));

// 插入数据
table.put(put);

// 关闭连接
table.close();
connection.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值