1. 程式人生 > >Hadoop詞頻統計(一)之叢集模式執行

Hadoop詞頻統計(一)之叢集模式執行

maven pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>HadoopStu</groupId>
	<artifactId>HadoopStu</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<resources>
			<resource>
				<directory>src</directory>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.6.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core -->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-core</artifactId>
			<version>1.2.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>

	</dependencies>
</project>


map:

package cn.hadoop.mr;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils;

public class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable>{

	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
			throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		String line = value.toString();
		
		String[] words = StringUtils.split(line,' ');
		
		for(String word : words) {
			context.write(new Text(word), new LongWritable(1));
		}
	}
}

reduce:
package cn.hadoop.mr;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
	@Override
	protected void reduce(Text key, Iterable<LongWritable> values,
			Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
		long count = 0;
		for(LongWritable value : values) {
			count += value.get();
		}
		context.write(key, new LongWritable(count));
	}
}

run:
package cn.hadoop.mr;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class WCRunner {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		
		Configuration conf = new Configuration();
		Job wcjob = Job.getInstance(conf);
		
		wcjob.setJarByClass(WCRunner.class);
		
		wcjob.setMapperClass(WCMapper.class);
		wcjob.setReducerClass(WCReducer.class);
		
		wcjob.setOutputKeyClass(Text.class);
		wcjob.setOutputValueClass(LongWritable.class);
		
		wcjob.setMapOutputKeyClass(Text.class);
		wcjob.setMapOutputValueClass(LongWritable.class);
		
		FileInputFormat.setInputPaths(wcjob, "/wc/inputdata/");
		FileOutputFormat.setOutputPath(wcjob, new Path("/output/"));
		
		wcjob.waitForCompletion(true);
	}
}
生成輸入資料:

[[email protected] ~]$ cat in.dat
haha lalala
hehe heiheihei
heiheihei lololo
lololo haha
haha haha
hehe lololo

在HDFS上建立相應路徑:
[[email protected] ~]$ hadoop fs -mkdir -p /wc/inputdata

將in.dat文字檔案上傳到HDFS上的相應路徑下:

[[email protected] ~]$ hadoop fs -put in.dat /wc/inputdata/

將上面的java程式打成jar包上傳伺服器,然後通過Hadoop呼叫:

hadoop jar mr.jar cn.hadoop.mr.WCRunner

[[email protected] ~]$ hadoop jar wc.jar  cn.hadoop.mr.WCRunner
16/07/25 15:25:05 INFO client.RMProxy: Connecting to ResourceManager at hadoop01/192.168.56.200:8032
16/07/25 15:25:06 WARN mapreduce.JobSubmitter: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
16/07/25 15:25:06 INFO input.FileInputFormat: Total input paths to process : 1
16/07/25 15:25:06 INFO mapreduce.JobSubmitter: number of splits:1
16/07/25 15:25:07 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1469431467769_0001
16/07/25 15:25:07 INFO impl.YarnClientImpl: Submitted application application_1469431467769_0001
16/07/25 15:25:07 INFO mapreduce.Job: The url to track the job: http://hadoop01:8088/proxy/application_1469431467769_0001/
16/07/25 15:25:07 INFO mapreduce.Job: Running job: job_1469431467769_0001
16/07/25 15:25:16 INFO mapreduce.Job: Job job_1469431467769_0001 running in uber mode : false
16/07/25 15:25:16 INFO mapreduce.Job:  map 0% reduce 0%
16/07/25 15:25:23 INFO mapreduce.Job:  map 100% reduce 0%
16/07/25 15:25:30 INFO mapreduce.Job:  map 100% reduce 100%
16/07/25 15:25:31 INFO mapreduce.Job: Job job_1469431467769_0001 completed successfully
16/07/25 15:25:31 INFO mapreduce.Job: Counters: 49
	File System Counters
		FILE: Number of bytes read=204
		FILE: Number of bytes written=211397
		FILE: Number of read operations=0
		FILE: Number of large read operations=0
		FILE: Number of write operations=0
		HDFS: Number of bytes read=183
		HDFS: Number of bytes written=44
		HDFS: Number of read operations=6
		HDFS: Number of large read operations=0
		HDFS: Number of write operations=2
	Job Counters 
		Launched map tasks=1
		Launched reduce tasks=1
		Data-local map tasks=1
		Total time spent by all maps in occupied slots (ms)=4219
		Total time spent by all reduces in occupied slots (ms)=4519
		Total time spent by all map tasks (ms)=4219
		Total time spent by all reduce tasks (ms)=4519
		Total vcore-seconds taken by all map tasks=4219
		Total vcore-seconds taken by all reduce tasks=4519
		Total megabyte-seconds taken by all map tasks=4320256
		Total megabyte-seconds taken by all reduce tasks=4627456
	Map-Reduce Framework
		Map input records=6
		Map output records=12
		Map output bytes=174
		Map output materialized bytes=204
		Input split bytes=105
		Combine input records=0
		Combine output records=0
		Reduce input groups=5
		Reduce shuffle bytes=204
		Reduce input records=12
		Reduce output records=5
		Spilled Records=24
		Shuffled Maps =1
		Failed Shuffles=0
		Merged Map outputs=1
		GC time elapsed (ms)=93
		CPU time spent (ms)=1100
		Physical memory (bytes) snapshot=348495872
		Virtual memory (bytes) snapshot=1864597504
		Total committed heap usage (bytes)=219480064
	Shuffle Errors
		BAD_ID=0
		CONNECTION=0
		IO_ERROR=0
		WRONG_LENGTH=0
		WRONG_MAP=0
		WRONG_REDUCE=0
	File Input Format Counters 
		Bytes Read=78
	File Output Format Counters 
		Bytes Written=44


輸出結果如下:

haha    4
hehe    2
heiheihei    2
lalala    1
lololo    3


相關推薦

Hadoop詞頻統計叢集模式執行

maven pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLo

Hadoop離線計算安裝並配置Hadoop

一、準備linux環境 1.0 配置好各虛擬機器的網路(採用NAT聯網模式) 第一種:通過Linux圖形介面進行修改(桌面版本Centos)進入Linux圖形介面 -> 右鍵點選右上方的兩個小電腦 -> 點選Edit connections->

Java進階 ——— Java多執行程序和執行

引言 講到執行緒,不可避免的提到程序。而因為執行緒無法脫離程序單獨存在,那什麼是程序? 延伸閱讀,Java多執行緒系列文章 什麼是程序? 程序:具有一定獨立功能的程式關於某個資料集合上的一次執行活動,程序是系統進行資源分配和排程的最小單位。 例如手機執行的眾多

Struts2 入門 控制器與執行步驟

Struts2是什麼? 是一個MVC框架。框架都是半成品。藉助框架可以提高開發效率。 Filter VS Servlet 過濾器要比Servlet要強大,開發中經常用Servlet作為控制器,Filter也可以作為控制器來使用。 public class ServletDemo implements Serv

python自然語言處理中文分詞預處理、統計詞頻

一個小的嘗試。。資料來源資料集 一共200條關於手機的中文評論,以XML格式儲存。分詞工具 python-jieba預處理包括去停用詞、去標點符號和數字去停用詞:使用的是他人總結的 停用詞表去標點符號和數字:用正則表示式。原本打算的是中文標點符號從網上覆制,英文標點符號用st

Hadoop基礎學習分析、編寫並執行WordCount詞頻統計程序

contains trace net tails super val 詞頻統計 上傳 str 版權聲明:本文為博主原創文章,未經博主同意不得轉載。 https://blog.csdn.ne

Hadoop初識大數據與Hadoop

hive 程序員 http 關系型數據庫 .com 邏輯 使用 alt clu 前言   從今天起,我將一步一步的分享大數據相關的知識,其實很多程序員感覺大數據很難學,其實並不是你想象的這樣,只要自己想學,還有什麽難得呢?   學習Hadoop有一個8020原則,80%

Hadoop初識大數據與Hadoop【轉載】

hba 無效 理解 組織 鼠標 掌握 能夠 through 2.3 原文地址:http://www.cnblogs.com/zhangyinhua/p/7647334.html 閱讀目錄(Content) 一、引言(大數據時代) 1.1、從數據中得到信息 1.2、大數據

7天hadoop學習虛擬機器的網路連線方式及linux的靜態ip設定

學習hadoop需要用到linux,所以視訊中講解了一些用到的linux知識。 如果通過虛擬機器進去linux系統一直讀進度條就是進不去主機頁面,有可能是網絡卡原因,那麼在虛擬機器設定選項選擇高階,重新生成MAC地址。 http://www.cnblogs.com/xi

統計】SQLcase when then else end用法(用於分類統計)

case具有兩種格式。簡單case函式和case搜尋函式。--簡單case函式 case sex when '1' then '男' when '2' then '女’ else '其他' end --case搜尋函式 case when sex = '1' th

地址映象和變換主存虛存

規則 根據 pan 命中率 實現 -s tro 分享 使用 地址映象:是將每一個虛存單元按某種規則裝入實存,即建立多用戶虛地址與實存地址之間的相應關系。 地址變換:是程序依照這樣的映象關系裝入實存後。在運行時,多用戶虛地址怎樣變換成相應的實存地址。 頁面爭用(實頁沖突

Hadoop自學筆記常見Hadoop相關項目一覽

-a https class Lucene 百萬 data fcm you 轉換 本自學筆記來自於Yutube上的視頻Hadoop系列。網址: https://www.youtube.com/watch?v=-TaAVaAwZTs(當中一個) 以後不再贅述 自學筆

web框架基礎簡介

-a 程序 我想 pos 客戶 創建 當前 自動 art http的請求聲明周期:域名----DNS服務器---IP地址---基於tcp協議的http協議發送請求協議,服務端返回響應頭+響應體(我們所看到的頁面(是經過js渲染的,接收的是字符串))服務端(web服務)根據我

android深入設計模式托付模式

-h listen back != new 聚合 string static data- (一)托付模式簡單介紹 托付模式是主要的設計模式之中的一個。托付。即是讓還有一個對象幫你做事情。 更多的模式,如狀態模式、策略模式、訪問者模式本質上是在更特殊的場合採用了托

數據庫中間件 Sharding-JDBC 源碼分析 —— SQL 解析語法解析

sharding-jdbc關註微信公眾號:【芋艿的後端小屋】有福利:RocketMQ / MyCAT / Sharding-JDBC 所有源碼分析文章列表RocketMQ / MyCAT / Sharding-JDBC 中文註釋源碼 GitHub 地址您對於源碼的疑問每條留言都將得到認真回復。甚至不知道如何讀

Linux I2C常用的幾種實例化(i2c_client ) 【轉】

掃描 sent near 通過 完成 check 根據 pup views 轉自:http://blog.csdn.net/lugandong/article/details/48092397 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 目錄(?)

數據結構鏈表

存儲 鏈表操作 author void 復雜 pac 部分 地址 插入 一、鏈表   鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。   鏈表由一系列結點(鏈表中每一個元素稱為結點)組成,結點可以在運行時動態生成。每個

tensorflow筆記基礎知識

輸入 gpu oat baidu nump 通過 img ubuntu下 能力 tensorflow筆記(一)之基礎知識 版權聲明:本文為博主原創文章,轉載請指明轉載地址 http://www.cnblogs.com/fydeblog/p/7399701.html 前言

JDBC細說JDBC

這一 操作 對應關系 rep throw tco 接口 nag cep Properties info = new Properties();//要參考數據庫文檔 info.setProperty("user", "root"); i

java學習——java基礎概念解析

userinfo shuf cdn pdm shu href ember sig lower 鵲拙崩系06凳q毫乙6http://docstore.docin.com/sina_6341933819 6j50uk佬詼4wn刮掖http://shequ.docin.com/