整體描述:將本地文件的數(shù)據整理之后導入到hbase中
成都創(chuàng)新互聯(lián)專注于澄邁企業(yè)網站建設,成都響應式網站建設公司,電子商務商城網站建設。澄邁網站建設公司,為澄邁等地區(qū)提供建站服務。全流程按需網站建設,專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
在HBase中創(chuàng)建表
數(shù)據格式
MapReduce程序
map程序
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<Object, Text, Text, Text>{ public Text keyValue = new Text(); public Text valueValue = new Text(); //數(shù)據類型為: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程序
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<Text, Text, NullWritable>{ @Override protected void reduce(Text key, Iterable<Text> value, Context out) throws IOException, InterruptedException { String keyValue = key.toString(); Iterator<Text> 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); } } }
主程序
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; /** * 將hdfs上的內容讀取到,并插入到hbase的表中,然后讀取hbase表中的內容,將統(tǒng)計結果插入到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"); //各種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); } }
結果:
注:如果運行的client沒有hbase,需要在hadoop里面的lib中加入hbase的lib
文章名稱:MapReduce將文本數(shù)據導入到HBase中
本文地址:http://vcdvsql.cn/article8/jhipip.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站建設、微信小程序、網站收錄、品牌網站設計、域名注冊、建站公司
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)