hbase的api编程put数据_2018-08-18期 Hbase客户端API操作( 插入数据)

该博客演示了如何使用HBase API进行数据的插入操作,包括单行插入和批量插入。首先通过HBaseConfiguration创建配置对象,并设置Zookeeper的连接信息,然后建立HBaseAdmin和HTable对象。在插入数据时,创建Put对象,指定行键rowkey和各列族的字段及其值,最后执行put操作。
摘要由CSDN通过智能技术生成

package cn.itcast.bigdata.hbase;

import java.io.IOException;

import java.io.InterruptedIOException;

import java.util.ArrayList;

import java.util.Iterator;

import org.apache.commons.collections.comparators.NullComparator;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.MasterNotRunningException;

import org.apache.hadoop.hbase.NamespaceDescriptor;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.ZooKeeperConnectionException;

import org.apache.hadoop.hbase.client.Delete;

import org.apache.hadoop.hbase.client.Get;

import org.apache.hadoop.hbase.client.HBaseAdmin;

import org.apache.hadoop.hbase.client.HTable;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.filter.BinaryComparator;

import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;

import org.apache.hadoop.hbase.filter.ByteArrayComparable;

import org.apache.hadoop.hbase.filter.CompareFilter;

import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;

import org.apache.hadoop.hbase.filter.Filter;

import org.apache.hadoop.hbase.filter.FilterBase;

import org.apache.hadoop.hbase.filter.PrefixFilter;

import org.apache.hadoop.hbase.filter.RegexStringComparator;

import org.apache.hadoop.hbase.filter.RowFilter;

import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

import org.apache.hadoop.hbase.filter.SubstringComparator;

import org.apache.hadoop.hbase.util.Bytes;

import org.apache.hadoop.io.BinaryComparable;

import org.junit.Before;

import org.junit.Test;

/**

* Hbase客户端操作示例

*

* @author songjq

*

*/

/**

* Hbase客户端操作示例

*

* @author songjq

*

*/

public class HbaseAPIDemo {

// 数据库连接对象

private HBaseAdmin hBaseAdmin = null;

// 表连接对象

private HTable hTable = null;

/**

* 获取Hbase对象连接

*

* @throws MasterNotRunningException

* @throws ZooKeeperConnectionException

* @throws IOException

*/

@Before

public void getHbaseConn() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {

/**

* 通过这种通用的配置对象构造的方法来创建一个配置对象 这种方法会自动加载classpath下的

* core-site.xml,hdfs-site.xml,core-default.xml...等这些hadoop的配置文件

*/

// Configuration hdfsconf = new Configuration();

/**

* HBaseConfiguration.create()则会自动加载classpath下的hadoop下的配置文件 及hbase-site.xml

*/

Configuration conf = HBaseConfiguration.create();

/**

* 这里通过zk集群地址连接hbase集群,只需要把hbase-site.xml中zk连接地址拷贝过来即可

* /usr/local/apps/hbase-0.96.2-hadoop2/conf/hbase-site.xml

*/

conf.set("hbase.zookeeper.quorum", "hadoop-server01:2181,hadoop-server02:2181,hadoop-server03:2181");

// 构造一个DDL操作的客户端对象hBaseAdmin

hBaseAdmin = new HBaseAdmin(conf);

// 实例化表连接对象

hTable = new HTable(conf, "oadb:t_user_info");

}

/**

* 插入数据,一次插入一行

*

* @throws IOException

* @throws RetriesExhaustedWithDetailsException

*/

@Test

public void putTestOneLine() throws RetriesExhaustedWithDetailsException, IOException {

// 先将需要插入的数据封装成Put对象,也就是行键rowkey

Put put = new Put("user000001".getBytes());

// 执行插入的列族为base_info、字段为userid、username、sex、address

put.add("base_info".getBytes(), "userid".getBytes(), "Usercode000001".getBytes());

put.add("base_info".getBytes(), "username".getBytes(), "张三".getBytes("utf-8"));

put.add(Bytes.toBytes("base_info"), Bytes.toBytes("sex"), "男".getBytes("utf-8"));

put.add("base_info".getBytes(), "address".getBytes(), "Beijing".getBytes());

// 执行插入的列族为extra_info,字段为orgid、orgname

put.add("extra_info".getBytes(), "orgid".getBytes(), "Orgcode000001".getBytes());

put.add("extra_info".getBytes(), "orgname".getBytes(), "昆明市烟草专卖局".getBytes("utf-8"));

// 执行插入操作

hTable.put(put);

// 关闭客户端连接

hTable.close();

}

/**

* 批量插入多行

*

* @throws IOException

* @throws RetriesExhaustedWithDetailsException

*/

@Test

public void putTestMutilLines() throws RetriesExhaustedWithDetailsException, IOException {

ArrayList puts = new ArrayList();

for (int i = 2; i < 12; i++) {

/**

* 一次性插入10行数据

*/

// 构造行键rowkey

Put put = new Put(("user00000" + i).getBytes());

// 构造列族中列对象

put.add("base_info".getBytes(), "userid".getBytes(), ("Usercode00000" + i).getBytes());

put.add("base_info".getBytes(), "username".getBytes(), ("张三" + i).getBytes("utf-8"));

put.add(Bytes.toBytes("base_info"), Bytes.toBytes("age"), ("2" + i).getBytes("utf-8"));

put.add("base_info".getBytes(), "address".getBytes(), "Shanghai".getBytes());

// 执行插入的列族为extra_info,字段为orgid、orgname

put.add("extra_info".getBytes(), "orgid".getBytes(), ("Usercode00000" + i).getBytes());

put.add("extra_info".getBytes(), "orgname".getBytes(), "昆明市烟草专卖局".getBytes("utf-8"));

put.add("extra_info".getBytes(), "leader".getBytes(), "李四".getBytes("utf-8"));

puts.add(put);

}

// 执行插入操作

hTable.put(puts);

// 关闭客户端连接

hTable.close();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值