JAVA API
HBase数据存储管理相关的内容,其涉及的主要类包括:HBaseAdmin,HBaseConfiguration,HTable,HTableDescriptor,HColumnDescriptor,Put,Get和Scanner.
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
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.TableName;
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.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseDemo{
HBaseAdmin admin;
Configuration conf;
HTable table;
}
// 初始化对象
public HBaseDemo() throws Exception
{
conf = HBaseConfiguration.create(); // 获取配置文件对象
admin = new HBaseAdmin(conf); // 通过配置文件对象或许操作数据库对象。用来创建表,删除表等。
table = new HTable(conf,"HBaseDemo"); // 此对象用来与HBase进行通信,指定配置文件即表名称
}
// 创建一个表
public void createTable() throws Exception
{
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("HBaseDemo"));//表对象,用于在表中添加列族
HColumnDescriptor family = new HColumnDescriptor("info");//定义列族对象,并指定列族名称
desc.addFaminl(family); //将列族添加到表中
admin.createTable(desc);//创建表。
}
// 删除一张表
public void deleteTable() throws Exception
{
String tablename="HBaseDemo";
// tableExists 健壮性判断,删除前先判断表是否存在
if(admin.tableExists(tablename){
if(admin.isTableEnabled(tablename) //删除表要先禁用表,因此判断表是否已经被禁用
} {
admin.disableTable(tablename); // 满足条件则表还未被禁用
}
admin.deleteTable(tablename);
}
// 在一个表中添加一行数据
public void putDemo() throws Exception
{
//创建put对象,在表中插入一行数据 因为hbase存储的都是二进制,因此在API中字符串一般都要转换成字节。
Put put = new put(Bytes.toBytes("rk001"); // 指定rowkey
// put的add 的方法三个参数 分别为(列族,字段名称,值)。
put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("lisi"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(23));
// 执行put语句
table.put(put);
}
// 通过list集合添加多行数据
public void putlistDemo() throws Exception
{
List
list = new ArrayList<>();//定义一个集合用来存储put语句
Put put1 = new Put(Bytes.toBytes("rk002"));
put1.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
Put put2 = new Put(Bytes.toBytes("rk003"));
put2.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("wangwu"));
list.add(put1); //在集合中添加put语句
list.add(put2);
table.put(list); //把list集合当做参数穿进去
}
// 查看表中一行数据
public void getDemo() throws Exception
{
Get get = new Get(Bytes.toBytes("rk001")); // 创建get对象指定rowkey来获取value
get.addColume(Bytes.toBytes("info"),Bytes.toBytes("age"));//指定获取的列族和字段
Result re = table.get(get); // 获取值
System.out.println(Bytes.toInt(re.value()));
}
// 扫描获取数据
public void scanDemo() throws Exception{
Scan scan = new Scan(); // 定义扫描对象
scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name")); //指定获取的列族和字段
Result result = table.getScanner(scan); // 执行scan语句
// 获取result中的值
Iterator
it = result.iterator();
/*while(it.hasNext())
{
System.out.println(Bytes.toStrig((it.next().value())));
}*/
for(Result re:result)
{
System.out.println(Bytes.toString(re.value()));
}
}
public static void main(String[] args) throws Exception{
HBaseDemo deom = new HBaseDemo();
// demo.createDemo();
// demo.putDemo()
// demo.putlistDemo();
// demo.getDemo();
// dmeo.scanDemo();
}
package p3.gyg.hbase.sougou;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
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.util.Bytes;
public class HBasePutDemo{
File file = new File("/home/gyg/a.txt");//定义本地文件对象
HTable table;
Configuration conf = HBaseConfiguration.create();
public void readdata() throws Exception
{
BufferedReader buf = new BufferedReader(new FileReader(file));//创建读取流
String line;
HTable table = new HTable(conf,"sougou_2");
int count = 0;
while((line=buf.readLine())!=null)
{
String words = buf.readLine();
String []filds = StringUtils.split(words);
if(filds.length !=6) continue; // 进行健壮性判断。
String time = filds[0];
String uuid = filds[1];
String keyword = filds[2];
String rand = filds[3];
String click = filds[4];
String url = filds[5];
//将要存储的字段当做参数进行传入,调用方法insert
Put put = insert(time,uuid,keyword,rand,click,url);
// 以上语句返回一个put对象。
table.put(put);
System.out.println(count++);// 为了判断执行情况,定义一个计数器
}
}
/**
* 用来定义put语句
*/
public Put insert(String time,String uuid,String keyword,String rand,String click,String url)
{
Put put = new Put(Bytes.toBytes(uuid)); // uuid作为行键
put.add(Bytes.toBytes("info"),Bytes.toBytes("time"),Bytes.toBytes(time)); //添加字段
put.add(Bytes.toBytes("info"),Bytes.toBytes("keyword"),Bytes.toBytes(keyword));
put.add(Bytes.toBytes("info"),Bytes.toBytes("rand"),Bytes.toBytes(rand));
put.add(Bytes.toBytes("info"),Bytes.toBytes("click"),Bytes.toBytes(click));
put.add(Bytes.toBytes("info"),Bytes.toBytes("url"),Bytes.toBytes(url));
return put;
}
/**
* 处理业务 ----------get() 根据行键 也就是uuid来查找数据,并获取两个字段的值
*/
public void getdata() throws Exception
{
List
list = new ArrayList<>(); //定义集合用来存储Get对象
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf,"sougou_2");
// 第一种方法 采用集合 封装两个get语句
Get get1 = new Get(Bytes.toBytes("a97641a564206e4920a508063134c655"));
get1.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("url"));
Get get2 = new Get(Bytes.toBytes("a97641a564206e4920a508063134c655"));
get2.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("keyword"));
list.add(get1);
list.add(get2);
Result[] res=table.get(list); // 执行结果返回一个结果集
for(Result re:res) // 遍历结果集取出结果
{
// re.value()只能获取一行结果的一个字段的值,如果多个字段,则会只去一个demo value 输出
System.out.println(Bytes.toString(re.value()));
}
// 第二种方法 获取整个结果集 通过对字段的名称来判断要输出的value
Get get3 = new Get(Bytes.toBytes("a97641a564206e4920a508063134c655"));
get3.addColumn(Bytes.toBytes("cf1"));//get语句只指定列族,不指定字段
Result[] result = table.get(get3);
for(Result res:result){
for(KeyValue kv:res.ras()){ // 返回的结果中是多个kv对 因此还要遍历这些kv对
if(kv.getQualifier().equals("url"); //kv.getQualifier() 获取字段名称
System.out.println(Bytes.toString(kv.getvalue());
if(kv.getQualifier().equals("keyword"));
System.out.println(Bytes.toString(kv.getvalue());
}
}
}
/**
* 处理业务 ----------获取uuid的数量,以为hbase存储的时候rowkey为uuid因此存储的时候会自动去重
----------但是相同的字段会进行覆盖因此建表的时候一般需要指定版本
*/
public void getUuid() throws Exception{
HTable table = new HTable(conf,"sougou_2");
Scan scan = new Scan();
int count=0; // 定义计数器 来输出行数
ResultScanner result=table.getScanner(scan);
for(Result re: result)
{
count++;
}
System.out.println(count);
}
/**
* 处理业务 ----------查看了搜索了“仙剑奇侠传”关键字的用户的uuid
*/
public void getxianjian() throws Exception{
HTable table = new HTable(conf,"sougou_2");
Scan scan = new Scan();
int count = 0;
scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("keyword");//定义要查询的列族和字段
ResultScanner res = table.gerScanner(scan);
for(Result result:res)
{
String keyword = Bytes.toString(result.value());
if(keyword.contains("仙剑奇侠传"))
{
count++
}
System.
}
public static void main(String[] args) throws Exception {
SougouPut put = new SougouPut();
put.readdata();
// put.getuuid();
// put.getdata();
// put.xianjian();
}
}
}
HBase 与 Mapreduce
在伪分布式模式下和完全分布式模式下,HBase是架构在HDFS之上的。因此完全可以将MapReduce变成框架和HBase结合起来使用。也就是说,将HBase作为底层“存储结构”,Mapreduce调用HBase进行特殊处理,这样就能够结合HBase分布式大型数据库和MapReduce并行计算的优点。
package p4.gyg.hbase.mapreduce;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
/**
* 使用mapreduce 读取本地文件 然后存储到HBase中,多个map不需要reduce
* 分布式 因此较快
* @author gyg
*
*/
public class HdfsToHBase {
/**
* 继承自Map 方法,用来读取HDFS或者本地文件
* @author gyg
*
*/
public static class HbaseMap extends Mapper
{
int count=0;
@Override
protected void map(LongWritable key, Text value,
Context context)
throws IOException, InterruptedException {
//1, 获取读取的一行值
String line = value.toString();
//2, 以特定的分割符进行切割
String[] words = line.split("\t");
//3, 为各个字段赋值
if(words.length ==6){
String time = words[0];
String uuid = words[1];
String keyword = words[2];
String click = words[3];
String rand = words[4];
String url = words[5];
//4,定以put对象
Put put = new Put(Bytes.toBytes(uuid));
put.add(Bytes.toBytes("info"), Bytes.toBytes("time"), Bytes.toBytes(time));
put.add(Bytes.toBytes("info"), Bytes.toBytes("keyword"), Bytes.toBytes(keyword));
put.add(Bytes.toBytes("info"), Bytes.toBytes("click"), Bytes.toBytes(click));
put.add(Bytes.toBytes("info"), Bytes.toBytes("rand"), Bytes.toBytes(rand));
put.add(Bytes.toBytes("info"), Bytes.toBytes("url"), Bytes.toBytes(url));
//5,写出数据
context.write(new ImmutableBytesWritable(Bytes.toBytes(uuid)),put);
System.out.println(count++);
}
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// 1,设置要读取的数据的源文件地址
String inputpath = "/home/gyg/a.txt";
// 2,设置要输入的HBase的表名
String tablename = "sougou_3";
// 3. 获取配置文件信息
Configuration conf = HBaseConfiguration.create();
// 设置输出的表名
conf.set(TableOutputFormat.OUTPUT_TABLE, tablename);
// 4. 定义job 对象
Job job = Job.getInstance(conf);
// 5.指定要运行的jar包
job.setJarByClass(HdfsToHBase.class);
// 6.指定map类
job.setMapperClass(HbaseMap.class);
// 7.指定reduce的个数为零
job.setNumReduceTasks(0);
// 8.设置map输出的格式为table
job.setOutputFormatClass(TableOutputFormat.class);
// 9.设置map 输出格式
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Writable.class);
// 10.设置输入文件路径
FileInputFormat.setInputPaths(job,inputpath);
// 11. 提交作业
job.waitForCompletion(true);
}
}
package p4.gyg.hbase.mapreduce;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
/**
* HBase表中的数据作为源,分布式读取表中的数据存储到hdfs文件系统上。
* TableMapper 扩展自Mapper,所有以HBase作为输入源的类都要继承TableMapper
*/
public class HBaseToHdfs extends TableMapper
{
private Text k = new Text();
private Text v = new Text();
// 定义存储时的分隔符
public static final String SEPARATOR = "\u0001";
@Override
protected void map(ImmutableBytesWritable row, Result columns,
Context context)
throws IOException, InterruptedException {
String value = null;
String rowkey = Bytes.toString(row.get());
byte[] columnfamily = null; //定义一行中的所有列族
byte[] columnqualifiter = null; //定义一行中的所有字段
long ts = 0L; //定义时间戳
// 遍历一行中的所有列
try{
for(KeyValue kv:columns.list())
{
// 获取列族名称
columnfamily = kv.getFamily();
// 获取列修饰符名称
columnqualifiter = kv.getQualifier();
// 获取单元格中的值
String value = Bytes.toString(kv.getValue());
// 获取时间戳
ts= kv.getTimestamp();
// 为Text t v 赋值
k.set(rowkey);
v.set(value);
context.write(k,v);
}
}
catch(Exception e){}
}
public static void main(String[] args) throws ClassNotFoundException, IOException, InterruptedException {
// 设值表的名称
String name = "sougou_2";
// 设置目的输出路径
String outpath="/tmp/sougou_2";
// 设置HBase的源表
String inputpath = "sougou_2";
//1.获得HBase的配置信息
Configuration conf = HBaseConfiguration.create();
//2.获取扫描全表的对象
Scan scan = new Scan();
scan.setBatch(0);
scan.setCaching(10000);
scan.setMaxVersions();
scan.setTimeRange(System.currentTimeMillis() - 3*24*3600*1000L, System.currentTimeMillis());
//3.配置scan,添加扫描的条件,列族和列族名
scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("keyword"));
//4.设置hadoop的推测执行为fasle
conf.setBoolean("mapred.map.tasks.speculative.execution", false);
conf.setBoolean("mapred.reduce.tasks.speculative.execution", false);
//5.设置HDFS的存储HBase表中数据的路径
Path tmpIndexPath = new Path(outpath);
FileSystem fs = FileSystem.get(conf);
//判断该路径是否存在,如果存在则首先进行删除
if(fs.exists(tmpIndexPath)){
fs.delete(tmpIndexPath,true);
}
//6.创建job对象
Job job = new Job(conf,name);
//设置执行JOB的类
job.setJarByClass(HBaseToHdfs.class);
//job.setMapperClass(MemberMapper.class);
//设置TableMapper类的相关信息,即对MemberMapper类的初始化设置
//(hbase输入源对应的表, 扫描器, 负责整个计算的逻辑,输出key的类型,输出value的类型,job )
//TableMapReduceUtil.initTableMapperJob(table, scan, mapper, outputKeyClass, outputValueClass, job);
TableMapReduceUtil.initTableMapperJob(inputpath, scan, HBaseToHdfs.class, Text.class, Text.class, job);
//设置作业的Reduce任务个数为0
job.setNumReduceTasks(0);
//设置从HBase表中经过MapReduece计算后的结果以文本的格式输出
job.setOutputFormatClass(TextOutputFormat.class);
//设置作业输出结果保存到HDFS的文件路径
FileOutputFormat.setOutputPath(job, tmpIndexPath);
//开始运行作业
boolean success = job.waitForCompletion(true);
System.exit(success?0:1);
}
}
HBase中过滤器的简单使用
package p5.gyg.hbase.filter;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
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.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseFilterDemo1{
Configuration conf;
HTable table;
HBaseFilterTest1() throws IOException
{ conf = HBaseConfiguration.create();
// conf.set("hbase.master","Slave1:60000");
// conf.set("hbase.zookeeper.quorum","Master,Slave1,Slave2");
table = new HTable(conf,"sougou_2");
}
// RegexStringComparator --> 支持正则表达式的字符串的表达式
public void columnValueFilter() throws IOException
{
Scan scan = new Scan();
// scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("url"));
// 定义过滤其
RegexStringComparator comp = new RegexStringComparator(".*www.*");
SingleColumnValueFilter filter = new SingleColumnValueFilter
(Bytes.toBytes("info"),
Bytes.toBytes("url"),
CompareOp.EQUAL,comp);
// 使用过滤器
scan.setFilter(filter);
ResultScanner results = table.getScanner(scan);
for(Result res:results)
{
for(KeyValue value : res.list())
{
System.out.println(Bytes.toString(value.getQualifier()));
if("url".equals(Bytes.toString(value.getQualifier())))
{
System.out.println("行键为="+Bytes.toString(value.getRow()));
System.out.println("时间戳为="+(value.getTimestamp()));
System.out.println("关键字为="+Bytes.toString(value.getValue()));
}
}
}
}
// SubstringComparator --> 表示 包含有。。。字符串的值
public void substring() throws IOException
{
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("url"));
SubstringComparator comp = new SubstringComparator("www");
SingleColumnValueFilter filter = new SingleColumnValueFilter
(Bytes.toBytes("info"), Bytes.toBytes("keyword"), CompareOp.EQUAL,comp);
scan.setFilter(filter);
ResultScanner result = table.getScanner(scan);
for(Result res: result)
{
System.out.println(res.toString());
}
}
// BinaryPrefixComparator --> 使用Bytes.compareTo()进行匹配,但是是从最左端开始前缀匹配
// 只要目标得值从最左端开始匹配跟指定的字符串相同 就匹配出来
public void BinaryPrefixComparatorDemo() throws IOException{
Scan scan = new Scan();
// 定义比较器
BinaryPrefixComparator comp = new BinaryPrefixComparator(Bytes.toBytes("http://www.3366"));
// 定义列过滤器
SingleColumnValueFilter filter = new SingleColumnValueFilter
(Bytes.toBytes("info"),Bytes.toBytes("url"),CompareOp.EQUAL,comp);
// 使用过滤器
scan.setFilter(filter);
// 查询数据
ResultScanner result = table.getScanner(scan);
for(Result res:result)
{
for(KeyValue kv:res.raw())
{
if("keyword".equals(Bytes.toString(kv.getQualifier())))
{
System.out.println(kv.getTimestamp());
System.out.println(Bytes.toString(kv.getQualifier()));
System.out.println(Bytes.toString(kv.getValue()));
}
}
}
}
// BinaryComparator --> 使用Bytes.compareTO()比较当前值与阈值
public void BinaryComparatorDemo() throws IOException
{
Scan scan = new Scan();
BinaryComparator comp = new BinaryComparator(Bytes.toBytes("https://profiles.google.com/114630731673"));
SingleColumnValueFilter filter = new SingleColumnValueFilter
(Bytes.toBytes("info"),Bytes.toBytes("url"), CompareOp.GREATER,comp);
scan.setFilter(filter);
try{
ResultScanner result = table.getScanner(scan);
for(Result res : result)
{
for(KeyValue kv : res.raw())
{
System.out.println(Bytes.toString(kv.getValue()));
}
}
}
finally{
if(table !=null)
{
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws IOException {
HBaseFilterTest1 test1 = new HBaseFilterTest1();
// test1.columnValueFilter();
// test1.substring();
// test1.BinaryPrefixComparatorDemo();
test1.BinaryComparatorDemo();
}
}
package p5.gyg.hbase.filter;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
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.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseFilterTest2 {
Configuration conf = null;
HTable table = null;
/**
* 构造函数 并初始化 配置文件和 表对象
* @throws IOException
*/
public HBaseFilterTest2() throws IOException {
conf = HBaseConfiguration.create();
table = new HTable(conf,"family");
}
/**
* 列值过滤器
* SingleColumnValueFilter用于测试列值相等 不等 范围等情况
* SingleColumnValueExcludeFilter用于但列值的过滤,但并不查询出该列的值
*/
public void singleExcludeDemo() throws Exception
{
Scan scan = new Scan();
RegexStringComparator comp = new RegexStringComparator(".*www.*");
SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter
(Bytes.toBytes("info"),Bytes.toBytes("keyword"), CompareOp.EQUAL,comp);
scan.setFilter(filter);
ResultScanner result=table.getScanner(scan);
}
/**
* 键值元数据过滤器
* FamilyFilter 用于过滤列族,但通常会在使用SCAN过程中通过设定某些列族来实现该功能。
* 只输出指定的列族的值
* LESS:小于
* LESS_OR_EQUAL 小于等于
* NOT_EQUAL 不等于
*/
public void familyFilterDemo() throws Exception{
Scan scan = new Scan();
BinaryComparator comp = new BinaryComparator(Bytes.toBytes("cf1"));
FamilyFilter filter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, comp);
scan.setFilter(filter);
ResultScanner result = table.getScanner(scan);
for(Result res : result)
{
for(KeyValue kv : res.raw())
{
System.out.println(Bytes.toString(kv.getValue()));
}
}
}
/**
* Columnprefixfilter 用于列名前缀过滤 通过前面几个字符来选取固定字段的值
*
* @param args
* @throws Exception
*/
public void ColumnprefixfilterDemo() throws Exception{
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes("cf1")); // 指定查询的列族。
Filter filter = new ColumnPrefixFilter(Bytes.toBytes("na"));
scan.setFilter(filter);
ResultScanner result = table.getScanner(scan);
for(Result res:result)
{
for(KeyValue kv: res.raw())
{
System.out.println(Bytes.toString(kv.getValue()));
}
}
}
/**
* 行键过滤器
* @param args
* @throws Exception
*/
public void RowFilterDemo() throws Exception{
Scan scan = new Scan();
// 指定列族
scan.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("name"));
// 定义比较规则
SubstringComparator comp = new SubstringComparator("rk");
// BinaryPrefixComparator comp = new BinaryPrefixComparator(Bytes.toBytes("zhang"));
// 定义行键过滤器
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,comp);
// 使用过滤器
scan.setFilter(filter);
ResultScanner result = table.getScanner(scan);
System.out.println(result.toString());
for(Result res : result)
{
System.out.println(res.value());
}
}
public static void main(String[] args) throws Exception {
HBaseFilterTest2 test2 = new HBaseFilterTest2();
// test2.singleExcludeDemo();
// test2.familyFilterDemo();
// test2.ColumnprefixfilterDemo();
test2.RowFilterDemo();
}
}