hbase如何编写mapreduce
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,这篇文章主要为大家展示了"hbase如何编写mapreduce",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"hbase如何编写mapreduce"这篇文章
千家信息网最后更新 2025年02月04日hbase如何编写mapreduce
这篇文章主要为大家展示了"hbase如何编写mapreduce",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"hbase如何编写mapreduce"这篇文章吧。
package com.hbase.test;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.client.Mutation;import org.apache.hadoop.hbase.client.Put;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.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.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;public class HbaseMrTest { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = HBaseConfiguration.create(); //配置conf conf.set("hbase.zookeeper.quorum", "bigdata01,bigdata02,bigdata03"); conf.set("hbase.zookeeper.property.clientPort", "2181"); Job job = Job.getInstance(conf, "word-count"); //指定执行job的主类 job.setJarByClass(HbaseMrTest.class); Scan scan = new Scan(); //定义mapper需要扫描的列 scan.addColumn(Bytes.toBytes("content"), Bytes.toBytes("words")); //配置mapper TableMapReduceUtil.initTableMapperJob("wordcount", scan,HMapper.class , Text.class, IntWritable.class, job); //配置recuder TableMapReduceUtil.initTableReducerJob("result", HReducer.class, job); //提交job System.exit(job.waitForCompletion(true)?0:1); }}// Text, IntWritable 为输出类型class HMapper extends TableMapper{ Text out = new Text(); IntWritable iw = new IntWritable(1); @Override protected void map(ImmutableBytesWritable key, Result value, Mapper .Context context) throws IOException, InterruptedException { //通过result 直接过得content:words 的值 byte[] bytes = value.getValue(Bytes.toBytes("content"), Bytes.toBytes("words")); if(bytes!=null) { String words = Bytes.toString(bytes); //对获得的一行单词进行分割 String[] ws = words.split(" "); for(String wd : ws) { out.set(wd); //写出值,如: you 1 context.write(out, iw); } } } }// Text, IntWritable 为mapper的输出类型class HReducer extends TableReducer { @Override protected void reduce(Text text, Iterable iter, Reducer .Context context) throws IOException, InterruptedException { int sum = 0 ; //对iter遍历 for(IntWritable intw : iter) { sum+= intw.get(); } //new 一个put 构造函数内的值为row key Put put = new Put(Bytes.toBytes(text.toString())); //put添加columnfamily 和column put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("wordcnt"), Bytes.toBytes(String.valueOf(sum))); //将每个单词当做row key 写出,put是相加的总和 context.write(new ImmutableBytesWritable(Bytes.toBytes(text.toString())), put); } }最后将java文件export为RaunableJar放到linux java -jar hbase.jar com.hbase.test.HbaseMrTest 运行
原始数据:
运行结果:
以上是"hbase如何编写mapreduce"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
内容
篇文章
配置
单词
类型
学习
帮助
输出
运行
原始
一行
函数
总和
数据
文件
易懂
更多
条理
知识
结果
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
全局位置指纹数据库建立
网络安全简介怎么写
软件开发工程师实训所需设备
存储不同类型的数据库
最牛的网络安全团队
瀚高 数据库
DNS服务器发生错误是什么意思
计算机网络技术的春考录取学校
北京楼控管理软件开发定制
record数据库的应用
手机天气预报软件开发
软件开发后台维护工资
网络安全问题及对策的阐述
青少年挑战网络安全大考验
数据库技术与应用实验收获
如何理解软件开发的本质是建模
网络安全的具体地点指
数据库关系代数中选择行怎么选
网络安全项目经历写什么
沈阳辽邦网络技术科技有限公司
二代网络技术
研发部网络安全
介休网络技术团队
数据库怎么算容量
高职单招计算机网络技术专业
电脑哪里登录服务器
网络安全主要采用什么技术
联想sr服务器更新微码
女孩适合软件开发吗
医学专用软件开发有关知识