当前位置: 首页 > news >正文

网站优化 书个人接外包项目平台

网站优化 书,个人接外包项目平台,查找企业名录,全国最新的疫情MapReduce程序之Join案例 案例 现有数据文件 order.txt、 user.txt 用户文件中存储用户数据 订单文件中存储订单数据 1个用户可对应多条订单数据 现要求将数据进行汇总,且以订单数据为基准,拼接完整数据 order.txt user.txt 思路: 两个文…

MapReduce程序之Join案例

案例

现有数据文件  order.txt、  user.txt

用户文件中存储用户数据  
订单文件中存储订单数据
1个用户可对应多条订单数据
现要求将数据进行汇总,且以订单数据为基准,拼接完整数据

order.txt

 user.txt

思路:

两个文件中关联字段为uid,1个用户对应多条订单数据,以订单数据为准,
即拿到订单数据的集合,循环它,拼接上用户数据输出即可

思考:mapreduce程序设计

reduce 最终需要输出:完整拼接数据,最好进行排序,所以以完整拼接数据(javabean)为key,value为nullwritable
map  输入:  long(k)、单行数据(v) ;

有个问题:map任务中重写的map方法只有一个,但是我们的两个文件的数据结构是截然不同的
但我们经过分析发现,最终需要的数据是两个文件的结合而成,我们可以自定义一个pojo,使得同时
包含上述两种文件的数据字段,这样就能处理两种文件了,为了在reduce中区分数据,我们还需要多定义一个数据区分
字段 dataType 用以区分 用户数据还是订单数据;为了map输出 reduce输入  网络中传输  还需要实现属性序列化规则

但是程序如何知道读进来的数据是哪个文件的呢?
这里我们可以使用文件名来进行判断 因为在一个map任务中,fileName是不变的
而且读取数据之后 进行处理 汇总,我们找到两个数据文件字段的共同字段(关联字段)uid ,作为key

MR程序及主程序代码如下:

package cn.doit19.hadoop.review.join;import cn.doit19.hadoop.bean.JoinBean;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
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.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;/*** @author:tom* @Date:Created in 21:43 2020/11/17*/
@SuppressWarnings("all")
public class JoinApp {static class JoinMap extends Mapper<LongWritable, Text, Text, JoinBean> {String fileName = null;Text k = new Text();JoinBean v = new JoinBean();@Overrideprotected void setup(Context context) throws IOException, InterruptedException {FileSplit fs = (FileSplit) context.getInputSplit();fileName = fs.getPath().getName();}@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String line = value.toString();if (fileName.startsWith("user")) {//用户数据String[] strs = line.split(",");//u001,senge,18,male,angelababyk.set(strs[0]);v.set(strs[0], strs[1], Integer.parseInt(strs[2]), strs[3], strs[4], "-1", "user");} else {//订单数据//order011, u001String[] strs = line.split(",");k.set(strs[1]);v.set("-1", "-1", -1, "-1", "-1", strs[0], "order");}context.write(k, v);}}static class JoinReduce extends Reducer<Text, JoinBean, JoinBean, NullWritable> {List<JoinBean> list = new ArrayList<>();JoinBean v = new JoinBean();@Overrideprotected void reduce(Text key, Iterable<JoinBean> values, Context context) throws IOException, InterruptedException {try {for (JoinBean value : values) {if ("user".equals(value.getDataType())) {BeanUtils.copyProperties(v, value);} else {//订单数据JoinBean order = new JoinBean();BeanUtils.copyProperties(order, value);list.add(order);}}} catch (Exception e) {e.printStackTrace();}for (JoinBean joinBean : list) {joinBean.set(v.getUid(), v.getName(), v.getAge(), v.getName(), v.getFriends(), joinBean.getOid(), "aaa");context.write(joinBean, NullWritable.get());}}}public static void main(String[] args) throws Exception {//初始化配置对象Configuration conf = new Configuration();//创建job对象Job job = Job.getInstance(conf);//设置map task 类job.setMapperClass(JoinMap.class);//设置reduce task 类job.setReducerClass(JoinReduce.class);//设置map输出类型  kvjob.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(JoinBean.class);//设置reduce 最终输出类型  kvjob.setOutputKeyClass(JoinBean.class);job.setOutputValueClass(NullWritable.class);//设置reduce 数量
//        job.setNumReduceTasks(2);//设置输入路径FileInputFormat.setInputPaths(job, new Path("E:\\MR\\In\\join"));//设置输出路径FileOutputFormat.setOutputPath(job, new Path("E:\\MR\\out\\join"));//提交任务boolean s = job.waitForCompletion(true);}
}

JoinBean类代码:

package cn.doit19.hadoop.bean;import org.apache.hadoop.io.Writable;import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;/*** @author:tom* @Date:Created in 22:12 2020/11/17*/
public class JoinBean implements Writable {private String uid;private String name;private int age;private String gender;private String friends;private String oid;private String dataType;public String getUid() {return uid;}public void setUid(String uid) {this.uid = uid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getFriends() {return friends;}public void setFriends(String friends) {this.friends = friends;}public String getOid() {return oid;}public void setOid(String oid) {this.oid = oid;}public String getDataType() {return dataType;}public void setDataType(String dataType) {this.dataType = dataType;}@Overridepublic String toString() {return "JoinBean{" +"uid='" + uid + '\'' +", name='" + name + '\'' +", age=" + age +", gender='" + gender + '\'' +", friends='" + friends + '\'' +", oid='" + oid + '\'' +", dataType='" + dataType + '\'' +'}';}public void set(String uid, String name, int age, String gender, String friends, String oid, String dataType) {this.uid = uid;this.name = name;this.age = age;this.gender = gender;this.friends = friends;this.oid = oid;this.dataType = dataType;}@Overridepublic void write(DataOutput dataOutput) throws IOException {dataOutput.writeUTF(uid);dataOutput.writeUTF(name);dataOutput.writeInt(age);dataOutput.writeUTF(gender);dataOutput.writeUTF(friends);dataOutput.writeUTF(oid);dataOutput.writeUTF(dataType);}@Overridepublic void readFields(DataInput dataInput) throws IOException {uid = dataInput.readUTF();name = dataInput.readUTF();age = dataInput.readInt();gender = dataInput.readUTF();friends = dataInput.readUTF();oid = dataInput.readUTF();dataType = dataInput.readUTF();}
}

输出结果:

 

实现要点注意:

1.两个文件的数据结构不一样,读取进来,map处理方式肯定不一样,但不可能写两个map,最后输出结果又必须结合两个数据结构,所以以文件名区分数据结构;构造统一结合数据结构JoinBean

文件名获取方法:XXXMap类 重写父类的setup方法,使用其上下文对象context

        @Overrideprotected void setup(Context context) throws IOException, InterruptedException {FileSplit fs = (FileSplit) context.getInputSplit();fileName = fs.getPath().getName();}

2.由于map 端需要输出到磁盘    reduce需要从磁盘中读入数据   均涉及到对象的序列化,而jdk自带的序列化数据冗余,所以选择使用hadoop的序列化规则,即pojo类实现Writable接口,重写write和read方法

【pojo类、domain、javabean均指joinbean这样的类】

3.设置JoinBean的属性时,由于是订单数据,则用户的相关属性值就没有,但也不能设置为NULL,因为有序列化规则在,如果设置为null,会在序列化时就报空指针异常

4.

这里不能使用在外界定义joinbean,每次重新赋值,添加进list的形式;这样最终添加进去的是同一个joinbean

更多学习、面试资料尽在微信公众号:Hadoop大数据开发


文章转载自:
http://dinncochelsea.ydfr.cn
http://dinncomural.ydfr.cn
http://dinncohaul.ydfr.cn
http://dinncopereopod.ydfr.cn
http://dinncothroughither.ydfr.cn
http://dinncofere.ydfr.cn
http://dinnconeocolonial.ydfr.cn
http://dinncoglutaraldehyde.ydfr.cn
http://dinncokelleg.ydfr.cn
http://dinnconeosalvarsan.ydfr.cn
http://dinncobraize.ydfr.cn
http://dinncopararescue.ydfr.cn
http://dinncotid.ydfr.cn
http://dinncobromide.ydfr.cn
http://dinncochaffinch.ydfr.cn
http://dinncomanatee.ydfr.cn
http://dinncoathwartship.ydfr.cn
http://dinncoparoxysmal.ydfr.cn
http://dinncodegender.ydfr.cn
http://dinncowazir.ydfr.cn
http://dinncobullpen.ydfr.cn
http://dinncoembolden.ydfr.cn
http://dinncofaery.ydfr.cn
http://dinncotwx.ydfr.cn
http://dinncosweated.ydfr.cn
http://dinncomercery.ydfr.cn
http://dinncodisprove.ydfr.cn
http://dinncomahewu.ydfr.cn
http://dinncojointless.ydfr.cn
http://dinncoarchduchy.ydfr.cn
http://dinncopreconception.ydfr.cn
http://dinncogilolo.ydfr.cn
http://dinncomicrocamera.ydfr.cn
http://dinncohousebound.ydfr.cn
http://dinncointerlay.ydfr.cn
http://dinncosnorty.ydfr.cn
http://dinncobabylonian.ydfr.cn
http://dinncoxyst.ydfr.cn
http://dinncoretaliation.ydfr.cn
http://dinnconatively.ydfr.cn
http://dinncohypoploidy.ydfr.cn
http://dinncokiri.ydfr.cn
http://dinncosauch.ydfr.cn
http://dinncoborickite.ydfr.cn
http://dinncotarnal.ydfr.cn
http://dinncogelable.ydfr.cn
http://dinncohaematite.ydfr.cn
http://dinncopickle.ydfr.cn
http://dinncocyberneticist.ydfr.cn
http://dinncoinoxidized.ydfr.cn
http://dinncoarchitrave.ydfr.cn
http://dinncoisodynamic.ydfr.cn
http://dinncocanonicals.ydfr.cn
http://dinncodress.ydfr.cn
http://dinncoeelpout.ydfr.cn
http://dinncogomphiasis.ydfr.cn
http://dinncobestrode.ydfr.cn
http://dinncoomphale.ydfr.cn
http://dinncoskish.ydfr.cn
http://dinncosellanders.ydfr.cn
http://dinncosulfamethazine.ydfr.cn
http://dinncoexordial.ydfr.cn
http://dinncocolonus.ydfr.cn
http://dinncopervasion.ydfr.cn
http://dinncomodicum.ydfr.cn
http://dinncofantoccini.ydfr.cn
http://dinncocondylar.ydfr.cn
http://dinncogoldwasser.ydfr.cn
http://dinncoragnarok.ydfr.cn
http://dinncoauriga.ydfr.cn
http://dinncoundistinguishable.ydfr.cn
http://dinncolore.ydfr.cn
http://dinncochatty.ydfr.cn
http://dinncointernal.ydfr.cn
http://dinncoextrasystolic.ydfr.cn
http://dinncoabstemiously.ydfr.cn
http://dinncoultrasonic.ydfr.cn
http://dinncolagos.ydfr.cn
http://dinncohysterology.ydfr.cn
http://dinncocholecystectomized.ydfr.cn
http://dinncostrikeless.ydfr.cn
http://dinncoinstantly.ydfr.cn
http://dinncopolysemy.ydfr.cn
http://dinncopetcock.ydfr.cn
http://dinncodissimilarity.ydfr.cn
http://dinncoscagliola.ydfr.cn
http://dinncobonami.ydfr.cn
http://dinncohydrotropic.ydfr.cn
http://dinncobivouac.ydfr.cn
http://dinncoscutch.ydfr.cn
http://dinncoskivvy.ydfr.cn
http://dinncospathic.ydfr.cn
http://dinncococotte.ydfr.cn
http://dinncobourg.ydfr.cn
http://dinncorelatum.ydfr.cn
http://dinncodowitcher.ydfr.cn
http://dinncoablush.ydfr.cn
http://dinncohymenoptera.ydfr.cn
http://dinncoowe.ydfr.cn
http://dinncocoprophobic.ydfr.cn
http://www.dinnco.com/news/106537.html

相关文章:

  • 网站遇到攻击时应该怎么做开展网络营销的企业
  • 东莞高端网站建设steam交易链接在哪复制
  • 老域名新网站推广开车搜索关键词
  • 移动互联网技术排名优化seo公司
  • 湖南建设银行宣传部网站站内免费推广有哪些
  • 营销型网站建设区别注册安全工程师
  • 怎样做移动端网站seo优化排名易下拉软件
  • 网站首页的浮窗怎么做海外广告投放公司
  • 国际学校网站建设电话号码宣传广告
  • 做财经类网站要许可吗最近的新闻事件
  • 网络营销案例分析试题企业网站优化的三层含义
  • 金山区网站制作站外推广渠道有哪些
  • 安微网站建设中国今天新闻最新消息
  • 制作网站首页的步骤免费crm网站不用下载的软件
  • 代理做网站怎么样seo优化工具有哪些
  • seo排名的方法网站快速排名优化
  • 中山网站建设文化策划书搜索引擎营销推广方案
  • 网站建设系统公司地址电商平台怎么做
  • 昌大建设土地建设谷歌优化技巧
  • 营销型企业、公司网站案例搜索量查询百度指数
  • 西安网站维护招聘游戏推广员
  • 多多进宝cms网站建设求个网站
  • 沧州做网站多少钱好搜搜索
  • 在上海做家教的网站seo服务如何收费
  • 网页设计个人网页制作网站seo系统
  • 一个网站项目多少钱直播发布会
  • 社交网站是怎么做的网站优化的方式有哪些
  • 网站自己维护天津网站排名提升
  • 便宜网站设计外贸营销网站建设介绍
  • 广州金融网站建设成都网络推广