Hadoop云计算基础

第1关:WordCount词频统计
本关任务
对预设文本文档进行词频统计,并将最终结果输出。
注:输入和输出事先已经预定,只要比较输出是否达到预期即可判断是否达到要求。

代码部分

import java.io.IOException;

import java.util.StringTokenizer;

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 WordCount {

public static class TokenizerMapper

extends Mapper<Object, Text, Text, IntWritable>{

/*

*LongWritable,IntWritable,Text是Hadoop中实现的用于封装Java数据类型的类,这些类实现了WritableComparable接口,

*都能够被串行化,便于在分布式环境中进行数据交换,可以视为long,int,String数据类型的替代。

*/

private final static IntWritable one = new IntWritable(1);

private Text word = new Text();//Text实现了BinaryComparable类,可以作为key值

public void map(Object key, Text value, Context context

) throws IOException, InterruptedException {

/*********begin*********/

StringTokenizer itr = new StringTokenizer(value.toString());

while (itr.hasMoreTokens()) {

word.set(itr.nextToken());

context.write(word, one);

}

}

}

public static class IntSumReducer

extends Reducer<Text,IntWritable,Text,IntWritable> {

private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> values,

Context context

) throws IOException, InterruptedException {

//****请补全reduce对<k2, list(v2)> 进行合计得到list(<k3,v3>)过程****//

/*********begin*********/

int sum = 0;

for (IntWritable val : values) {

sum += val.get();

}

result.set(sum);

context.write(key, result);

}

}

public static void main(String[] args) throws Exception {

/**

* JobConf:map/reduce的job配置类,向hadoop框架描述map-reduce执行的工作

* 构造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等

*/

Configuration conf = new Configuration();

String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

/*

* 需要配置输入和输出的HDFS的文件路径参数

* 可以使用"Usage: wordcount <in> <out>"实现程序运行时动态指定输入输出

*/

if (otherArgs.length != 2) {

System.err.println("Usage: wordcount <in> <out>");

System.exit(2);

}

Job job = new Job(conf, "word count");//Job(Configuration conf,String jobName)设置job名称

job.setJarByClass(WordCount.class);//为job设置Mapper类

/*********begin*********/

//****请为job设置Mapper类****//

job.setJarByClass(WordCount.class);

job.setMapperClass(TokenizerMapper.class);

job.setCombinerClass(IntSumReducer.class);

job.setReducerClass(IntSumReducer.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(otherArgs[0]));

FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

System.exit(job.waitForCompletion(true) ? 0 : 1);

FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//为map-reduce任务设置InputFormat实现类,设置输入路径

FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//为map-reduce任务设置OutputFormat实现类,设置输出路径

System.exit(job.waitForCompletion(true) ? 0 : 1);

}

}

第2关:HDFS文件读写
本关任务
利用HDFS文件系统开放的API对HDFS系统进行文件的创建和读写
要求:

1.在HDFS的路径/user/hadoop/下新建文件myfile,并且写入内容“china cstor cstor cstor china”;
2.输出HDFS系统中刚写入的文件myfile的内容

代码部分

import java.io.IOException;

import java.sql.Date;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FSDataInputStream;

import org.apache.hadoop.fs.FSDataOutputStream;

import org.apache.hadoop.fs.FileStatus;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

public class hdfs {

public static void main(String[] args) throws IOException {

Configuration conf = new Configuration();

// 实例化文件系统

FileSystem fs = FileSystem.get(conf);

 

//*****请按照题目填写要创建的路径,其他路径及文件名无法被识别******//

Path file = new Path("/user/hadoop/myfile");

/*********end**********/

if (fs.exists(file)) {

System.out.println("File exists.");

} else

{

//****请补全使用文件流将字符写入文件过程,使用outStream.writeUTF()函数****//

/*********begin*********/

// 使用文件流将字符写入文件

FSDataOutputStream outStream = fs.create(file);

outStream.writeUTF("china cstor cstor cstor china");

outStream.close();

 

 

 

/*********end**********/

 

}

 

//****请补全读取文件内容****//

/*********begin*********/

// 提示:FSDataInputStream实现接口,使Hadoop中的文件输入流具有流式搜索和流式定位读取的功能

// 读取文件内容

FSDataInputStream inStream = fs.open(file);

String data = inStream.readUTF();

/*********end**********/

 

//输出文件状态

//FileStatus对象封装了文件的和目录的元数据,包括文件长度、块大小、权限等信息

FileSystem hdfs = file.getFileSystem(conf);

FileStatus[] fileStatus = hdfs.listStatus(file);

for(FileStatus status:fileStatus)

{

System.out.println("FileOwer:"+status.getOwner());//所有者

System.out.println("FileReplication:"+status.getReplication());//备份数

System.out.println("FileModificationTime:"+new Date(status.getModificationTime()));//目录修改时间

System.out.println("FileBlockSize:"+status.getBlockSize());//块大小

}

System.out.println(data);

System.out.println("Filename:"+file.getName());

inStream.close();

fs.close();

}

}

 

阅读剩余
THE END