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