最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

hadoop的wordcount實(shí)例代碼

 更新時(shí)間:2018年02月03日 10:45:58   作者:杭州胡欣  
這篇文章主要介紹了hadoop的wordcount實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

可以通過(guò)一個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明MapReduce到底是什么:

  我們要統(tǒng)計(jì)一個(gè)大文件中的各個(gè)單詞出現(xiàn)的次數(shù)。由于文件太大。我們把這個(gè)文件切分成如果小文件,然后安排多個(gè)人去統(tǒng)計(jì)。這個(gè)過(guò)程就是”Map”。然后把每個(gè)人統(tǒng)計(jì)的數(shù)字合并起來(lái),這個(gè)就是“Reduce"。

  上面的例子如果在MapReduce去做呢,就需要?jiǎng)?chuàng)建一個(gè)任務(wù)job,由job把文件切分成若干獨(dú)立的數(shù)據(jù)塊,并分布在不同的機(jī)器節(jié)點(diǎn)中。然后通過(guò)分散在不同節(jié)點(diǎn)中的Map任務(wù)以完全并行的方式進(jìn)行處理。MapReduce會(huì)對(duì)Map的輸出地行收集,再將結(jié)果輸出送給Reduce進(jìn)行下一步的處理。

  對(duì)于一個(gè)任務(wù)的具體執(zhí)行過(guò)程,會(huì)有一個(gè)名為"JobTracker"的進(jìn)程負(fù)責(zé)協(xié)調(diào)MapReduce執(zhí)行過(guò)程中的所有任務(wù)。若干條TaskTracker進(jìn)程用來(lái)運(yùn)行單獨(dú)的Map任務(wù),并隨時(shí)將任務(wù)的執(zhí)行情況匯報(bào)給JobTracker。如果一個(gè)TaskTracker匯報(bào)任務(wù)失敗或者長(zhǎng)時(shí)間未對(duì)本身任務(wù)進(jìn)行匯報(bào),JobTracker會(huì)啟動(dòng)另外一個(gè)TaskTracker重新執(zhí)行單獨(dú)的Map任務(wù)。

下面的具體的代碼實(shí)現(xiàn):

1. 編寫wordcount的相關(guān)job

(1)eclipse下創(chuàng)建相關(guān)maven項(xiàng)目,依賴jar包如下(也可參照hadoop源碼包下的hadoop-mapreduce-examples項(xiàng)目的pom配置)

  注意:要配置一個(gè)maven插件maven-jar-plugin,并指定mainClass

<dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.11</version>
  </dependency>
  <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-mapreduce-client-core</artifactId>
    <version>2.5.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>2.5.2</version>
  </dependency>
 </dependencies>
 
 <build>
   <plugins>
     <plugin>
  <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <configuration>
    <archive>
     <manifest>
      <mainClass>com.xxx.demo.hadoop.wordcount.WordCount</mainClass>
     </manifest>
    </archive>
   </configuration>
  </plugin>
   </plugins>
 </build>

(2)根據(jù)MapReduce的運(yùn)行機(jī)制,一個(gè)job至少要編寫三個(gè)類分別用來(lái)完成Map邏輯、Reduce邏輯、作業(yè)調(diào)度這三件事。

Map的代碼可繼承org.apache.hadoop.mapreduce.Mapper類

public static class TokenizerMapper
    extends Mapper<Object, Text, Text, IntWritable>{
 
  private final static IntWritable one = new IntWritable(1);
  private Text word = new Text();
   //由于該例子未用到key的參數(shù),所以該處key的類型就簡(jiǎn)單指定為Object
  public void map(Object key, Text value, Context context
          ) throws IOException, InterruptedException {
   StringTokenizer itr = new StringTokenizer(value.toString());
   while (itr.hasMoreTokens()) {
    word.set(itr.nextToken());
    context.write(word, one);
   }
  }
 }

Reduce的代碼可繼承org.apache.hadoop.mapreduce.Reducer類

public 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);
  }
 }

編寫main方法進(jìn)行作業(yè)調(diào)度

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]));
  job.waitForCompletion(true) ;
  //System.exit(job.waitForCompletion(true) ? 0 : 1);
 }

2. 上傳數(shù)據(jù)文件到hadoop集群環(huán)境

執(zhí)行mvn install把項(xiàng)目打成jar文件然后上傳到linux集群環(huán)境,使用hdfs dfs -mkdir命令在hdfs文件系統(tǒng)中創(chuàng)建相應(yīng)的命令,使用hdfs dfs -put 把需要處理的數(shù)據(jù)文件上傳到hdfs系統(tǒng)中,示例:hdfs dfs -put ${linux_path/數(shù)據(jù)文件} ${hdfs_path}

3. 執(zhí)行job

在集群環(huán)境中執(zhí)行命令: hadoop jar ${linux_path}/wordcount.jar ${hdfs_input_path} ${hdfs_output_path}

4. 查看統(tǒng)計(jì)結(jié)果

hdfs dfs -cat ${hdfs_output_path}/輸出文件名

以上的方式在未啟動(dòng)hadoop集群環(huán)境時(shí),是以Local模式運(yùn)行,此時(shí)HDFS和YARN都不起作用。下面是在偽分布式模式下執(zhí)行mapreduce job時(shí)需要做的工作,先把官網(wǎng)上列的步驟摘錄出來(lái):

配置主機(jī)名

# vi /etc/sysconfig/network

例如:

NETWORKING=yes
HOSTNAME=master


vi /etc/hosts

填入以下內(nèi)容

127.0.0.1 localhost

配置ssh免密碼互通

ssh-keygen -t rsa
# cat?~/.ssh/id_rsa.pub?>>?~/.ssh/authorized_keys

配置core-site.xml文件(位于${HADOOP_HOME}/etc/hadoop/

<configuration>
  <property>
    <name>fs.defaultFS</name>
    <value>hdfs://localhost:9000</value>
  </property>
</configuration>

配置hdfs-site.xml文件

<configuration>
  <property>
    <name>dfs.replication</name>
    <value>1</value>
  </property>
</configuration>

下面的命令可以在單機(jī)偽分布模式下運(yùn)行mapreduce的job

1.Format the filesystem:
$ bin/hdfs namenode -format
2.Start NameNode daemon and DataNode daemon:
$ sbin/start-dfs.sh
3.The hadoop daemon log output is written to the $HADOOP_LOG_DIR directory (defaults to $HADOOP_HOME/logs).

4.Browse the web interface for the NameNode; by default it is available at:
NameNode - http://localhost:50070/
Make the HDFS directories required to execute MapReduce jobs:
$ bin/hdfs dfs -mkdir /user
$ bin/hdfs dfs -mkdir /user/<username>
5.Copy the input files into the distributed filesystem:
$ bin/hdfs dfs -put etc/hadoop input
6.Run some of the examples provided:
$ bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.2.jar grep input output 'dfs[a-z.]+'
7.Examine the output files:
Copy the output files from the distributed filesystem to the local filesystem and examine them:

$ bin/hdfs dfs -get output output
$ cat output/*
or

View the output files on the distributed filesystem:

$ bin/hdfs dfs -cat output/*
8.When you're done, stop the daemons with:
$ sbin/stop-dfs.sh

總結(jié)

以上就是本文關(guān)于hadoop的wordcount實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • SpringBoot統(tǒng)一api返回風(fēng)格的實(shí)現(xiàn)

    SpringBoot統(tǒng)一api返回風(fēng)格的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot統(tǒng)一api返回風(fēng)格的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Spring WebSocket 404錯(cuò)誤的解決方法

    Spring WebSocket 404錯(cuò)誤的解決方法

    這篇文章主要為大家詳細(xì)介紹了Spring WebSocket 404錯(cuò)誤的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Java Spring開發(fā)環(huán)境搭建及簡(jiǎn)單入門示例教程

    Java Spring開發(fā)環(huán)境搭建及簡(jiǎn)單入門示例教程

    這篇文章主要介紹了Java Spring開發(fā)環(huán)境搭建及簡(jiǎn)單入門示例,結(jié)合實(shí)例形式分析了spring環(huán)境搭建、配置、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-11-11
  • java高并發(fā)的volatile與Java內(nèi)存模型詳解

    java高并發(fā)的volatile與Java內(nèi)存模型詳解

    這篇文章主要介紹了java高并發(fā)的volatile與Java內(nèi)存模型,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-10-10
  • 關(guān)于接口ApplicationContext中的getBean()方法使用

    關(guān)于接口ApplicationContext中的getBean()方法使用

    這篇文章主要介紹了關(guān)于接口ApplicationContext中的getBean()方法使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Java中實(shí)現(xiàn)高清圖片壓縮的兩種方案(最新推薦)

    Java中實(shí)現(xiàn)高清圖片壓縮的兩種方案(最新推薦)

    文章首先介紹了Java中進(jìn)行高清圖片壓縮的基本方法,包括使用Java標(biāo)準(zhǔn)庫(kù)ImageIO和第三方庫(kù)ApacheCommonsCompress,通過(guò)示例代碼展示了如何調(diào)整圖像質(zhì)量和使用第三方工具來(lái)壓縮圖片文件,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • spring boot udp或者tcp接收數(shù)據(jù)的實(shí)例詳解

    spring boot udp或者tcp接收數(shù)據(jù)的實(shí)例詳解

    這篇文章主要介紹了spring boot udp或者tcp接收數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • Java中的LinkedHashMap源碼詳解

    Java中的LinkedHashMap源碼詳解

    這篇文章主要介紹了Java中的LinkedHashMap源碼詳解,LinkedHashMap的實(shí)現(xiàn)方式是將所有的Entry節(jié)點(diǎn)鏈入一個(gè)雙向鏈表,并且它的底層數(shù)據(jù)結(jié)構(gòu)是HashMap,因此,LinkedHashMap具有HashMap的所有特性,但在存取元素的細(xì)節(jié)實(shí)現(xiàn)上有所不同,需要的朋友可以參考下
    2023-09-09
  • Java雜談之代碼重構(gòu)的方法多長(zhǎng)才算長(zhǎng)

    Java雜談之代碼重構(gòu)的方法多長(zhǎng)才算長(zhǎng)

    關(guān)于代碼重構(gòu)的理解:在不改變軟件系統(tǒng)/模塊所具備的功能特性的前提下,遵循/利用某種規(guī)則,使其內(nèi)部結(jié)構(gòu)趨于完善。其在軟件生命周期中的價(jià)值體現(xiàn)主要在于可維護(hù)性和可擴(kuò)展性
    2021-10-10
  • springboot2 生產(chǎn)部署注意事項(xiàng)及示例代碼

    springboot2 生產(chǎn)部署注意事項(xiàng)及示例代碼

    這篇文章主要介紹了springboot2 生產(chǎn)部署注意事項(xiàng)及示例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04

最新評(píng)論

南皮县| 钟山县| 女性| 响水县| 达拉特旗| 连州市| 麻江县| 宾阳县| 吴川市| 淄博市| 信宜市| 榆中县| 韶关市| 互助| 竹北市| 东方市| 宝应县| 西宁市| 珠海市| 天津市| 玛沁县| 启东市| 南昌县| 平安县| 晋城| 高安市| 横峰县| 晋江市| 新乡市| 黄山市| 肥东县| 宝山区| 曲阳县| 本溪| 金沙县| 浦北县| 墨竹工卡县| 和林格尔县| 龙江县| 乌苏市| 忻州市|