首页 > 其他分享 >大数据从入门到实战 - 第3章 MapReduce基础实战——信息挖掘 - 挖掘父子关系

大数据从入门到实战 - 第3章 MapReduce基础实战——信息挖掘 - 挖掘父子关系

时间:2023-11-27 22:56:37浏览次数:32  
标签:实战 String Text MapReduce value split child 挖掘 new

输出一直顺序不正确,把正确答案和我自己写的混了混,目前感觉是mapper的问题
正确输出:

grand_child    grand_parent
Mark    Jesse
Mark    Alice
Philip    Jesse
Philip    Alice
Jone    Jesse
Jone    Alice
Steven    Jesse
Steven    Alice
Steven    Frank
Steven    Mary
Jone    Frank
Jone    Mary
package org.csh;
/**
 * @author :Changersh
 * @date : 2023/11/27 21:03
 */

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class simple_data_mining {
    public static int time = 0;

    /**
     *
     */
    // Map将输入文件按照空格分割成child和parent,然后正序输出一次作为右表,反序输出一次作为左表,需要注意的是在输出的value中必须加上左右表区别标志
    // 要标记一下,哪个是反过来的
    /*
        一个人既是父亲又是儿子,那么倒过来的时候,ta的名字后跟着的就是ta的父子,这俩是爷孙,根据标记输出即可
     */
    public static class Map extends Mapper<Object, Text, Text, Text> {
        Text outK = new Text();
        Text outV = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            /********** Begin **********/

            String[] split = value.toString().split("\\s+");
            if (split[0].equals("child")) return;
            outK.set(split[0]);
//            outV.set("+" + split[1]); // 正序
            outV.set("2+" + split[0] + "+" + split[1]);
            context.write(outK, outV);
            outK.set(split[1]);
//            outV.set("-" + split[0]); // 倒序
            outV.set("1+" + split[0] + "+" + split[1]);
            context.write(outK, outV);


//            String line = value.toString();
//            String[] childAndParent = line.split(" ");
//            List<String> list = new ArrayList<>(2);
//            for (String childOrParent : childAndParent) {
//                if (!"".equals(childOrParent)) {
//                    list.add(childOrParent);
//                }
//            }
//            if (!"child".equals(list.get(0))) {
//                String childName = list.get(0);
//                String parentName = list.get(1);
//                String relationType = "1";
//                context.write(new Text(parentName), new Text(relationType + "+"
//                        + childName + "+" + parentName));
//                relationType = "2";
//                context.write(new Text(childName), new Text(relationType + "+"
//                        + childName + "+" + parentName));
//            }
            /********** End **********/
        }
    }

    public static class Reduce extends Reducer<Text, Text, Text, Text> {
        Text outK = new Text();
        Text outV = new Text();

        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            /********** Begin **********/

            //输出表头
            if (time == 0) {
                time = 1;
                outK.set("grand_child");
                outV.set("grand_parent");
                context.write(outK, outV);
            }

//
//            ArrayDeque<String> child = new ArrayDeque<>();
//            ArrayDeque<String> parent = new ArrayDeque<>();
//
//            //获取value-list中value的child
//            //获取value-list中value的parent
//            for (Text value : values) {
//                String s = value.toString();
//                if (s.startsWith("-")) child.addLast(s.substring(1));
//                else if (s.startsWith("+")) parent.addLast(s.substring(1));
//            }
//
//
//            //左表,取出child放入grand_child
//            //右表,取出parent放入grand_parent
//            //输出结果
//
//            for (String s : child) {
//                for (String s1 : parent) {
//                    outK.set(s);
//                    outV.set(s1);
//                    context.write(outK, outV);
//                }
//            }

            //获取value-list中value的child
            List<String> grandChild = new ArrayList<>();

            //获取value-list中value的parent
            List<String> grandParent = new ArrayList<>();

            //左表,取出child放入grand_child
            for (Text text : values) {
                String s = text.toString();
                String[] relation = s.split("\\+");
                String relationType = relation[0];
                String childName = relation[1];
                String parentName = relation[2];
                if ("1".equals(relationType)) {
                    grandChild.add(childName);
                } else {
                    grandParent.add(parentName);
                }
            }

            //右表,取出parent放入grand_parent
            int grandParentNum = grandParent.size();
            int grandChildNum = grandChild.size();
            if (grandParentNum != 0 && grandChildNum != 0) {
                for (int m = 0; m < grandChildNum; m++) {
                    for (int n = 0; n < grandParentNum; n++) {
                        //输出结果
                        context.write(new Text(grandChild.get(m)), new Text(
                                grandParent.get(n)));
                    }
                }
            }


            /********** End **********/

        }
    }

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "Single table join");
        job.setJarByClass(simple_data_mining.class);
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        String inputPath = "D:\\DataFiles\\Hadoop\\Input\\Educoder";   //设置输入路径
        String outputPath = "D:\\DataFiles\\Hadoop\\Output\\Educoder\\ChildAndParent";   //设置输出路径
        FileInputFormat.addInputPath(job, new Path(inputPath));
        FileOutputFormat.setOutputPath(job, new Path(outputPath));
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }

}


标签:实战,String,Text,MapReduce,value,split,child,挖掘,new
From: https://www.cnblogs.com/Changersh/p/17860764.html

相关文章

  • 超详细的Mysql锁 实战分析,你想知道的都在这里~
    1.mysql回表查询在这里提起主要是用于说明mysql数据和索引的结构,有助于理解后续加锁过程中的一些问题。mysql索引结构和表数据结构是相互独立的,根据索引查询,只能找到索引列和主键聚簇索引。如果select语句中不包含索引列,mysql会根据主键聚簇索引二次回表查询所需要的数据,查询出......
  • 五分钟 k8s 实战-应用探针
    今天进入kubernetes的运维部分(并不是运维kubernetes,而是运维应用),其实日常我们大部分使用kubernetes的功能就是以往运维的工作,现在云原生将运维和研发关系变得更紧密了。今天主要讲解Probe探针相关的功能,探针最实用的功能就是可以控制应用优雅上线。就绪探针举个例子,当......
  • 2023版 STM32实战8 独立看门狗(IWDG)
     IWDG简介 STM32F10xxx内置两个看门狗,提供了更高的安全性、时间的精确性和使用的灵活性。两个看门狗设备(独立看门狗和窗口看门狗)可用来检测和解决由软件错误引起的故障。 说人话就是能解决程序跑飞的问题。  编写代码思路 -1-使用这个功能必须解除写保护-2-IW......
  • spark的shuffle和mapreduce的shuffle的区别
    功能上,MR的shuffle和Spark的shuffle是没啥区别的,都是对Map端的数据进行分区,要么聚合排序,要么不聚合排序,然后Reduce端或者下一个调度阶段进行拉取数据,完成map端到reduce端的数据传输功能。方案上,有很大的区别,MR的shuffle是基于合并排序的思想,在数据进入reduce端之前,都会进行sor......
  • 2023.11.17-20湖北 武汉 2023第五届全国生物医学数据挖掘与计算学术会议拟于2023年1
     2023第五届全国生物医学数据挖掘与计算学术会议拟于2023年11月17日-20日于华中科技大学举行。会议简介:     全国生物医学数据挖掘与计算学术会议是一个专注于生物医学大数据算法、软件与人工智能方法的重要学术盛会。生物医学领域的快速发展导致了大量的生物医学数据......
  • JVM学习记录六(JVM调优实战案例)
    这几期的文章都通过学习黑马课程里老师的内容总结出来的,想看视频的伙伴可以上B站搜索《黑马程序员》一、内存溢出内存溢出的位置有三个,如图所示:思路:1.获取堆内存快照2.VisualVM分析dump文件3.通过查看对内信息的情况,定位内存溢出的问题4.找到代码,找到问题代码进行修复二、CPU飚高......
  • 15-有参转录组实战1-批量质控-fastp
     #本教程部分参考B站15天入门生物信息教程,在开启以下教程前,请务必看看我前面两个教程,Linux系统上安装R语言(https://www.bilibili.com/read/cv24718269)和下载好转录组(https://www.bilibili.com/read/cv24719254)。#1,我们对上次下载的转录组进行实战分析,首先进行质量控制,使用fast......
  • day12 购物车系统实战
    注册函数:defregisiter():withopen(r"F:\pylearn\day12\购物车系统\账号密码.txt","r",encoding="utf8")asfr:user_dict=dict()data=fr.read()foriinrange(len(data.split())):user_dict[data.......
  • # yyds干货盘点 # 盘点一个Pandas处理Excel表格实战问题(下篇)
    大家好,我是皮皮。一、前言继续接着上一篇文章说,这一篇文章我们一起来看看大佬们的解决办法。二、实现过程这里【郑煜哲·Xiaopang】和【瑜亮老师】给了一个提示,如下图所示:后来【隔壁......
  • 【23秋】提高实战营 之 课后习题篇
    HomeworkWeek1A-排序题目链接A-排序完整代码......