溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

hadoop MR maven級(jí)代碼模板是怎樣的

發(fā)布時(shí)間:2021-10-14 14:36:43 來(lái)源:億速云 閱讀:98 作者:柒染 欄目:編程語(yǔ)言

本篇文章給大家分享的是有關(guān)hadoop MR maven級(jí)代碼模板是怎樣的,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

關(guān)于Maven的使用就不再啰嗦了,網(wǎng)上很多,并且這么多年變化也不大,這里僅介紹怎么搭建Hadoop的開發(fā)環(huán)境。

1. 首先創(chuàng)建工程

mvn archetype:generate -DgroupId=my.hadoopstudy -DartifactId=hadoopstudy -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2. 然后在pom.xml文件里添加hadoop的依賴包hadoop-common, hadoop-client, hadoop-hdfs,添加后的pom.xml文件如下

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>my.hadoopstudy</groupId>  <artifactId>hadoopstudy</artifactId>  <packaging>jar</packaging>  <version>1.0-SNAPSHOT</version>  <name>hadoopstudy</name>  <url>http://maven.apache.org</url>  <dependencies>    <dependency>      <groupId>org.apache.hadoop</groupId>      <artifactId>hadoop-common</artifactId>      <version>2.5.1</version>    </dependency>    <dependency>      <groupId>org.apache.hadoop</groupId>      <artifactId>hadoop-hdfs</artifactId>      <version>2.5.1</version>    </dependency>    <dependency>      <groupId>org.apache.hadoop</groupId>      <artifactId>hadoop-client</artifactId>      <version>2.5.1</version>    </dependency>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>  </dependencies></project>

3. 測(cè)試3.1 首先我們可以測(cè)試一下hdfs的開發(fā),這里假定使用上一篇Hadoop文章中的hadoop集群,類代碼如下

package my.hadoopstudy.dfs;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;import java.io.InputStream;import java.net.URI;public class Test {  public static void main(String[] args) throws Exception {    String uri = "hdfs://9.111.254.189:9000/";    Configuration config = new Configuration();    FileSystem fs = FileSystem.get(URI.create(uri), config);    // 列出hdfs上/user/fkong/目錄下的所有文件和目錄    FileStatus[] statuses = fs.listStatus(new Path("/user/fkong"));    for (FileStatus status : statuses) {      System.out.println(status);    }    // 在hdfs的/user/fkong目錄下創(chuàng)建一個(gè)文件,并寫入一行文本    FSDataOutputStream os = fs.create(new Path("/user/fkong/test.log"));    os.write("Hello World!".getBytes());    os.flush();    os.close();    // 顯示在hdfs的/user/fkong下指定文件的內(nèi)容    InputStream is = fs.open(new Path("/user/fkong/test.log"));    IOUtils.copyBytes(is, System.out, 1024, true);  }
}

3.2 測(cè)試MapReduce作業(yè)測(cè)試代碼比較簡(jiǎn)單,如下:

package my.hadoopstudy.mapreduce;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;import java.io.IOException;public class EventCount {  public static class MyMapper extends Mapper<Object, Text, Text, IntWritable>{    private final static IntWritable one = new IntWritable(1);    private Text event = new Text();    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {      int idx = value.toString().indexOf(" ");      if (idx > 0) {        String e = value.toString().substring(0, idx);        event.set(e);        context.write(event, one);      }    }  }  public static class MyReducer 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();    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();    if (otherArgs.length < 2) {      System.err.println("Usage: EventCount <in> <out>");      System.exit(2);    }    Job job = Job.getInstance(conf, "event count");    job.setJarByClass(EventCount.class);    job.setMapperClass(MyMapper.class);    job.setCombinerClass(MyReducer.class);    job.setReducerClass(MyReducer.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);  }
}

運(yùn)行“mvn package”命令產(chǎn)生jar包hadoopstudy-1.0-SNAPSHOT.jar,并將jar文件復(fù)制到hadoop安裝目錄下

這里假定我們需要分析幾個(gè)日志文件中的Event信息來(lái)統(tǒng)計(jì)各種Event個(gè)數(shù),所以創(chuàng)建一下目錄和文件

/tmp/input/event.log.1/tmp/input/event.log.2/tmp/input/event.log.3

因?yàn)檫@里只是要做一個(gè)列子,所以每個(gè)文件內(nèi)容可以都一樣,假如內(nèi)容如下

JOB_NEW ...
JOB_NEW ...
JOB_FINISH ...
JOB_NEW ...
JOB_FINISH ...

然后把這些文件復(fù)制到HDFS上

$ bin/hdfs dfs -put /tmp/input /user/fkong/input

運(yùn)行mapreduce作業(yè)

$ bin/hadoop jar hadoopstudy-1.0-SNAPSHOT.jar my.hadoopstudy.mapreduce.EventCount /user/fkong/input /user/fkong/output

查看執(zhí)行結(jié)果

$ bin/hdfs dfs -cat /user/fkong/output/part-r-00000

以上就是hadoop MR maven級(jí)代碼模板是怎樣的,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI