MapReduce:通过数据具有爷孙关系的结果-创新互联

1)启动环境

创新互联建站专注于中阳企业网站建设,自适应网站建设,商城开发。中阳网站建设公司,为中阳等地区提供建站服务。全流程专业公司,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务

 start-all.sh

2)产看状态

 jps

0613 NameNode

10733 DataNode

3455 NodeManager

15423 Jps

11082 ResourceManager

10913 SecondaryNameNode

3)利用Eclipse编写jar


    1.编写    MapCal类

package com.mp;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

public class MapCal extends Mapper {

@Override

protected void map(LongWritable lon, Text value, Context context)

throws IOException, InterruptedException {

String line = value.toString();

String[] peps = line.split("-");

// 键值对

context.write(new Text(peps[0]), new Text("s" + peps[1]));

context.write(new Text(peps[1]), new Text("g" + peps[0]));

}

}

    2.编写ReduceCal类

public class ReduceCal extends Reducer {

@Override

protected void reduce(Text arg0, Iterable arg1, Context context)

throws IOException, InterruptedException {

ArrayList grands = new ArrayList();

ArrayList sons = new ArrayList();

// 把这些值写入集合

for (Text text : arg1) {

String str = text.toString();

if (str.startsWith("g")) {

grands.add(text);

} else {

sons.add(text);

}

}

// 输出

for (int i = 0; i < sons.size(); i++) {

for (int j = 0; j < grands.size(); j++) {

context.write(grands.get(i), sons.get(j));

}

}

}

}

    3. 编写Jobrun类

public class RunJob {

// 全限定名

public static void main(String[] args) {

Configuration conf = new Configuration();

// 本地多线程模拟执行。

// conf.set("fs.defaultFS", "hdfs://node3:8020");

// conf.set("mapred.jar", "C:\\Users\\Administrator\\Desktop\\wc.jar");

try {

FileSystem fs = FileSystem.get(conf);

Job job = Job.getInstance(conf);

job.setJobName("wc");

job.setJarByClass(RunJob.class);

job.setMapperClass(WordCountMapper.class);

job.setReducerClass(WordCountReduce.class);

job.setMapOutputKeyClass(Text.class);

job.setMapOutputValueClass(IntWritable.class);

// job 输入数据和输出数据的目录

FileInputFormat.addInputPath(job, new Path("/word.txt"));

Path outPath = new Path("/output/wc2");// job执行结果存放的目录。该目录在执行前不能存在。

if (fs.exists(outPath)) {

fs.delete(outPath, true);

}

FileOutputFormat.setOutputPath(job, outPath);

boolean f = job.waitForCompletion(true);

if (f) {

System.out.println("任务执行成功!");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

4)导出jar包.

MapReduce :通过数据具有爷孙关系的结果

5)通过ftp上传jar到linux目录

6)运行jar包

 hadoop jar shuju.jar  com.mc.RunJob  /   /outg

7)如果map和reduce都100%

Shuffle Errors

BAD_ID=0

CONNECTION=0

IO_ERROR=0

WRONG_LENGTH=0

WRONG_MAP=0

WRONG_REDUCE=0

File Input Format Counters

Bytes Read=45

File Output Format Counters

Bytes Written=18

表示运行成功!!

8)产看结果

hadoop fs -tail  /outg/part-r-00000

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站题目:MapReduce:通过数据具有爷孙关系的结果-创新互联
URL地址:http://pwwzsj.com/article/dhisdh.html