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 imports text data into HBase

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Overall description: import the data from local files into hbase after collation

Create a table in HBase

Data format

MapReduce program

Map program

Package com.hadoop.mapreduce.test.map;import java.io.IOException;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;public class WordCountHBaseMapper extends Mapper {public Text keyValue = new Text (); public Text valueValue = new Text (); / / data type is: key@addressValue#ageValue#sexValue @ Override protected void map (Object key, Text value, Context context) throws IOException, InterruptedException {String lineValue = value.toString () If (lineValue! = null) {String [] valuesArray = lineValue.split ("@"); context.write (new Text (valuesArray [0]), new Text (valuesArray [1]));}

Reduce program

Package com.hadoop.mapreduce.test.reduce;import java.io.IOException;import java.util.Iterator;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.mapreduce.TableReducer;import org.apache.hadoop.io.NullWritable;import org.apache.hadoop.io.Text;public class WordCountHBaseReduce extends TableReducer {@ Override protected void reduce (Text key, Iterable value, Context out) throws IOException, InterruptedException {String keyValue = key.toString () Iterator valueIterator = value.iterator (); while (valueIterator.hasNext ()) {Text valueV = valueIterator.next (); String [] valueArray = valueV.toString (). Split ("#"); Put putRow = new Put (keyValue.getBytes ()) PutRow.add ("address" .getBytes (), "baseAddress" .getBytes (), valueArray [0] .getBytes ()); putRow.add ("sex" .getBytes (), "baseSex" .getBytes (), valueArray [1] .getBytes ()) PutRow.add ("age" .getBytes (), "baseAge" .getBytes (), valueArray [2] .getBytes ()); out.write (NullWritable.get (), putRow);}

Main program

Package com.hadoop.mapreduce.test;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import com.hadoop.mapreduce.test.map.WordCountHBaseMapper;import com.hadoop.mapreduce.test.reduce.WordCountHBaseReduce / * read the contents of hdfs and insert them into the hbase table, then read the contents of the hbase table and insert the statistical results into hbase * / public class WordCountHBase {public static void main (String args []) throws IOException, InterruptedException, ClassNotFoundException {Configuration conf = HBaseConfiguration.create (); conf.set ("hbase.zookeeper.quorum", "192.168.192.137") Job job = Job.getInstance (conf, "MapReduceHbaseJob"); / / various class job.setJarByClass (WordCountHBase.class); job.setMapperClass (WordCountHBaseMapper.class); TableMapReduceUtil.initTableReducerJob ("userInfo3", WordCountHBaseReduce.class, job); FileInputFormat.addInputPath (job, new Path (args [0])); job.setMapOutputKeyClass (Text.class); job.setMapOutputValueClass (Text.class) System.exit (job.waitForCompletion (true)? 0: 1);}}

Results:

Note: if the client you are running does not have hbase, you need to add the lib of hbase to the lib in hadoop.

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