shell开发Hadoop streaming 实例代码
时间: 2023-10-04 21:12:38 浏览: 55
以下是一个使用Hadoop Streaming的Shell脚本示例代码:
```
#!/bin/bash
# 设置输入和输出目录
INPUT_DIR=/input
OUTPUT_DIR=/output
# 删除旧的输出目录
hadoop fs -rm -r $OUTPUT_DIR
# 运行Hadoop Streaming作业
hadoop jar /usr/local/hadoop/share/hadoop/tools/lib/hadoop-streaming-2.10.1.jar \
-input $INPUT_DIR \
-output $OUTPUT_DIR \
-mapper "map.sh" \
-reducer "reduce.sh" \
-file "map.sh" \
-file "reduce.sh"
# 打印输出
hadoop fs -cat $OUTPUT_DIR/*
```
其中,map.sh和reduce.sh是实际的Mapper和Reducer脚本。在这个例子中,我们使用了Hadoop默认的Streaming jar文件,并将map.sh和reduce.sh文件作为输入文件传递给它。在运行作业之前,我们删除了旧的输出目录(如果存在),以便Streaming作业可以重新生成输出。最后,我们使用hadoop fs -cat命令打印输出。
相关问题
执行Hadoop分布式实例详细代码分析
以下是一个简单的Hadoop分布式实例详细代码分析:
1. 配置Hadoop集群:
在多个服务器上安装Hadoop,并进行配置以创建一个Hadoop集群。这些服务器可以是物理服务器或虚拟机。
2. 准备数据:
将需要处理的数据上传到Hadoop集群中的HDFS(Hadoop分布式文件系统)。可以使用以下命令将本地文件上传到HDFS:
```
hdfs dfs -put local_file_path hdfs_file_path
```
3. 编写MapReduce程序:
编写MapReduce程序来处理上传到HDFS的数据。以下是一个简单的单词计数MapReduce程序实现:
```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;
public class WordCount {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
String[] words = value.toString().split(" ");
for (String w : words) {
word.set(w);
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 {
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 {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
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(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
```
这个MapReduce程序会对上传到HDFS的数据进行单词计数。其中,Mapper类用于将输入数据分割成单词,然后输出一个键-值对,其中键是单词,值为1。Reducer类用于将相同键的值相加,然后输出键-值对,其中键为单词,值为单词出现的次数。
4. 运行MapReduce程序:
将编写的MapReduce程序提交到Hadoop集群上运行。使用以下命令提交MapReduce程序:
```
hadoop jar jar_file_path input_path output_path
```
其中,jar_file_path是编译后的MapReduce程序的jar包路径;input_path是上传到HDFS的数据的路径;output_path是MapReduce程序输出结果的路径。
5. 获取结果:
查看MapReduce程序的运行结果,并将结果下载到本地计算机中进行分析。使用以下命令查看MapReduce程序的运行结果:
```
hdfs dfs -cat output_path/part-r-00000
```
其中,output_path是MapReduce程序输出结果的路径。
hadoop streaming测试网站
Hadoop streaming测试网站是一个在线的Hadoop流式处理测试工具,可以让开发人员在不需要安装Hadoop的情况下测试他们的MapReduce作业。以下是一些常用的Hadoop streaming测试网站:
1. Cloudera Hadoop Streaming Test: https://hadoop.apache.org/docs/stable/hadoop-streaming/HadoopStreaming.html
2. AWS EMR Hadoop Streaming Test: https://aws.amazon.com/emr/hadoop-streaming/
3. Qubole Hadoop Streaming Test: https://www.qubole.com/resources/hadoop-streaming/
4. Hortonworks Hadoop Streaming Test: https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.6.5/bk_command-line-installation/content/hadoop-streaming.html
5. Google Cloud Dataproc Hadoop Streaming Test: https://cloud.google.com/dataproc/docs/tutorials/streaming-example
这些网站提供了一个简单的界面,让用户上传他们的MapReduce代码和输入数据,并运行作业。它们还提供了一些示例代码和文档,以帮助用户了解Hadoop流式处理的基础知识。