大数据笔记(十四)——HBase的过滤器与Mapreduce
一. HBase过滤器
1、列值过滤器
2、列名前缀过滤器
3、多个列名前缀过滤器
4、行键过滤器
5、组合过滤器
package demo; import javax.swing.RowFilter; import org.apache.hadoop.conf.Configuration;
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.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test; import net.spy.memcached.ops.OperationErrorType; public class TestHBaseFilter { /**
* 列值过滤器:SingleColumnValueFilter
*/
@Test
public void testSingleColumnValueFilter() throws Exception{
//查询工资等于3000的员工
//select * from emp where sal=3000
//配置ZK的地址信息
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.153.11"); //得到HTable客户端
HTable client = new HTable(conf, "emp");
//定义一个列值过滤器
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("empinfo"),//列族
Bytes.toBytes("sal"), //工资
CompareOp.EQUAL, // =
Bytes.toBytes("3000"));//ֵ //定义一个扫描器
Scan scan = new Scan();
scan.setFilter(filter); //通过过滤器查询数据
ResultScanner rs = client.getScanner(scan);
for (Result result : rs) {
String name = Bytes.toString(result.getValue(Bytes.toBytes("empinfo"), Bytes.toBytes("ename")));
System.out.println(name);
} client.close();
} /**
* 列名前缀过滤器:ColumnPrefixFilter
*/
@Test
public void testColumnPrefixFilter() throws Exception{
//列名前缀过滤器
//select ename from emp
//配置ZK的地址信息
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.153.11"); //得到HTable客户端
HTable client = new HTable(conf, "emp"); //定义一个列名前缀过滤器
ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("ename")); //定义一个扫描器
Scan scan = new Scan();
scan.setFilter(filter); //通过过滤器查询数据
ResultScanner rs = client.getScanner(scan);
for (Result result : rs) {
String name = Bytes.toString(result.getValue(Bytes.toBytes("empinfo"), Bytes.toBytes("ename")));
System.out.println(name);
} client.close();
} /**
* 多个列名前缀过滤器:MultipleColumnPrefixFilter
*/
@Test
public void testMultipleColumnPrefixFilter() throws Exception{ Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.153.11"); HTable client = new HTable(conf, "emp");
//员工姓名 薪资
byte[][] names = {Bytes.toBytes("ename"),Bytes.toBytes("sal")}; MultipleColumnPrefixFilter filter = new MultipleColumnPrefixFilter(names); Scan scan = new Scan();
scan.setFilter(filter); ResultScanner rs = client.getScanner(scan);
for (Result result : rs) {
String name = Bytes.toString(result.getValue(Bytes.toBytes("empinfo"), Bytes.toBytes("ename")));
String sal = Bytes.toString(result.getValue(Bytes.toBytes("empinfo"), Bytes.toBytes("sal")));
System.out.println(name+"\t"+sal);
} client.close();
} /**
* 行键过滤器:RowFilter
*/
@Test
public void testRowFilter() throws Exception{ Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.153.11"); HTable client = new HTable(conf, "emp"); //定义一个行键过滤器
org.apache.hadoop.hbase.filter.RowFilter filter = new org.apache.hadoop.hbase.filter.RowFilter(
CompareOp.EQUAL, //=
new RegexStringComparator("7839")); //定义一个扫描器
Scan scan = new Scan();
scan.setFilter(filter); //通过过滤器查询数据
ResultScanner rs = client.getScanner(scan);
for (Result result : rs) {
String name = Bytes.toString(result.getValue(Bytes.toBytes("empinfo"), Bytes.toBytes("ename")));
String sal = Bytes.toString(result.getValue(Bytes.toBytes("empinfo"), Bytes.toBytes("sal")));
System.out.println(name+"\t"+sal);
} client.close();
} /**
* 组合过滤器
*/
@Test
public void testFilter() throws Exception{ Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.153.11"); HTable client = new HTable(conf, "emp"); //工资=3000
SingleColumnValueFilter filter1 = new SingleColumnValueFilter(Bytes.toBytes("empinfo"),
Bytes.toBytes("sal"), CompareOp.EQUAL, Bytes.toBytes("3000"));
//名字
ColumnPrefixFilter filter2 = new ColumnPrefixFilter(Bytes.toBytes("ename")); FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);
filterList.addFilter(filter1);
filterList.addFilter(filter2); Scan scan = new Scan();
scan.setFilter(filterList); ResultScanner rs = client.getScanner(scan);
for (Result result : rs) {
String name = Bytes.toString(result.getValue(Bytes.toBytes("empinfo"), Bytes.toBytes("ename")));
String sal = Bytes.toString(result.getValue(Bytes.toBytes("empinfo"), Bytes.toBytes("sal")));
System.out.println(name+"\t"+sal);
} client.close();
}
}
二. HDFS上的mapreduce
建立表
create 'word','content'
put 'word','1','content:info','I love Beijing'
put 'word','2','content:info','I love China'
put 'word','3','content:info','Beijing is the capital of China'
create 'stat','content'
注意:export HADOOP_CLASSPATH=$HBASE_HOME/lib/*:$CLASSPATH

WordCountMapper.java
package wc; import java.io.IOException; import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text; //K2 V2
//没有k1和v1,因为输入的就是表中一条记录
public class WordCountMapper extends TableMapper<Text, IntWritable>{ @Override
protected void map(ImmutableBytesWritable key, Result value,
Context context)throws IOException, InterruptedException {
//key和value代表从表中输入的一条记录
//key:行键 value:数据
String data = Bytes.toString(value.getValue(Bytes.toBytes("content"), Bytes.toBytes("info")));
//分词
String[] words = data.split(" ");
for (String w : words) {
context.write(new Text(w), new IntWritable(1));
}
}
}
WordCountReducer.java
package wc; import java.io.IOException; import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
//k3 v3 代表输出一条记录
public class WordCountReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable>{ @Override
protected void reduce(Text k3, Iterable<IntWritable> v3,Context context)
throws IOException, InterruptedException {
// 求和
int total = 0;
for (IntWritable v : v3) {
total = total + v.get();
} //构造一个put对象
Put put = new Put(Bytes.toBytes(k3.toString()));
put.add(Bytes.toBytes("content"),//列族
Bytes.toBytes("result"),//列
Bytes.toBytes(String.valueOf(total))); //输出
context.write(new ImmutableBytesWritable(Bytes.toBytes(k3.toString())), //把这个单词作为key,就是输出的行键
put);//表中的一条记录
} }
WordCountMain.java
package wc; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job; public class WordCountMain { public static void main(String[] args) throws Exception {
//获取ZK的地址
//指定的配置信息:Zookeeper
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.153.11"); //创建一个任务
Job job = Job.getInstance(conf);
job.setJarByClass(WordCountMain.class); //定义一个扫描器读取:content:info
Scan scan = new Scan();
//可以使用filter
scan.addColumn(Bytes.toBytes("content"), Bytes.toBytes("info")); //使用工具类设置Mapper
TableMapReduceUtil.initTableMapperJob(
Bytes.toBytes("word"), //输入的表
scan, //扫描器,只读取需要处理的数据
WordCountMapper.class,
Text.class, //key
IntWritable.class,//value
job); //使用工具类Reducer
TableMapReduceUtil.initTableReducerJob("stat", WordCountReducer.class, job); job.waitForCompletion(true);
} }
结果:

大数据笔记(十四)——HBase的过滤器与Mapreduce的更多相关文章
- 大数据笔记(四)——操作HDFS
一.Web Console:端口50070 二.HDFS的命令行操作 (一)普通操作命令 HDFS 操作命令帮助信息: hdfs dfs + Enter键 常见命令 1. -mkdir 在HDFS上 ...
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- 《C++游戏开发》笔记十四 平滑过渡的战争迷雾(二) 实现:真正的迷雾来了
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9712321 作者:七十一雾央 新浪微博:http:/ ...
- 跟上节奏 大数据时代十大必备IT技能
跟上节奏 大数据时代十大必备IT技能 新的想法诞生新的技术,从而造出许多新词,云计算.大数据.BYOD.社交媒体……在互联网时代,各种新词层出不穷,让人应接不暇.这些新的技术,这些新兴应用和对应的IT ...
- CentOS6安装各种大数据软件 第四章:Hadoop分布式集群配置
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- 大数据学习系列之—HBASE
hadoop生态系统 zookeeper负责协调 hbase必须依赖zookeeper flume 日志工具 sqoop 负责 hdfs dbms 数据转换 数据到关系型数据库转换 大数据学习群119 ...
- 大数据笔记(十三)——常见的NoSQL数据库之HBase数据库(A)
一.HBase的表结构和体系结构 1.HBase的表结构 把所有的数据存到一张表中.通过牺牲表空间,换取良好的性能. HBase的列以列族的形式存在.每一个列族包括若干列 2.HBase的体系结构 主 ...
- 跟上节奏 大数据时代十大必备IT技能(转)
新的想法诞生新的技术,从而造出许多新词,云计算.大数据.BYOD.社交媒体……在互联网时代,各种新词层出不穷,让人应接不暇.这些新的技术,这些新兴应用和对应的IT发展趋势,使得IT人必须了解甚至掌握最 ...
- 大数据时代数据库-云HBase架构&生态&实践
业务的挑战 存储量量/并发计算增大 现如今大量的中小型公司并没有大规模的数据,如果一家公司的数据量超过100T,且能通过数据产生新的价值,基本可以说是大数据公司了 .起初,一个创业公司的基本思路就是首 ...
- 大数据核心知识点:Hbase、Spark、Hive、MapReduce概念理解,特点及机制
今天,上海尚学堂大数据培训班毕业的一位学生去参加易普软件公司面试,应聘的职位是大数据开发.面试官问了他10个问题,主要集中在Hbase.Spark.Hive和MapReduce上,基础概念.特点.应用 ...
随机推荐
- 100+ Python挑战性编程练习(2)
熟能生巧,多撸代码多读书 https://github.com/zhiwehu/Python-programming-exercises/blob/master/100+%20Python%20cha ...
- Keil共存的方法 - Keil MDK兼容Keil C51,实操可行
记录一下成功使Keil MDK和Keil C51共存的过程! 之前一直用Keil C51开发,最近需要用到ARM9内核的IC,就需要Keil C51和Keil MDK共存.看了一下网上几个教程,方法大 ...
- Linux常用命令基础
linux 常用指令 基础命令 宿主目录 目录结构 文件管理 目录管理 用户管理 别名管理 压缩包管理 网络设置 shell技巧 帮助方法 /表示根目录 ~表示家目录 软件的安装(光盘中的软件呢): ...
- SpringBoot、ActiveMQ整合阿里大鱼-----短信服务
3.短信微服务 3.1需求分析 构建一个通用的短信发送服务(独立于优乐选的单独工程),接收activeMQ的消息(MAP类型) 消息包括手机号(mobile).短信模板号(template_code ...
- mysql之sql性能调优
sql调优大致分为两步:1 如何定位慢查询 2 如何优化sql语句. 一:定位慢查询 -- 显示到mysql数据库的连接数 -- show status like 'connections'; - ...
- 计算机系统结构总结_Scoreboard and Tomasulo
Textbook:<计算机组成与设计——硬件/软件接口> HI<计算机体系结构——量化研究方法> QR 超标量 前面讲过超标量的概念.超标量的目的就是实现指 ...
- sqlserver 创建 aspstate的方法
找到路径 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 不同版本找不同版本的路径 在命令行执行命令 aspnet_regsql.exe -ssadd -s ...
- redis基础及redis特殊场景使用描述
数据类型 String set list hash zset redis原理 单线程:redis是单线程+io多路复用:检查文件描述的就绪状态 对比memchached:多线程+锁 redis优势 解 ...
- Java常用日期处理方法
import org.apache.commons.lang3.time.FastDateFormat; import org.joda.time.DateTime; import org.apach ...
- MyBatis中批量insert
在orcale和mybatis执行批量插入是不一样的. orcale如下:(这里要注意的是:useGeneratedKeys="false" ) 方式1:oracle批量插入使用 ...