欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

云計(jì)算實(shí)驗(yàn):Java?MapReduce編程

 更新時(shí)間:2022年01月25日 09:34:26   作者:wow_awsl_qwq  
這篇文章主要介紹了云計(jì)算實(shí)驗(yàn):Java?MapReduce編程,?居于Java圍繞MapReduce編程展開詳細(xì)內(nèi)容,文章助大家掌握MapReduce編程,理解MapReduce原理,需要的朋友可以參考一下

實(shí)驗(yàn)題目:

MapReduce:編程

實(shí)驗(yàn)內(nèi)容:

本實(shí)驗(yàn)利用 Hadoop 提供的 Java API 進(jìn)行編程進(jìn)行 MapReduce 編程。

實(shí)驗(yàn)?zāi)繕?biāo):

  • 掌握MapReduce編程。
  • 理解MapReduce原理

【實(shí)驗(yàn)作業(yè)】簡單流量統(tǒng)計(jì)

有如下這樣的日志文件:

13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 2481 24681 200
13726230513 00-FD-07-A4-72-B8:CMCC 120.196.40.8 i02.c.aliimg.com 248 0 200
13826230523 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 2481 24681 200
13726230533 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 2481 24681 200
13726230543 00-FD-07-A4-72-B8:CMCC 120.196.100.82 Video website 1527 2106 200
13926230553 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 2481 24681 200
13826230563 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 2481 24681 200
13926230573 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 2481 24681 200
18912688533 00-FD-07-A4-72-B8:CMCC 220.196.100.82 Integrated portal 1938 2910 200
18912688533 00-FD-07-A4-72-B8:CMCC 220.196.100.82 i02.c.aliimg.com 3333 21321 200
13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 Search Engines 9531 9531 200
13826230523 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 2481 24681 200
13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 2481 24681 200

該日志文件記錄了每個(gè)手機(jī)用戶在一段時(shí)間內(nèi)的網(wǎng)絡(luò)流量信息,具體字段含義為:

手機(jī)號(hào)碼 MAC地址 IP地址 域名 上行流量(字節(jié)數(shù)) 下行流量(字節(jié)數(shù)) 套餐類型
根據(jù)以上日志,統(tǒng)計(jì)出每個(gè)手機(jī)用戶在該時(shí)間段內(nèi)的總流量(上行流量+下行流量),統(tǒng)計(jì)結(jié)果的格式為:

手機(jī)號(hào)碼 字節(jié)數(shù)量

實(shí)驗(yàn)結(jié)果:

實(shí)驗(yàn)代碼:

WcMap.java

import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

        public class WcMap extends Mapper<LongWritable, Text, Text, LongWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
                    String str = value.toString();
                    String[] words = StringUtils.split(str," ",10);
                    int i=0;
                    for(String word : words){
                        if(i==words.length-2||i==words.length-3)
                        context.write(new Text(words[0]), new LongWritable(Integer.parseInt(word)));
                        i++;
                    }
            }
        }

WcReduce.java

import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WcReduce extends Reducer<Text, LongWritable, Text, LongWritable>{
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values,Context context)
            throws IOException, InterruptedException {
        long count = 0;
        for(LongWritable value : values){
            count += value.get();
        }
        context.write(key, new LongWritable(count));
    }
}

WcRunner.java

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.util.Scanner;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import java.net.URI;

public class WcRunner{
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        
        job.setJarByClass(WcRunner.class);
        
        job.setMapperClass(WcMap.class);
        job.setReducerClass(WcReduce.class);
        
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        Scanner sc = new Scanner(System.in);
        System.out.print("inputPath:");
        String inputPath = sc.next();
        System.out.print("outputPath:");
        String outputPath = sc.next();

        try {
            FileSystem fs0 = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            Path hdfsPath = new Path(outputPath);
            fs0.copyFromLocalFile(new Path("/headless/Desktop/workspace/mapreduce/WordCount/data/1.txt"),new Path("/mapreduce/WordCount/input/1.txt"));
            if(fs0.delete(hdfsPath,true)){
                System.out.println("Directory "+ outputPath +" has been deleted successfully!");
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        FileInputFormat.setInputPaths(job, new Path("hdfs://master:9000"+inputPath));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000"+outputPath));
        job.waitForCompletion(true);
        try {
            FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            Path srcPath = new Path(outputPath+"/part-r-00000");

            FSDataInputStream is = fs.open(srcPath);
            System.out.println("Results:");
            while(true) {
                String line = is.readLine();
                if(line == null) {
                    break;
                }
                System.out.println(line);
            }
            is.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

【實(shí)驗(yàn)作業(yè)】索引倒排輸出行號(hào)

在索引倒排實(shí)驗(yàn)中,我們可以得到每個(gè)單詞分布在哪些文件中,以及在每個(gè)文件中出現(xiàn)的次數(shù),修改以上實(shí)現(xiàn),在輸出的倒排索引結(jié)果中可以得到每個(gè)單詞在每個(gè)文件中的具體行號(hào)信息。輸出結(jié)果的格式如下:
單詞 文件名:行號(hào),文件名:行號(hào),文件名:行號(hào)

實(shí)驗(yàn)結(jié)果:

MapReduce在3.txt的第一行出現(xiàn)了兩次所以有兩個(gè)1

import java.io.*;
import java.util.StringTokenizer;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

public class MyMapper extends Mapper<Object,Text,Text,Text>{
    private Text keyInfo = new Text();
    private Text valueInfo = new Text();
    private FileSplit split;
    int num=0;

    public void map(Object key,Text value,Context context)
            throws IOException,InterruptedException{
        num++;
        split = (FileSplit)context.getInputSplit();
        StringTokenizer itr = new StringTokenizer(value.toString());
        while(itr.hasMoreTokens()){
            keyInfo.set(itr.nextToken()+" "+split.getPath().getName().toString());
            valueInfo.set(num+"");
            context.write(keyInfo,valueInfo);
        }
    }
}



import java.io.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Reducer;

public class MyCombiner extends Reducer<Text,Text,Text,Text>{

    private Text info = new Text();

    public void reduce(Text key,Iterable<Text>values,Context context)
            throws IOException, InterruptedException{
        String  sum = "";
        for(Text value:values){
            sum += value.toString()+" ";
        }

                String record = key.toString();
        String[] str = record.split(" ");

        key.set(str[0]);
        info.set(str[1]+":"+sum);
        context.write(key,info);
    }
}

import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class MyReducer extends Reducer<Text,Text,Text,Text>{
    private Text result = new Text();
    public void reduce(Text key,Iterable<Text>values,Context context) throws

            IOException, InterruptedException{
        String value =new String();
        for(Text value1:values){
            value += value1.toString()+" ; ";
        }
        result.set(value);
        context.write(key,result);
    }
}

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.util.Scanner;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import java.net.URI;

public class MyRunner {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);

        job.setJarByClass(MyRunner.class);

        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);
        job.setCombinerClass(MyCombiner.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);


        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        Scanner sc = new Scanner(System.in);
        System.out.print("inputPath:");
        String inputPath = sc.next();
        System.out.print("outputPath:");
        String outputPath = sc.next();

        try {
            FileSystem fs0 = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            Path hdfsPath = new Path(outputPath);
            if(fs0.delete(hdfsPath,true)){
                System.out.println("Directory "+ outputPath +" has been deleted successfully!");
            }
        }catch(Exception e) {
            e.printStackTrace();
        }

        FileInputFormat.setInputPaths(job, new Path("hdfs://master:9000"+inputPath));

        FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000"+outputPath));

        job.waitForCompletion(true);

        try {
            FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            Path srcPath = new Path(outputPath+"/part-r-00000");

            FSDataInputStream is = fs.open(srcPath);
            System.out.println("Results:");
            while(true) {
                String line = is.readLine();
                if(line == null) {
                    break;
                }
                System.out.println(line);
            }
            is.close();
        }catch(Exception e) {
            e.printStackTrace();
        }

    }
}

到此這篇關(guān)于云計(jì)算實(shí)驗(yàn):Java MapReduce編程的文章就介紹到這了,更多相關(guān)Java MapReduce編程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java自動(dòng)讀取指定文件夾下所有文件的方法

    Java自動(dòng)讀取指定文件夾下所有文件的方法

    這篇文章主要為大家詳細(xì)介紹了Java自動(dòng)讀取指定文件夾下所有文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Java 如何使用Feign發(fā)送HTTP請(qǐng)求

    Java 如何使用Feign發(fā)送HTTP請(qǐng)求

    這篇文章主要介紹了Java 如何使用Feign發(fā)送HTTP請(qǐng)求,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-11-11
  • springboot項(xiàng)目防止XSS攻擊和sql注入方式

    springboot項(xiàng)目防止XSS攻擊和sql注入方式

    這篇文章主要介紹了springboot項(xiàng)目防止XSS攻擊和sql注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Druid連接池未關(guān)閉導(dǎo)致內(nèi)存泄漏問題

    Druid連接池未關(guān)閉導(dǎo)致內(nèi)存泄漏問題

    這篇文章主要介紹了Druid連接池未關(guān)閉導(dǎo)致內(nèi)存泄漏問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • SparkSQl簡介及運(yùn)行原理

    SparkSQl簡介及運(yùn)行原理

    Spark SQL就是將SQL轉(zhuǎn)換成一個(gè)任務(wù),提交到集群上運(yùn)行,類似于Hive的執(zhí)行方式。今天通過本文給大家分享SparkSQl簡介及運(yùn)行原理,感興趣的朋友跟隨小編一起看看吧
    2021-08-08
  • Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL揭秘

    Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL揭秘

    這篇文章主要給大家介紹了關(guān)于Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL的相關(guān)資料,小編覺得挺不錯(cuò)的,對(duì)大家學(xué)習(xí)或者使用Mybatis會(huì)有一定的幫助,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • Java基礎(chǔ)之創(chuàng)建虛擬機(jī)對(duì)象的過程詳細(xì)總結(jié)

    Java基礎(chǔ)之創(chuàng)建虛擬機(jī)對(duì)象的過程詳細(xì)總結(jié)

    本文基于虛擬機(jī)HotSpot和常用的內(nèi)存區(qū)域Java堆深入對(duì)象分配、布局和訪問的全過程,文中有非常詳細(xì)的圖文解說,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • idea?springBoot項(xiàng)目自動(dòng)注入mapper為空?qǐng)?bào)錯(cuò)的解決方法

    idea?springBoot項(xiàng)目自動(dòng)注入mapper為空?qǐng)?bào)錯(cuò)的解決方法

    這篇文章主要介紹了idea?springBoot項(xiàng)目自動(dòng)注入mapper為空?qǐng)?bào)錯(cuò)的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • java字符緩沖流面試精講

    java字符緩沖流面試精講

    這篇文章主要為大家介紹了java中字符緩沖流面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Java中的反射機(jī)制基本運(yùn)用詳解

    Java中的反射機(jī)制基本運(yùn)用詳解

    這篇文章主要介紹了Java 反射機(jī)制原理與用法,結(jié)合實(shí)例形式詳細(xì)分析了Java反射機(jī)制的相關(guān)概念、原理、基本使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2021-08-08

最新評(píng)論