欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Maven搭建Hadoop開發(fā)環(huán)境

 更新時(shí)間:2016年10月13日 11:47:58   作者:kongxx  
這篇文章主要介紹了使用Maven搭建Hadoop開發(fā)環(huán)境的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

復(fù)制代碼 代碼如下:
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. 測試

3.1 首先我們可以測試一下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 測試MapReduce作業(yè)

測試代碼比較簡單,如下:

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信息來統(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上

復(fù)制代碼 代碼如下:
$ bin/hdfs dfs -put /tmp/input /user/fkong/input

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

復(fù)制代碼 代碼如下:
$ bin/hadoop jar hadoopstudy-1.0-SNAPSHOT.jar my.hadoopstudy.mapreduce.EventCount /user/fkong/input /user/fkong/output

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

復(fù)制代碼 代碼如下:
$ bin/hdfs dfs -cat /user/fkong/output/part-r-00000

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java仿淘寶首頁分類列表功能的示例代碼

    Java仿淘寶首頁分類列表功能的示例代碼

    這篇文章主要介紹了仿淘寶分類管理功能的示例代碼,具有很好的參考價(jià)值,希望對大家有所幫助,也給大家做個(gè)參考
    2018-05-05
  • java單例模式實(shí)現(xiàn)面板切換

    java單例模式實(shí)現(xiàn)面板切換

    這篇文章主要為大家詳細(xì)介紹了java單例模式實(shí)現(xiàn)面板切換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • java 中 阻塞隊(duì)列BlockingQueue詳解及實(shí)例

    java 中 阻塞隊(duì)列BlockingQueue詳解及實(shí)例

    這篇文章主要介紹了java 中 阻塞隊(duì)列BlockingQueue詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • SpringBoot如何通過配置禁用swagger

    SpringBoot如何通過配置禁用swagger

    這篇文章主要給大家介紹了關(guān)于SpringBoot如何通過配置禁用swagger的相關(guān)資料,Swagger用來在開發(fā)階段方便前后端分離的項(xiàng)目實(shí)戰(zhàn)中,提高前后端人員的工作效率,降低交流成本,但是版本上線之后要是把Swagger帶上去會(huì)存在很大的風(fēng)險(xiǎn),需要的朋友可以參考下
    2023-08-08
  • mybaties?plus?selectMaps和selectList的區(qū)別說明

    mybaties?plus?selectMaps和selectList的區(qū)別說明

    這篇文章主要介紹了mybaties?plus?selectMaps和selectList的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 深入SQLite多線程的使用總結(jié)詳解

    深入SQLite多線程的使用總結(jié)詳解

    本篇文章是對SQLite多線程的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Java中終止線程的三種方法

    Java中終止線程的三種方法

    這篇文章主要為大家詳細(xì)介紹了Java中終止線程的三種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Hibernate5新特性介紹

    Hibernate5新特性介紹

    hibernate5中有了一些新的變動(dòng),下面腳本之家小編把Hibernate5新特性相關(guān)知識(shí),分享到腳本之家平臺(tái),感興趣的朋友參考下吧
    2017-09-09
  • 詳解Java 打印堆棧的幾種方法

    詳解Java 打印堆棧的幾種方法

    本篇文章主要介紹了Java 打印堆棧的幾種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • Java 高并發(fā)十: JDK8對并發(fā)的新支持詳解

    Java 高并發(fā)十: JDK8對并發(fā)的新支持詳解

    本文主要介紹Java 高并發(fā)JDK8的支持,這里整理了詳細(xì)的資料及1. LongAdder 2. CompletableFuture 3. StampedLock的介紹,有興趣的小伙伴可以參考下
    2016-09-09

最新評(píng)論