1. 程式人生 > >IDEA下新建一個gradle工程,用kotlin寫MapReduce。

IDEA下新建一個gradle工程,用kotlin寫MapReduce。

話不多說,直接上正餐。

首先我們新建一個gradle的專案,把預設的Java前面的勾去掉,然後再kotlin前面打鉤後Next:


接著設定對應的groupId和artifactId,然後Next:


接著一直Next(當然要有什麼其他的需求配置就自己選擇操作):


在這邊確認專案名稱之後直接finish,然後專案建成,接著讓子彈飛一會兒……

接著開啟build.gradle檔案,開始配置gradle專案,在dependencies下加入配置:


接著為了後續打包方便,我們再加入下列的配置:

def appMainClass = 'com.ktdemo.TempMainKt'
apply plugin: 'application'
mainClassName = appMainClass

配置完成之後的配置檔案截圖如下:


其中的appMainClass是指啟動類。後續我們要新建一個對應的main類。然後點選箭頭所提示的地方,讓gradle自動下載依賴包。



接著在新的專案下,kotlin目錄中建立新的package:


新建一個TempMapper的kotlin類,程式碼如下:

package com.ktdemo

import org.apache.hadoop.io.IntWritable
import org.apache.hadoop.io.LongWritable
import org.apache.hadoop.io.Text
import org.apache.hadoop.mapred.JobConf
import org.apache.hadoop.mapred.OutputCollector
import org.apache.hadoop.mapred.Reporter
import org.apache.hadoop.mapreduce.Mapper

class TempMapper : Mapper<LongWritable, Text, Text, IntWritable>() {

    override fun map(key: LongWritable?, value: Text?, context: Context) {
        print("Before Mapper:" + key + ", " + value)
        var line : String = value.toString()
        var year : String = line.substring(0, 4)
        var temperature : Int = line.substring(8).toInt()
        context.write(Text(year), IntWritable(temperature))
        System.out.println("After Mapper:" + Text(year) + ", " + IntWritable(temperature));
    }
}
新建一個TempReducer的kotlin類,程式碼如下:
import org.apache.hadoop.io.IntWritable
import org.apache.hadoop.io.Text
import org.apache.hadoop.mapred.OutputCollector
import org.apache.hadoop.mapred.Reporter
import org.apache.hadoop.mapreduce.Reducer

class TempReducer : Reducer<Text, IntWritable, Text, IntWritable>() {
    override fun reduce(key: Text?, values: Iterable<IntWritable>?, context: Context?) {
        var maxValue : Int = Int.MIN_VALUE
        var sb : StringBuffer = StringBuffer()
        //取values的最大值
        if (values != null) {
            for (value in values) {
                maxValue = Math.max(maxValue, value.get())
                sb.append(value).append(", ")
            }
        }
        print("Before Reduce:" + key + ", " + sb.toString())
        if (context != null) {
            context.write(key, IntWritable(maxValue))
        }
        print("After Reduce:" + key + ", " + maxValue)
    }
}

最後我們新建一個main啟動類,程式碼如下:

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.lib.input.FileInputFormat
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat

fun resource(name: String) = TempMain::class.java.getResource(name)

object TempMain{
    fun run (input : String?, outPut : String?) {
        var hadoopConfig : Configuration = Configuration()

        hadoopConfig.set("fs.hdfs.impl",
                org.apache.hadoop.hdfs.DistributedFileSystem::class.java.name)
        hadoopConfig.set("fs.file.impl",
                org.apache.hadoop.fs.LocalFileSystem::class.java.name)
        //Hadoop的配置檔案
        hadoopConfig.addResource(resource("/hdfs-conf/core-site.xml"))
        hadoopConfig.addResource(resource("/hdfs-conf/hdfs-site.xml"))

        var job : Job = Job(hadoopConfig)

        //如果需要打成jar執行,需要下面這句
        job.setJarByClass(TempMain::class.java)

        //job執行作業時輸入和輸出檔案的路徑
        FileInputFormat.addInputPath(job, Path(input))
        FileOutputFormat.setOutputPath(job, Path(outPut))

        //指定自定義的Mapper和Reducer作為兩個階段的任務處理類
        job.mapperClass = TempMapper::class.java
        job.reducerClass = TempReducer::class.java

        //設定最後輸出結果的Key和Value的型別
        job.outputKeyClass = Text::class.java
        job.outputValueClass = IntWritable::class.java

        //執行job,直到完成
        job.waitForCompletion(true)
        print("Finished")
    }
}

fun main(args: Array<String>) {
    //輸入路徑 這邊的IP是Hadoop的Master地址
    val dst = "hdfs://172.16.134.251:9000/test/input.txt"
    //輸出路徑
    val dstOut = "hdfs://172.16.134.251:9000/test/output3"
    TempMain.run(dst, dstOut)
}

其中,有引入Hadoop的配置檔案。這邊直接把Hadoop的conf下對應的配置檔案複製過來即可。


完成之後,使用gradle把專案打成tar包。


這個時候就專案build目錄下就可以看到對應的檔案。


接下來把打好的tar包,傳到Hadoop的虛擬機器中,並且把需要計算的資料來源檔案也準備好。

2014010114
2014010216
2014010317
2014010410
2014010506
2012010609
2012010732
2012010812
2012010919
2012011023
2001010116
2001010212
2001010310
2001010411
2001010529
2013010619
2013010722
2013010812
2013010929
2013011023
2008010105
2008010216
2008010337
2008010414
2008010516
2007010619
2007010712
2007010812
2007010999
2007011023
2010010114
2010010216
2010010317
2010010410
2010010506
2015010649
2015010722
2015010812
2015010999
2015011023

把上面的資料用vi命令新建成一個input.txt的檔案。然後這個時候我們在虛擬機器中看到的應該是如下的介面:


因為MapReduce在計算的時候是取hdfs裡的資源,所以我們必須先把input.txt放入到hdfs中。具體命令如下:

hadoop fs -mkdir /test #新建一個test的目錄
hadoop fs -put /home/hadoop/runfile/input.txt /test/ #把對應的檔案傳入到hdfs裡面的test目錄下
hadoop fs -ls /test #查詢對應目錄的資訊

這個時候查詢出來就應該會顯示對應的input.txt已經在hdfs裡面了。


當然也可以直接從瀏覽器下檢視對應的檔案是否已經傳到hdfs裡。


完成資料檔案的準備後,把剛剛傳上去的專案用tar命令解壓一下:

tar -xvf ktdemo-1.0-SNAPSHOT.tar 

解壓完成之後我們進入的到專案路徑下,在bin目錄中有一下兩個執行檔案。這邊直接使用下面的命令執行即可:

./ktdemo

因為我們已經把原本的一些引數寫在了專案當中,所以這邊就無需再給出引數了。執行結果下顯示:

[[email protected] bin]$ ./ktdemo
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Before Mapper:0, 2014010114After Mapper:2014, 14
Before Mapper:11, 2014010216After Mapper:2014, 16
Before Mapper:22, 2014010317After Mapper:2014, 17
Before Mapper:33, 2014010410After Mapper:2014, 10
Before Mapper:44, 2014010506After Mapper:2014, 6
Before Mapper:55, 2012010609After Mapper:2012, 9
Before Mapper:66, 2012010732After Mapper:2012, 32
Before Mapper:77, 2012010812After Mapper:2012, 12
Before Mapper:88, 2012010919After Mapper:2012, 19
Before Mapper:99, 2012011023After Mapper:2012, 23
Before Mapper:110, 2001010116After Mapper:2001, 16
Before Mapper:121, 2001010212After Mapper:2001, 12
Before Mapper:132, 2001010310After Mapper:2001, 10
Before Mapper:143, 2001010411After Mapper:2001, 11
Before Mapper:154, 2001010529After Mapper:2001, 29
Before Mapper:165, 2013010619After Mapper:2013, 19
Before Mapper:176, 2013010722After Mapper:2013, 22
Before Mapper:187, 2013010812After Mapper:2013, 12
Before Mapper:198, 2013010929After Mapper:2013, 29
Before Mapper:209, 2013011023After Mapper:2013, 23
Before Mapper:220, 2008010105After Mapper:2008, 5
Before Mapper:231, 2008010216After Mapper:2008, 16
Before Mapper:242, 2008010337After Mapper:2008, 37
Before Mapper:253, 2008010414After Mapper:2008, 14
Before Mapper:264, 2008010516After Mapper:2008, 16
Before Mapper:275, 2007010619After Mapper:2007, 19
Before Mapper:286, 2007010712After Mapper:2007, 12
Before Mapper:297, 2007010812After Mapper:2007, 12
Before Mapper:308, 2007010999After Mapper:2007, 99
Before Mapper:319, 2007011023After Mapper:2007, 23
Before Mapper:330, 2010010114After Mapper:2010, 14
Before Mapper:341, 2010010216After Mapper:2010, 16
Before Mapper:352, 2010010317After Mapper:2010, 17
Before Mapper:363, 2010010410After Mapper:2010, 10
Before Mapper:374, 2010010506After Mapper:2010, 6
Before Mapper:385, 2015010649After Mapper:2015, 49
Before Mapper:396, 2015010722After Mapper:2015, 22
Before Mapper:407, 2015010812After Mapper:2015, 12
Before Mapper:418, 2015010999After Mapper:2015, 99
Before Mapper:429, 2015011023After Mapper:2015, 23
Before Reduce:2001, 12, 10, 11, 29, 16, After Reduce:2001, 29Before Reduce:2007, 23, 19, 12, 12, 99, After Reduce:2007, 99Before Reduce:2008, 16, 14, 37, 16, 5, After Reduce:2008, 37Before Reduce:2010, 10, 6, 14, 16, 17, After Reduce:2010, 17Before Reduce:2012, 19, 12, 32, 9, 23, After Reduce:2012, 32Before Reduce:2013, 23, 29, 12, 22, 19, After Reduce:2013, 29Before Reduce:2014, 14, 6, 10, 17, 16, After Reduce:2014, 17Before Reduce:2015, 23, 49, 22, 12, 99, After Reduce:2015, 99Finished

這個時候,就已經可以查詢到執行任務成功後生成的檔案了:


通過下面的命令,可以把執行後的結果檔案從HDFS中匯出,匯出後就可以看到對應的計算結果了。

hadoop fs -get /test/output3/part-r-00000

至此這個demo就算完成了。

參考:https://my.oschina.net/itblog/blog/275294

相關推薦

IDEA新建一個gradle工程kotlinMapReduce

話不多說,直接上正餐。首先我們新建一個gradle的專案,把預設的Java前面的勾去掉,然後再kotlin前面打鉤後Next:接著設定對應的groupId和artifactId,然後Next:接著一直Next(當然要有什麼其他的需求配置就自己選擇操作):在這邊確認專案名稱之後

idea如何新建一個springmvc 工程

  如果引用,請保留版權連結謝謝! https://www.cnblogs.com/tochw/p/13811051.html java 版本 1.8.0_261 idea 版本2020.1 Tomcat 9  maven 3.6 新建工程 File->new->

Intellij idea 建立一個test工程並使用maven管理依賴

1.  Cannot start compilation: the output path is not specified for module "Test". Specify the output path in the Project Structure dialog. 解決方法

shell實現一個小指令碼來同來統計自己某個檔案的程式碼總的程式碼行數總的註釋量總的空行量?支援遍歷查詢支援軟連結查詢

[[email protected] yunwei]# cat sum_code_row_version1.4.sh #!/bin/bash # File Name: sum_code_row.sh # Author: Liwqiang # mail: [email

新建一個QT工程的詳細步驟終於搞清楚了!

QT? VC? C++? 怎麼個關係?現在基本上搞清楚了,記錄一下,之前一直用VC,只知其然,不知其所以然,而現在開發是底層的東西,所以的東西基本都自己寫,跨平臺的。 下面是詳細步驟: =============================================

eclipse新建一個project時原始檔夾的解釋

原始檔夾:指的是在eclipse的“.classpath”檔案中【kind="src"】的path。可以使用【新建->原始檔夾】命令建立新的原始檔夾,也可以在【配置構建路徑】對話方塊的【原始碼】選項卡中調整。原始檔

新建一個maven工程

val strategy 清單 4.3 創建 star ret cut code 軟件151 徐中飛 使用spring boot新建maven工程不在需要建立maven web工程,只要一般的maven工程就好了。 二、maven包的導入 清單如下: <

c程序設計 8.15幾個函數:①輸個職工的姓名和職工號;②按職工號由小到大順序排序姓名順序也隨之調整;③要求輸入一個職工號,用折半法找出該職工的姓名從主函數輸入要查找的職工號輸出該職工

xmx ebr ckey ros lbp loj ase vfk cu2 8.15寫幾個函數:①輸個職工的姓名和職工號;②按職工號由小到大順序排序,姓名順序也隨之調整;③要求輸入一個職工號,用折半法找出該職工的姓名, 從主函數輸入要查找的職工號,輸出該職工。 寫的時候為

在上線項目中Vue一個星級評價

spa align wid cti function top icon com ont 先看一下效果: html: <div class="big-star-box"> <img :src="imgNum>0 ?

Idea建立第一個gradle管理的專案

xl_echo編輯整理,歡迎轉載,轉載請宣告文章來源。更多IT、程式設計案例、資料請聯絡QQ:1280023003,加群298140694。百戰不敗,依不自稱常勝,百敗不頹,依能奮力前行。——這才是真正的堪稱強大!!! 開啟Idea,直接點選新建 這裡我們為了掩飾方便直接使

編寫一個ArrayList類來儲存1到10之間的數打亂順序後輸出按從小到大輸出按從大到小輸出

/** * Created by whp on 2018/7/30. */ public class Test { public static void main(String[] args) { List list = new ArrayList();

Android studio 新建一個空白工程提示:Conflict with dependency 'com.android.support:support-annotations' in proj

Android studio 新建一個空白工程提示:Conflict with dependency 'com.android.support:support-annotations' in project ':app'.     Error:Execution fa

[PAT][Python](讀入一個正整數 n計算其各位數字之和漢語拼音出和的每一位數字)

讀入一個正整數 n,計算其各位數字之和,用漢語拼音寫出和的每一位數字。 輸入格式:每個測試輸入包含 1 個測試用例,即給出自然數 n 的值。這裡保證 n 小於 10^100​​ 。 輸出格式: 在一行內輸出 n 的各位數字之和的每一位,拼音數字間有 1 空格,但一行中最後一個拼音數字後沒有空

IntelliJ IDEA Maven 建立 Scala 專案執行hello world

建立Maven Scala專案 https://blog.csdn.net/qq_1290259791/article/details/79052584 IntelliJ IDEA 中 右鍵新建(new)時,選項沒有scala(java) class的解決方法和具體解釋 https://

基於adt-bundle-windows-x86-20140702_2的Android新建一個Android工程(利用第三方模擬器)

新手建議先下載: adt-bundle-windows-x86-20140702_2 連結:https://pan.baidu.com/s/1wS7-wXKxpH1VeYsXsABeHQ 密碼:yzz

習題 11.1 將例11.1的程式片斷補充和改寫成一個完整、正確的程式公用繼承方式在程式中應包括輸入資料的函式在程式執行時輸入numnamesexageaddr的值程式應輸出以上

C++程式設計(第三版) 譚浩強 習題11.1 個人設計 習題 11.1 將例11.1的程式片斷補充和改寫成一個完整、正確的程式,用公用繼承方式。在程式中應包括輸入資料的函式,在程式執行時輸入num,n

IDEA新建Java Web工程註意事項

let idea microsoft 文件 ica clas 每次 部署方式 art IDEA中註意事項 在WEB-INF下新建lib和classes不是必須的。如果自己新建了,每次必須手動將jar依賴在src和web下的lib都新放一份。 getServlet

IDEA新建Java Web工程注意事項

IDEA中注意事項 在WEB-INF下新建lib和classes不是必須的。如果自己新建了,每次必須手動將jar依賴在src和web下的lib都新放一份。 getServletConfig().getServletContext().getRealPath()如果在idea中使用,需

# 從鍵盤輸入一個正整數2的冪次方的形式輸出約定冪次方括號來表示即表示為2(b),b=1時冪省略例如139=2^7+2^3+2^1+2^0即:2(7)+2(3)+2+2(0)

樣例輸入: 402 樣例輸出: 2(8)+2(7)+2(4)+2 要求:冪不能重複,如:139=26+26+23+21+20(出現了2個6次方) 參考 C 程式碼: #include<stdio.h> #include<stdlib.h>

idea使用maven建立web工程搭建SSH整合框架並部署到Tomcat伺服器

目錄 建立、執行環境 建立web工程 導架包配配置檔案搭框架 使用IDEA生成資料庫對應實體類和hibernate的對映檔案 建立類執行 部署伺服器Tomcat執行 執行檢查調bug 建立、執行環境 IntelliJ IDEA 2018.1.5 x64