首先先封装一个工具类HBaseUtil.java
public class HBaseUtil {
private static final String ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum";
private static final String ZOOKEEPER_ADDRS = "hadoop02,hadoop03,hadoop04";
private static Connection conn = null;
//工具类是不能创建对象
private HBaseUtil() {}
/**
* 获取Admin对象
* @return Admin对象
* @throws IOException
*/
public static Admin getAdmin() throws IOException {
//1.获取conf对象
Configuration conf = HBaseConfiguration.create();
//2.必须指定hbase的zk集群地址
conf.set(ZOOKEEPER_QUORUM,ZOOKEEPER_ADDRS);
//3.获取连接对象connection
conn = ConnectionFactory.createConnection(conf);
//4.获取Hbase操作对象
Admin ad = conn.getAdmin();
return ad;
}
/**
* 获取表操作对象
* @return table对象
* @throws IOException
* 为了达到表的通用性,需要给当前方一个参数 参数是表名 String tableName
*/
public static Table getTable() throws IOException {
//1.获取配置信息并指定zk集群
Configuration conf = HBaseConfiguration.create();
conf.set(ZOOKEEPER_QUORUM, ZOOKEEPER_ADDRS);
//2.获取连接对象
conn = ConnectionFactory.createConnection(conf);
//3.获取操作对象,操作对象是一样表 参数是操作的那张表
Table table = conn.getTable(TableName.valueOf("ns2:t2"));
return table;
}
/**
* 打印数据
* @param results 数据集
*/
public static void printRS(ResultScanner results) {
//取出每一条数据
for(Result rs : results) {
for(Cell cell : rs.listCells()) {
System.out.print("rowkey:"+new String(CellUtil.cloneRow(cell)));
System.out.print("\t"+"列族:"+new String(CellUtil.cloneFamily(cell)));
System.out.print("\t"+"列:"+new String(CellUtil.cloneQualifier(cell)));
System.out.print("\t"+"值:"+new String(CellUtil.cloneValue(cell)));
System.out.println("\t"+"时间戳:"+ cell.getTimestamp());
}
System.out.println();
}
}
/**
* 释放table资源
* @param table
* @throws IOException
*/
public static void close(Table table) throws IOException {
//进行判断也即说admin被创建之后才能释放其对象
if(table!=null) {
table.close();
conn.close();
}
}
/**
* 释放admin资源
* @param admin
* @throws IOException
*/
public static void close(Admin admin) throws IOException {
//进行判断也即说admin被创建之后才能释放其对象
if(admin!=null) {
admin.close();
conn.close();
}
}
}
Table table = HBaseUtil.getTable();
//age>=50 and sex =1
//过滤连创建
FilterList fls = new FilterList(Operator.MUST_PASS_ALL);
//过滤对象(单个)
SingleColumnValueFilter scvf1 = new
SingleColumnValueFilter("info".getBytes(), "age".getBytes(), CompareOp.GREATER_OR_EQUAL, "50".getBytes());
SingleColumnValueFilter scvf2 = new
SingleColumnValueFilter("info".getBytes(), "sex".getBytes(), CompareOp.EQUAL, "1".getBytes());
//将单个过滤器对象天际到过滤连中
fls.addFilter(scvf1);
fls.addFilter(scvf2);
//创建扫描器对象
Scan scan = new Scan();
scan.setFilter(fls);
//获取扫描结果
ResultScanner rs = table.getScanner(scan);
//打印结果
HBaseUtil.printRS(rs);
HBaseUtil.close(table);
//获取table对象
Table table = HBaseUtil.getTable();
//分页过滤器(参数是页数)
PageFilter pf = new PageFilter(3);
//构建扫描器
Scan scan = new Scan();
//添加过滤到过滤器中
scan.setFilter(pf);
//定义 一个变量来记录rowkey的值,因为rowkey是分页的依据
String maxkey="";
//循环获取数据
while(true) {
//需要定义一个循环停止的条件
int count = 0;
//万能的(最小rowkey值)
scan.setStartRow(Bytes.toBytes(maxkey+"\001"));
//获取数据
//因为scan已经限制了一次只能取3行数据
ResultScanner results = table.getScanner(scan);
//获取迭代器对象
Iterator it = results.iterator();
while(it.hasNext()) {
//取出具体的值
Result result = it.next();
//统计计数
count += 1;
//更改rewkey 位置
maxkey = Bytes.toString(result.getRow());
//进行打印
for(Cell cell : result.listCells()) {
System.out.print("rowkey:"+new String(CellUtil.cloneRow(cell)));
System.out.print("\t"+"列族:"+new String(CellUtil.cloneFamily(cell)));
System.out.print("\t"+"列:"+new String(CellUtil.cloneQualifier(cell)));
System.out.print("\t"+"值:"+new String(CellUtil.cloneValue(cell)));
System.out.println("\t"+"时间戳:"+ cell.getTimestamp());
}
}
//不足3条停止循环
if(count < 3) {
break;
}
System.out.println("-------------------------------------------------------------");
}
//先获取table对象
Table table = HBaseUtil.getTable();
//获取行过滤器对象(只能和rowKey的值进行比较)
//SubstringComparator 字符串截取过滤器 --> 判断当前数据中是否存在子串(过滤器参数)
//要过滤数据中含有当前字符串成功
//RegexStringComparator 正则过滤器 参数也是字符串 只要符合提供的正则表达式既可以过滤
//若是字符串比较最好使用的比较符号就是等于和不等于
RowFilter rf = new RowFilter(CompareOp.EQUAL, new SubstringComparator("1"));
//直接判断RowKey前缀的
PrefixFilter pf = new PrefixFilter(Bytes.toBytes("u0"));
//获取扫描器对象
Scan scan = new Scan();
//将过滤器添加到scan中(添加过滤条件)
scan.setFilter(rf);
//获取结果集
ResultScanner rs = table.getScanner(scan);
//输出结果
HBaseUtil.printRS(rs);
//释放对象
HBaseUtil.close(table);
//table对象
Table table = HBaseUtil.getTable();
//获取行过滤器对象,每行只获取第一个字段
FirstKeyOnlyFilter fkof = new FirstKeyOnlyFilter();
//获取扫描器对象
Scan scan = new Scan();
//将过滤器添加到scan中
scan.setFilter(fkof);
//在遍历的时候统计行的个数
ResultScanner rs = table.getScanner(scan);
//一个统计计数
int count = 0;
//取出每一条数据
for(Result r : rs) {
for(Cell cell : r.listCells()) {
System.out.print("rowkey:"+new String(CellUtil.cloneRow(cell)));
System.out.print("\t"+"列族:"+new String(CellUtil.cloneFamily(cell)));
System.out.print("\t"+"列:"+new String(CellUtil.cloneQualifier(cell)));
System.out.print("\t"+"值:"+new String(CellUtil.cloneValue(cell)));
System.out.println("\t"+"时间戳:"+ cell.getTimestamp());
}
count++;
System.out.println();
}
System.out.println("总行数:"+count);
HBaseUtil.close(table);
System.out.println("finished");
Table table = HBaseUtil.getTable();
//2.创建一个列族过滤器
//列族只有一个info,最好的判断条件就是等于和不等于
//参数是1是 运算符 参数2是一个ByteArrayComparable它是一个抽象类不能new
//所以后面的参数我们可以使用四大比较器来添加
FamilyFilter ff = new FamilyFilter(CompareOp.EQUAL, new BinaryComparator("info".getBytes()));
//3获取扫描对象
Scan scan = new Scan();
//将当前过滤方式添加到扫描器中
scan.setFilter(ff);
//获取扫描数据
ResultScanner rs = table.getScanner(scan);
//打印结果
HBaseUtil.printRS(rs);
//释放对象
HBaseUtil.close(table);
System.out.println("finished");
//1.直接表的数据 -
Table table = HBaseUtil.getTable();
//age>=50 and sex = 1
//过滤链创建
/** !AND MUST_PASS_ALL*/
/** !OR MUST_PASS_ONE */
FilterList fls = new FilterList(Operator.MUST_PASS_ALL);
//过滤对象(单个)
//在不指定过滤器的前提之下过滤方式ByteArrayComparable 全值过滤
//age>=50下面完成就是这句话
//第三个参数可以传的
/** less than LESS 小于*/
/** less than or equal to LESS_OR_EQUAL 小于等于*/
/** equals EQUAL 等于*/
/** not equal NOT_EQUAL 不等于*/
/** greater than or equal to GREATER_OR_EQUAL 大于等于*/
/** greater than GREATER 大于*/
/** no operation NO_OP 不使用运算符 */
SingleColumnValueFilter scvf1 =
new SingleColumnValueFilter("info".getBytes(), "age".getBytes(), CompareOp.GREATER_OR_EQUAL, "50".getBytes());
SingleColumnValueFilter scvf2 =
new SingleColumnValueFilter("info".getBytes(), "sex".getBytes(), CompareOp.EQUAL, "1".getBytes());
//将单个过滤对象添加到过滤链中
fls.addFilter(scvf1);
fls.addFilter(scvf2);
//创建扫描器对象
Scan scan = new Scan();
//2将过滤器添加到scan中
scan.setFilter(fls);
//获取扫描结果
ResultScanner rs = table.getScanner(scan);
//打印结果
HBaseUtil.printRS(rs);
//释放对象
HBaseUtil.close(table);
System.out.println("finished");