博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mapreduce文件读取与清洗
阅读量:6882 次
发布时间:2019-06-27

本文共 3609 字,大约阅读时间需要 12 分钟。

hot3.png

package com.demo.admin;import java.io.DataInput;import java.io.DataOutput;import java.io.IOException;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.io.Writable;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;import org.apache.hadoop.util.GenericOptionsParser;import org.apache.hadoop.util.Tool;import org.apache.hadoop.util.ToolRunner;public class Test extends Configured implements Tool {//构建map类public static class TestMap extends Mapper
{public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{//根据$开始切割字段名final String[] splited = value.toString().split(“\\$”);//以name为key从0开始final String name = splited[0];final Text k2 = new Text(name);//phone shiqu就是第四位和第八位final TestWritable v2=new TestWritable(splited[4], splited[8]);//name为key值 phone和shiqu为v2写入context.write(k2, v2);}}//构建reduce类public static class TestReduce extends Reducer
{public void reduce(Text k2,Iterable
 v2s,Context context) throws IOException, InterruptedException{String phone;String shiqu;//循环所有的key值和values值for(TestWritable testWritable:v2s){phone=testWritable.phone;shiqu=testWritable.shiqu;TestWritable v3=new TestWritable(phone, shiqu);context.write(k2, v3);}}}//main方法启动public static void main(String [] args) throws IOException, Exception{ToolRunner.run(new Test(), args);}@SuppressWarnings(“deprecation”)public int run(String[] args) throws Exception {Configuration conf=new Configuration();String[]argArray=new GenericOptionsParser(conf, args).getRemainingArgs();if(argArray.length!=2){System.out.println(“请提供两个参数”);System.exit(1);}Job job=Job.getInstance(conf, “Test”);FileSystem fs = FileSystem.get(new URI(args[1]), conf);fs.delete(new Path(args[1]));job.setJarByClass(Test.class);job.setMapperClass(TestMap.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(TestWritable.class);job.setReducerClass(TestReduce.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(TestWritable.class);job.setInputFormatClass(TextInputFormat.class);job.setOutputFormatClass(TextOutputFormat.class);FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job,new Path(args[1]));job.waitForCompletion(true);return 0;}static class TestWritable implements Writable{String phone;String shiqu;public TestWritable(String phone,String shiqu){this.phone=phone;this.shiqu=shiqu;}//无参构造方法public class UserBean implements Writable//这个应该是在自定义writable的时候需要注意,反射过程中需要调用无参构造。public TestWritable(){}public void readFields(DataInput in) throws IOException {this.phone=in.readUTF();this.shiqu=in.readUTF();}public void write(DataOutput out) throws IOException {out.writeUTF(phone);out.writeUTF(shiqu);}public String toString() {return phone + “\t” + shiqu + “\t”;}}}  示例文件:张三$25$男$未婚$15997444444$409930360$中国$湖北$广水输入文件:张三 15997444444广水 shell 命令:/usr/local/hadoop/bin/hadoop fs -put /home/XX/test.txt /test_log//usr/local/hadoop/bin/hadoop jar /home/XX/test.jar /test_log/test.txt /test_cleaned/ 1>/dev/nul

转载于:https://my.oschina.net/u/559635/blog/550676

你可能感兴趣的文章
微软262亿美元收购LinkedIn
查看>>
c/c++(hiredis)异步调用redis【转】
查看>>
Ceph集群块设备使用-创建和使用OSD
查看>>
大数据||hadoop分布式集群安装
查看>>
华为设备默认console密码
查看>>
wxWidgets第四课 EVT_LEFT_UP关联鼠标弹起事件不生效
查看>>
【故障解决】ORA-06502错误解决
查看>>
升级Windows 10周年更新部分用户遭遇卡死BUG
查看>>
WannaCry病毒提醒CIO要掌握打补丁
查看>>
昂纳科技2016年营收15.98亿港元 数据中心业务大增409%
查看>>
国内首个商用固移融合视频业务用户数破200万
查看>>
三星高管:家电业务将实行并购策略 海尔并购案影响小
查看>>
微软重调对处理器的技术支持 都怪你不升Win10?
查看>>
为何还处于概念阶段的智能家居被3.15点名批评
查看>>
捷克光伏电站的装机容量为何停滞不前?
查看>>
CipherLab展出NFC安卓电脑,UHF RFID蓝牙读取器
查看>>
产品经理的新三观:数据观、格局观、细节观
查看>>
大数据技术服务商个推获4亿人民币D轮融资
查看>>
Centos命令系列 之 screen
查看>>
Mac OS X版本的sublime text 3安装汇编语言语法支持
查看>>