从hbase表1中读取数据,最终结果写入到hbase表2 ,如何通过MapReduce实现 ?
2017-09-19 00:39
627 查看
需要一:
将hbase中‘student’表中的info:name和info:age两列数据取出并写入到hbase中‘user’表中的basic:XM和basic:NL
class ReadStudentMapper extends TableMapper
将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); } }
相关文章推荐
- MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中?
- MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中?
- MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中?
- MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中?
- mapreduce实现从hbase中统计数据,结果存入mysql中
- MapReduce编程之通过MapReduce读取数据,往Hbase中写数据
- HBase建表高级属性,hbase应用案例看行键设计,HBase和mapreduce结合,从Hbase中读取数据、分析,写入hdfs,从hdfs中读取数据写入Hbase,协处理器和二级索引
- 简单通过java的socket&serversocket以及多线程技术实现多客户端的数据的传输,并将数据写入hbase中
- 通过mapreduce程序读取hdfs文件写入hbase
- Hbase通过 Mapreduce 写入数据到Mysql
- 【干货】如何通过OPC自定义接口来实现客户端数据的读取?
- MapReduce中,从HDFS读取数据计算后写入HBase
- Mapreduce读取和写入Hbase(从A表读取数据,统计结果放入B表,非常详细,附有代码说明以及流程)
- 【HBase基础教程】6、HBase之读取MapReduce数据写入HBase
- 如何通过JDBC向数据库写入/读取大数据文件?
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- MapReduce功能实现四---小综合(从hbase中读取数据统计并在hdfs中降序输出Top 3)
- 如何通过SerialPort读取和写入设备COM端口数据
- 通过ajax来读取xml里面的数据,以及查询好数据如何写入xml里去 来供ajax读取
- mapreduce中实现对hbase中表数据的添加