从hbase表1中读取数据,最终结果写入到hbase表2 ,如何通过MapReduce实现 ?
来源:互联网 发布:windows运行lua脚本 编辑:程序博客网 时间:2024/05/16 07:17
需要一:
将hbase中‘student’表中的info:name和info:age两列数据取出并写入到hbase中‘user’表中的basic:XM和basic:NL
class ReadStudentMapper extends TableMapper
package hbaseapi.hbase;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.HBaseConfiguration;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.NullWritable;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper.Context;import org.apache.hadoop.util.Tool;import org.apache.hadoop.util.ToolRunner;public class Student2UserMapReduce extends Configured implements Tool { // Step 1 : Mapper public static class ReadStudentMapper extends TableMapper<ImmutableBytesWritable, Put> { @Override protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException { // create put , Put put = new Put(key.get()); // add cell/data to put for (Cell cell : value.rawCells()) { // get info family if ("info".equals(Bytes.toString(CellUtil.cloneFamily(cell)))) { // add name to put if ("name".equals(Bytes.toString(CellUtil .cloneQualifier(cell)))) { put.add(Bytes.toBytes("info"), Bytes.toBytes("XM"), CellUtil.cloneValue(cell)); } else if ("age".equals(Bytes.toString(CellUtil .cloneQualifier(cell)))) { put.add(Bytes.toBytes("info"), Bytes.toBytes("NL"), CellUtil.cloneValue(cell)); } } } // context output context.write(key, put); } } // Step 2 : Reducer public static class WriteUserReducer extends TableReducer<ImmutableBytesWritable, Put, NullWritable> { @Override protected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context) throws IOException, InterruptedException { for (Put put : values) { context.write(NullWritable.get(), put); } } } // Step 3 : Driver public int run(String[] args) throws Exception { // 1) Configuration Configuration conf = this.getConf(); // 2) create job Job job = Job.getInstance(conf, this.getClass().getSimpleName()); job.setJarByClass(Student2UserMapReduce.class); // 3) set job // set scan 设置一个查询范围或条件 Scan scan = new Scan(); // 设置只扫描某些列或列簇 scan.addFamily(Bytes.toBytes("info")); // set Mapper TableMapReduceUtil.initTableMapperJob("student", scan, ReadStudentMapper.class, ImmutableBytesWritable.class, Put.class, job); // set Reducer TableMapReduceUtil.initTableReducerJob( "user", WriteUserReducer.class, job); //set reduce nums job.setNumReduceTasks(1); //at least one ,adjust as required!! boolean isSuccess = job.waitForCompletion(true); if (!isSuccess) { throw new IOException("error with job!"); } return isSuccess ? 0 : 1; } public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); int status = ToolRunner.run(// conf, // new Student2UserMapReduce(), // args // ); System.exit(status); }}
阅读全文
0 0
- 从hbase表1中读取数据,最终结果写入到hbase表2 ,如何通过MapReduce实现 ?
- MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中?
- MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中?
- MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中?
- MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中?
- HBase建表高级属性,hbase应用案例看行键设计,HBase和mapreduce结合,从Hbase中读取数据、分析,写入hdfs,从hdfs中读取数据写入Hbase,协处理器和二级索引
- Hbase通过 Mapreduce 写入数据到Mysql
- mapreduce读取hbase值,结果输出到多个hbase表中
- Mapreduce读取和写入Hbase(从A表读取数据,统计结果放入B表,非常详细,附有代码说明以及流程)
- hadoop MR从hbase中读取数据写入到hbase中
- 【HBase基础教程】6、HBase之读取MapReduce数据写入HBase
- MapReduce读写结构化文件数据写入HBase表中
- hbase 下mapreduce 读取hbase中数据
- 通过MapReduce把Hive表数据导入到HBase
- mapreduce读取hbase中表的数据,直接打印或者回传数据到hbase数据库表
- 通过MapReduce程序实现对hbase表数据的插入
- mapreduce读取hbase内容,结果放到hbase中
- mapreduce读取hbase数据输出到文件中
- [BZOJ2326][HNOI2011]数学作业-矩阵乘法
- 要学算法,排序应该是第一站吧 —— 比较排序
- HDU-2017 ACM/ICPC Asia Regional Qingdao Online-1009-Smallest Minimum Cut
- 基于Dubbo框架构建分布式服务-容错分析
- Android解析WindowManager(三)Window的添加过程
- 从hbase表1中读取数据,最终结果写入到hbase表2 ,如何通过MapReduce实现 ?
- 处理struts2框架国际化资源文件问题(使用native2ascii工具)
- 数据库中的范式
- hbase与hive集成
- MyBatis学习之获取主键策略
- 软考助手V3.5发布喽
- Jquery+ajax+springMVC返回json的运用方法
- hbase与sqoop集成
- 访问权限控制