首页 > 编程语言 >大数据实验(MapReduce编程2)

大数据实验(MapReduce编程2)

时间:2023-12-05 19:12:40浏览次数:35  
标签:Text 编程 MapReduce public job 实验 context new class

代码参考:

MapReduce实验 - CodeDancing - 博客园 (cnblogs.com)

编程实现总代码:

编译工具:IDEA

说明:

1.完成不同的任务的时候,需要修改cmd的值

2.conf.set("fs.default.name","hdfs://node1:8020");换上自己的连接路径

3.System.setProperty("HADOOP_USER_NAME","root");

不加上这个会出现权限不足,报错信息:Permission denied: user=1234, access=WRITE, inode=“/“:root:supergroup:drwxr-xr-x

发现idea中连接过去是以本机window用户,比如说我用户是1234,连接过去user就是1234,但是hdfs用户里面没有这个用户。

我的解决方法:System.setProperty("HADOOP_USER_NAME","root");说明用户名是root,同时,hdfs中root有修改添加等权限。

import java.io.IOException;
import java.util.ArrayList;
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;

public class Merge {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        conf.set("fs.default.name","hdfs://node1:8020");
        System.setProperty("HADOOP_USER_NAME","root");
        Job job = Job.getInstance(conf);
        job.setJarByClass(Merge.class);

        int cmd = 3;
//        String[] file = {"input" + cmd, "output" + cmd};
        String[] file = {"/user/root/input", "/user/root/output"};
        if (cmd == 1) {
            job.setMapperClass(Merge.Mapper1.class);
            job.setCombinerClass(Merge.Reducer1.class);
            job.setReducerClass(Merge.Reducer1.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            FileInputFormat.setInputPaths(job, new Path(file[0]));
            FileOutputFormat.setOutputPath(job, new Path(file[1]));

        } else if (cmd == 2) {
            job.setMapperClass(Merge.Mapper2.class);
            job.setReducerClass(Merge.Reducer2.class);
            job.setOutputKeyClass(IntWritable.class);
            job.setOutputValueClass(IntWritable.class);
            FileInputFormat.setInputPaths(job, new Path(file[0]));
            FileOutputFormat.setOutputPath(job, new Path(file[1]));
        } else {
            job.setMapperClass(Merge.Mapper3.class);
            job.setReducerClass(Merge.Reducer3.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            FileInputFormat.setInputPaths(job, new Path(file[0]));
            FileOutputFormat.setOutputPath(job, new Path(file[1]));
        }
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }



    public static class Mapper1 extends Mapper<Object,Text,Text,Text>
    {
        public void map(Object key,Text value,Mapper<Object,Text,Text,Text>.Context context) throws IOException, InterruptedException
        {
            context.write(value, new Text(""));
        }
    }

    public static class Reducer1 extends Reducer<Text,Text,Text,Text>
    {

        public void reduce(Text key,Iterable<Text> values,Reducer<Text,Text,Text,Text>.Context context) throws IOException,InterruptedException
        {
            context.write(key, new Text(""));
        }
    }

    public static class Mapper2 extends Mapper<Object,Text,IntWritable,IntWritable>
    {
        public void map(Object key,Text value,Context context) throws IOException, InterruptedException
        {
            IntWritable data = new IntWritable();
            data.set(Integer.parseInt(value.toString()));
            context.write(data, new IntWritable(1));
        }
    }

    public static class Reducer2 extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>
    {
        private static int sum = 1;
        public void reduce(IntWritable key,Iterable<IntWritable> values,Context context) throws IOException, InterruptedException
        {
            for(IntWritable num:values)
            {
                context.write(new IntWritable(sum),key);
                sum++;//可能存在重复的数字
            }
        }
    }

    public static class Mapper3 extends Mapper<Object,Text,Text,Text>
    {
        public void map(Object key,Text value,Context context) throws IOException, InterruptedException
        {
            String[] splStr = value.toString().split(" ");
            String child = splStr[0];
            String parent = splStr[1];

            if(child.equals("child")&&parent.equals("parent"))
                return;
            context.write(new Text(child), new Text("old#"+parent));
            context.write(new Text(parent), new Text("young#"+child));
        }
    }

    public static class Reducer3 extends Reducer<Text,Text,Text,Text>
    {
        private static boolean head = true ;
        public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException
        {
            if(head)
            {
                context.write(new Text("grandchild"), new Text("grandparent"));
                head = false;
            }
            ArrayList<String> grandchild = new ArrayList<String>();
            ArrayList<String> grandparent = new ArrayList<String>();
            String[] temp;
            for(Text val:values)
            {
                temp = val.toString().split("#");
                if(temp[0].equals("young"))
                    grandchild.add(temp[1]);
                else
                    grandparent.add(temp[1]);
            }
            if(grandchild.size()==0||grandparent.size()==0)
                return;
            for(String gc:grandchild)
                for(String gp:grandparent)
                    context.write(new Text(gc), new Text(gp));
        }
    }

}

 

(一)

(1)编写A.txt、B.txt

 (2)上传到hdfs上

 (3)编程实现的结果

 代码在上面,总代码,但是cmd要自己改为1

 (二)

(1)准备text1.txt、text2.txt、text3.txt数据,且上传到hdfs上

 (2)编程实现

 

总代码cmd改成2

 

(三)

说明:在写child-parent的时候,中间用一个空格隔开数据,不要用其他的。

 

(1)数据准备

(2)编程实现

代码cmd改成3

 

标签:Text,编程,MapReduce,public,job,实验,context,new,class
From: https://www.cnblogs.com/hmy22466/p/17877935.html

相关文章

  • 大数据实验——mysql服务的启动
    黑马程序的mysql服务启动密码是hadoop直接在主控制台上输出mysql-uroot-p然后输入密码进入mysql服务 剩下的就是在finallshell里面进行一些建表增删改查操作,还有一个问题就是通过java代码进行对表数据的增删改查我才用的方法是在Navicat里面建一个node1主机的链接然后就......
  • 1、开篇 - 编程指导系列文章
          笔者进行软件编程已经有十多年的时间了。这些年,最主要的学习内容在开始的那些年,后面因为转了管理方向,所以其它技术内容就是知道有这么个东西,但是没实际去编程。这段时间没啥事,就想把原来的代码进行下重构,解决一些技术问题,然后,就想着把编程这类事情做一个指导系列的文......
  • 【linux上机实验】实验八 Linux编程实验
    1.使用系统调用对文件进行操作。编写一个程序,把一个文件的内容复制到另一个文件上,即实现简单的copy功能。要求:只用open(),read(),write(),close()系统调用,程序要求带参数运行,第一个参数是源文件,第二个参数是目标文件。步骤一:创建file_copy.c文件vifile_copy.c步骤二:将下列代......
  • 抽奖系统的部署(实验可行)
    以Windows10为例1.node安装最新版Node下载Node.js,一直下一步->安装完毕验证2.程序压缩包下载&解压lotterycdlottery#服务端插件安装cdservernpminstall#前端插件安装cd../productnpminstall#打包-这一步的时候出现了报错#Error:error:0308010C:digita......
  • [编程] AI助力软件项目正向生成,注释编写的革命
    引言软件项目质量直接影响着用户体验和企业效益。随着软件的应用范围不断扩大,提高软件质量的重要性也日益凸显。传统上,软件工程师通常采用自下而上的开发模式,自行设计实现代码并进行测试,这给质量把控带来一定难度。而注释与知识管理在这个过程中可以发挥重要作用。注释作为......
  • 2023.12.4学习笔记(stm32跑马灯实验——库函数)
     STM32f4有七组引脚(GPIOx),每组引脚有16个IO口,每组由十个寄存器控制。   查找STM32引脚的功能,可以在STM32F04ZGT6文件50页左右查询,此文件所在的位置为硬件资料、芯片资料文件夹里。跑马灯实验思路步骤:1:使能时钟,调用函数RCC_AHB1PeriphClockCmd();       ......
  • Python编程:从入门到实践--Chapter16
    在16章的json数据测试如下代码时报错:[Errno22]Invalidargument:'eq_data\readable_eq_data.json'#将数据作为字符串读取并转换为python对象path=Path('eq_data\eq_data_1_day_m1.json')contents=path.read_text()all_eq_data=json.loads(contents)#将数据文件转换为......
  • 基于Java的实验室设备管理系统设计与实现(源码+lw+部署文档+讲解等)
    文章目录前言具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序(小蔡coding)代码参考数据库参考源码获取前言......
  • 软件测试/人工智能|Python 数据类型解析:探索编程世界的多样性
    数据类型是编程中不可或缺的基本概念。在Python中,有多种数据类型,每种都有其独特的特点和用途。本文将带你深入了解常见的Python数据类型及其实际应用。引言在编程中,数据类型是对数据进行分类和组织的方式。Python中有多种数据类型,每种类型都有其自身的特性和功能。了解这......
  • [oeasy]python0002_终端_CLI_GUI_编程环境_游戏_真实_元宇宙
    回忆 上次了解了python语言的特点历史悠久功能强大深受好评已成趋势 3大主流操作系统macwindowslinux      添加图片注释,不超过140字(可选)  我们选择linux作为基础系统 ......