Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MapReduce on Hbase

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

org.apache.hadoop.hbase.mapreduce

TableMapper TableReducer

A region corresponds to a map.

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.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Reducer;public class HbaseMR { public class MyMapper extends TableMapper { @Override protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException { // key stands for rowkey Text k = new Text(Bytes.toString(key.get())); Text v = new Text(Bytes.toString(value.getValue( "basicinfo".getBytes(), "age".getBytes()))); context.write(v, k); } } public class MyReducer extends TableReducer { @Override protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { Put put = new Put(Bytes.toBytes(key.toString())); for (Text value : values) { put.add(Bytes.toBytes("f1"), Bytes.toBytes(value.toString()), Bytes.toBytes(value.toString())); } context.write(null, put); } } public static void main(String[] args) { Configuration conf= HBaseConfiguration.create(); try { Job job=new Job(conf, "mapreduce on hbase"); job.setJarByClass(HbaseMR.class); Scan scan=new Scan(); scan.setCaching(1000);// TableMapReduceUtil.initTableMapperJob("students", scan, MyMapper.class, Text.class, Text.class, job); TableMapReduceUtil.initTableReducerJob("student-age", MyReducer.class, job); job.waitForCompletion(true); } catch (Exception e) { e.printStackTrace(); } }}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report