-涉及主要对象认识
- HBaseConfiguration: HBase配置类
- HConnectionManager: HBase的java连接类
- HBaseAdmin: HBase的管理类,主要对表的管理,创建,禁用,删除等
- HTableDescriptor: 表属性的描述,添加列族等
- HTable: HBase的表对象,用操作表,添加数据,表扫面等
package com.fang.demo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.KeyValue;
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.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
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.Scan;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.TimestampsFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HBaseFilterTest {
public static Configuration configuration;
public static HConnection hConnection;
@Before
public void testBefore() {
try {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "redhat");
configuration.set("hbase.master", "redhat:600000");
hConnection = HConnectionManager.createConnection(configuration);
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
}
@After
public void testAfter() {
try {
hConnection.close();
System.out.println("close");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @Description: 创建表
* 通过HBaseAdmin管理hbase中的表
* HTableDescriptor管理hbase中的列族信息
*/
@Test
public void createTable() throws Exception {
String tableName = "user";
HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
if (hBaseAdmin.tableExists(tableName)) {
// hBaseAdmin.disableTable(tableName);
// hBaseAdmin.deleteTable(tableName);
System.out.println(tableName + "表已经存在,并删除表");
}
HTableDescriptor hDescriptor = new HTableDescriptor(tableName);
hDescriptor.addFamily(new HColumnDescriptor("name"));
hDescriptor.addFamily(new HColumnDescriptor("age"));
hDescriptor.addFamily(new HColumnDescriptor("dept"));
hBaseAdmin.createTable(hDescriptor);
hBaseAdmin.close();
}
/***
*
* @Description: 插入表
*/
@Test
public void testPut() throws Exception {
String tableName = "user";
HTable table = (HTable) hConnection.getTable(tableName);
table.setAutoFlush(false);// 关闭自动刷新,提交IO吞吐率
List<Put> list = new ArrayList<Put>(1000);
for (int i = 0; i < 200000; i++) {
Put put = new Put(Bytes.toBytes("user_" + i));
put.add(Bytes.toBytes("name"), Bytes.toBytes("FirstName"), Bytes.toBytes("First" + i));
put.add(Bytes.toBytes("name"), Bytes.toBytes("SecondName"), Bytes.toBytes("Second" + i));
put.add(Bytes.toBytes("age"), null, Bytes.toBytes(i + ""));
put.add(Bytes.toBytes("dept"), null, Bytes.toBytes("dept" + i));
list.add(put);
if (i % 1000 == 0) {
table.put(list);
list = new ArrayList<Put>(1000);
}
}
if (list.size() > 0) {
table.put(list);
}
table.close();
hConnection.close();
}
/**
*
* @Description: 通过key获取值
*/
@Test
public void testGet() throws Exception {
HTable table = (HTable) hConnection.getTable("user");
Get get = new Get(Bytes.toBytes("user_9985"));
Result result = table.get(get);
if (result != null && result.size() > 0) {
System.out.print("key=" + new String(result.getRow()) + " ");
for (KeyValue key : result.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
} else {
System.out.println("没有数据");
}
hConnection.close();
}
/**
* @Description: hbase扫描器
*/
@Test
public void testScan() throws Exception {
HTable table = (HTable) hConnection.getTable("user");
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
}
/**
*
* @Description: HBase过滤器
* 单列值过滤器(SingleColumnValueFilter)
*/
@Test
public void testScanFilter() throws Exception {
HTable table = (HTable) hConnection.getTable("user");
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("name"), Bytes.toBytes("FirstName"), CompareOp.EQUAL, Bytes.toBytes("First22"));
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
}
/**
*
* @Description: 多扫描器使用
*/
@Test
public void testScanFilterList() throws Exception {
HTable table = (HTable) hConnection.getTable("user");
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
List<Filter> filterList = new ArrayList<>();
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("name"), Bytes.toBytes("FirstName"), CompareOp.EQUAL, Bytes.toBytes("First22"));
Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes("age"), null, CompareOp.EQUAL, Bytes.toBytes("22"));
filterList.add(filter);
filterList.add(filter2);
FilterList fList = new FilterList(filterList);
scan.setFilter(fList);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
}
@Test
public void deleteTableRow() {
try {
HTable table = new HTable(configuration, "user");
List<Delete> list = new ArrayList<Delete>();
Delete d1 = new Delete("user19".getBytes());
list.add(d1);
table.delete(list);
System.out.println("删除行成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @Description: 行过滤器
*/
@Test
public void testRowFilter() throws Exception {
HTable table = (HTable) hConnection.getTable("user");
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
Filter filter = new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("user_22")));
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
}
/**
*
* @Description: 分页过滤器
*/
@Test
public void testPageFilter() throws Exception {
HTable table = (HTable) hConnection.getTable("user");
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
PageFilter filter = new PageFilter(20);//每页20条
scan.setStartRow(Bytes.toBytes(20));//从20行开始,就是查询第二页
// scan.setStopRow(s)设置结束行
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
}
/**
*
* @throws Exception
* @Description: 前缀过滤器(PrefixFilter)
*/
@Test
public void testPrefixFilter() throws Exception{
HTable table = (HTable) hConnection.getTable("user");
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
Filter filter = new PrefixFilter(Bytes.toBytes("user_222"));
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
}
/**
* 时间戳过滤器(TimestampsFilter)
*/
@Test
public void testTimestampsFilter() throws Exception{
HTable table = (HTable) hConnection.getTable("user");
Scan scan = new Scan();
scan.setCaching(1000);// 设置扫描器缓存区的大小
List<Long> ts = new ArrayList<Long>();
ts.add(new Long(1462618668891L));
ts.add(new Long(1462618657998L));
ts.add(new Long(1462618668392L));
Filter filter = new TimestampsFilter(ts);
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.print("key=" + new String(r.getRow()) + " ");
for (KeyValue key : r.raw()) {
System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + " ");
}
System.out.println();
}
}
}