一、實(shí)驗(yàn)?zāi)康?/h2>
- 通過實(shí)驗(yàn)掌握基本的MapReduce編程方法;
- 掌握用MapReduce解決一些常見的數(shù)據(jù)處理問題,包括數(shù)據(jù)去重、數(shù)據(jù)排序和數(shù)據(jù)挖掘等。
二、實(shí)驗(yàn)平臺(tái)
- 操作系統(tǒng):Linux(建議Ubuntu16.04或Ubuntu18.04)
- Hadoop版本:3.1.3
三、實(shí)驗(yàn)內(nèi)容
編程實(shí)現(xiàn)文件合并和去重操作
對于兩個(gè)輸入文件,即文件A和文件B,請編寫MapReduce程序,對兩個(gè)文件進(jìn)行合并,并剔除其中重復(fù)的內(nèi)容,得到一個(gè)新的輸出文件C。下面是輸入文件和輸出文件的一個(gè)樣例供參考。
輸入文件A的樣例如下:
20150101 x
20150102 y
20150103 x
20150104 y
20150105 z
20150106 x
輸入文件B的樣例如下:
20150101 y
20150102 y
20150103 x
20150104 z
20150105 y
根據(jù)輸入文件A和B合并得到的輸出文件C的樣例如下:
20150101 x
20150101 y
20150102 y
20150103 x
20150104 y
20150104 z
20150105 y
20150105 z
20150106 x
四、實(shí)驗(yàn)步驟
進(jìn)入 Hadoop 安裝目錄,啟動(dòng) hadoop:
cd /usr/local/hadoop
sbin/start-dfs.sh
新建文件夾,創(chuàng)建文件 A、B:
sudo mkdir MapReduce && cd MapReduce
sudo vim A
sudo vim B
編寫 Java 文件實(shí)現(xiàn) MapReduce:
sudo vim Merge.java
實(shí)現(xiàn)的 Java 代碼如下:
import java.io.IOException;
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 Merge {
/**
* @param args
* 對A,B兩個(gè)文件進(jìn)行合并,并剔除其中重復(fù)的內(nèi)容,得到一個(gè)新的輸出文件C
*/
//重載map函數(shù),直接將輸入中的value復(fù)制到輸出數(shù)據(jù)的key上
public static class Map extends Mapper<Object, Text, Text, Text>{
private static Text text = new Text();
public void map(Object key, Text value, Context context) throws IOException,InterruptedException{
text = value;
context.write(text, new Text(""));
}
}
//重載reduce函數(shù),直接將輸入中的key復(fù)制到輸出數(shù)據(jù)的key上
public static class Reduce extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key, Iterable<Text> values, Context context ) throws IOException,InterruptedException{
context.write(key, new Text(""));
}
}
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Configuration conf = new Configuration();
conf.set("fs.default.name","hdfs://localhost:9000");
String[] otherArgs = new String[]{"input","output"}; /* 直接設(shè)置輸入?yún)?shù) */
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in><out>");
System.exit(2);
}
Job job = Job.getInstance(conf,"Merge and duplicate removal");
job.setJarByClass(Merge.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
賦予用戶相關(guān)權(quán)限:
sudo chown -R hadoop /usr/local/hadoop
添加編譯所需要使用的 jar 包:
vim ~/.bashrc
添加下面一行到文件的最后:
export HADOOP_HOME=/usr/local/hadoop
export CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath):$CLASSPATH
使更改立即生效:
source ~/.bashrc
編譯 Merge.java:
javac Merge.java
打包生成的 class 文件為 jar 包:
jar -cvf Merge.jar *.class
創(chuàng)建 Hadoop 主目錄為 /user/hadoop 并創(chuàng)建 input 文件夾:
/usr/local/hadoop/bin/hdfs dfs -mkdir -p /user/hadoop
/usr/local/hadoop/bin/hdfs dfs -mkdir input
若 intput 已存在則刪除原有文件:
/usr/local/hadoop/bin/hdfs dfs -rm input/*
上傳 A、B 文件到 input 文件夾中:
/usr/local/hadoop/bin/hdfs dfs -put ./A input
/usr/local/hadoop/bin/hdfs dfs -put ./B input
使用之前確保 output 文件夾不存在:
/usr/local/hadoop/bin/hdfs dfs -rm -r output
使用我們剛生成的 Merge.jar 包:
/usr/local/hadoop/bin/hadoop jar Merge.jar Merge
查看輸出結(jié)果:
/usr/local/hadoop/bin/hdfs dfs -cat output/*
輸出如下:文章來源:http://www.zghlxwxcb.cn/news/detail-414185.html
hadoop@fzqs-Laptop:/usr/local/hadoop$ hdfs dfs -cat output/*
20170101 x
20170101 y
20170102 y
20170103 x
20170104 y
20170104 z
20170105 y
20170105 z
20170106 x
hadoop@fzqs-Laptop:/usr/local/hadoop$
此外,有想用 Python 寫的可以參考我這篇博客:實(shí)驗(yàn)5 MapReduce初級編程實(shí)踐(Python實(shí)現(xiàn))文章來源地址http://www.zghlxwxcb.cn/news/detail-414185.html
到了這里,關(guān)于實(shí)驗(yàn)5 MapReduce初級編程實(shí)踐(1)——編程實(shí)現(xiàn)文件合并和去重操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!