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.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");
}
/**
* 数据查询,get方式【单行查询】 查询方式比较单一
*
* @throws IOException
*/
@Test
public void getTest() throws IOException {
/*
* 构造查询的行键rowkey
*/
Get get = new Get(Bytes.toBytes("user0000010"));
/*
* 查询该行中一个完整列族Family base_info 不指定就查询所有列族
*/
get.addFamily(Bytes.toBytes("base_info"));
/*
* 执行查询
*/
Result result = hTable.get(get);
/*
* 从结果集中获取对应列族的字段
*
*/
byte[] base_info_userid = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("userid"));
byte[] base_info_username = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("username"));
byte[] base_info_address = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("address"));
/*
* 输出结果集 由于hbase是按bytes方式存放数据,因此获取数据字段值时需要和put方式保持一致
*/
System.out.println("base_info_userid:" + Bytes.toString(base_info_userid));
System.out.println("base_info_username:" + Bytes.toString(base_info_username));
System.out.println("base_info_address:" + Bytes.toString(base_info_address));
/*
* 关闭连接
*/
hTable.close();
}
/**
* 数据查询,get方式【多行查询】 查询方式比较单一
*
* @throws IOException
*/
@Test
public void getMoreLines() throws IOException {
/*
* 构造查询list对象
*/
ArrayList<Get> gets = new ArrayList<Get>();
/*
* 循环获取3个get对象
*/
for (int i = 7; i < 10; i++) {
Get get = new Get(Bytes.toBytes("user00000" + i));
/*
* 查询该行键中多个列族中部分字段 family->base_info-> userid,age family->extra_info->orgname
* 不指定就查询所有列族
*/
get.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("userid"));
get.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
get.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("orgname"));
/*
* 将对象get添加到gets
*/
gets.add(get);
}
/*
* 执行多行查询,返回结果集合Result数组
*/
Result[] results = hTable.get(gets);
/*
* 遍历每个数组值
*/
System.out.println("base_info_userid\t" + "base_info_age\t" + "base_info_orgname");
for (Result result : results) {
byte[] base_info_userid = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("userid"));
byte[] base_info_age = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
byte[] base_info_orgname = result.getValue(Bytes.toBytes("extra_info"), Bytes.toBytes("orgname"));
System.out.println(Bytes.toString(base_info_userid) + "\t\t\t" + Bytes.toString(base_info_age) + "\t"
+ Bytes.toString(base_info_orgname));
}
/*
* 关闭客户端连接
*/
hTable.close();
}
}