有勇气的牛排博客

hadoop程序开发(Java)

有勇气的牛排 494 大数据 2022-12-04 17:26:04

1、创建maven项目

如果不懂配置maven请点击:传送门

image.png

image.png

2、在pom.xml写入架包配置文件

<dependencies> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-common --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-common</artifactId> <version>2.8.4</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.8.4</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.8.4</version> </dependency> </dependencies>

3、创建源程序

src–>main–>java–>com–>test–>WordCount.java

image.png

WordCount.java

/ ** *通过一项授权给Apache Software Foundation(ASF) *或更多贡献者许可协议。 查看公告文件 *随本作品分发以获取更多信息 *关于版权拥有权。 ASF许可此文件 *根据Apache许可2.0版(以下简称“ * “执照”); 除非合规,否则您不得使用此文件 *带许可证。 您可以在以下位置获得许可的副本: * * http://www.apache.org/licenses/LICENSE-2.0 * *除非适用法律要求或书面同意,否则软件 *根据许可协议分发的内容是按“原样”分发的, *不作任何明示或暗示的保证或条件。 *有关特定语言的管理权限,请参阅许可证 *许可中的限制。 * / package com.xxx; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapred.FileInputFormat; import org.apache.hadoop.mapred.FileOutputFormat; import org.apache.hadoop.mapred.JobClient; import org.apache.hadoop.mapred.JobConf; import org.apache.hadoop.mapred.MapReduceBase; import org.apache.hadoop.mapred.Mapper; import org.apache.hadoop.mapred.OutputCollector; import org.apache.hadoop.mapred.Reducer; import org.apache.hadoop.mapred.Reporter; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; /** * 这是一个示例Hadoop Map / Reduce应用程序。 * 读取文本输入文件,将每一行分解为单词 * 并计数。 输出是单词的本地排序列表,并且 * 计算它们发生的频率。 * * 运行:bin / hadoop jar build / hadoop-examples.jar wordcount * [-m <i>地图</ i>] [-r <i>减少</ i>] <i>目录内</ i> <i>目录外</ i> */ public class WordCount extends Configured implements Tool { /** * 计算每一行中的单词。 * 于输入的每一行,将其分解为单词并将其作为 * <b>单词</ b>,<b> 1 </ b>)。 */ public static class MapClass extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { String line = value.toString(); StringTokenizer itr = new StringTokenizer(line); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); output.collect(word, one); } } } /** * 一个reducer类,该类仅发出输入值的总和。 */ public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { int sum = 0; while (values.hasNext()) { sum += values.next().get(); } output.collect(key, new IntWritable(sum)); } } static int printUsage() { System.out.println("wordcount [-m <maps>] [-r <reduces>] <input> <output>"); ToolRunner.printGenericCommandUsage(System.out); return -1; } /** * 字数映射/减少程序的主要驱动程序。 * 调用此方法以提交地图/缩小作业。 * @throws When there is communication problems with the job tracker. */ public int run(String[] args) throws Exception { JobConf conf = new JobConf(getConf(), WordCount.class); conf.setJobName("wordcount"); // the keys are words (strings) conf.setOutputKeyClass(Text.class); // the values are counts (ints) conf.setOutputValueClass(IntWritable.class); conf.setMapperClass(MapClass.class); conf.setCombinerClass(Reduce.class); conf.setReducerClass(Reduce.class); List<String> other_args = new ArrayList<String>(); for(int i=0; i < args.length; ++i) { try { if ("-m".equals(args[i])) { conf.setNumMapTasks(Integer.parseInt(args[++i])); } else if ("-r".equals(args[i])) { conf.setNumReduceTasks(Integer.parseInt(args[++i])); } else { other_args.add(args[i]); } } catch (NumberFormatException except) { System.out.println("ERROR: Integer expected instead of " + args[i]); return printUsage(); } catch (ArrayIndexOutOfBoundsException except) { System.out.println("ERROR: Required parameter missing from " + args[i-1]); return printUsage(); } } // Make sure there are exactly 2 parameters left. if (other_args.size() != 2) { System.out.println("ERROR: Wrong number of parameters: " + other_args.size() + " instead of 2."); return printUsage(); } FileInputFormat.setInputPaths(conf, other_args.get(0)); FileOutputFormat.setOutputPath(conf, new Path(other_args.get(1))); JobClient.runJob(conf); return 0; } public static void main(String[] args) throws Exception { int res = ToolRunner.run(new Configuration(), new WordCount(), args); System.exit(res); } }

4、将WordCount.java 打包为jar文件

(1)基本配置

image.png

image.png

选择完后 Apply–>ok

(2)开始打包

Build–>Build Artifacts–> XXX.jar–> Build

image.png

(3)查看生成的jar文件

在文件夹 out–>artifacts–>WordCount_jar里面

5、运行

我这里将WordCount.jar 上传到 /usr/local/hadoop-jar 目录下了
运行命令

重要:程序名前一定要写 包名 这里是 com.test

yarn jar /usr/local/hadoop-jar/WordCount.jar com.test.WordCount /input/word.txt /output/01

6、结束

示例结束,如果想开发其他程序,可以自己另外编写java 文件,打包上传运行即可。
如有转载请标明出处,支持原创。


留言

专栏
文章
加入群聊