Flink官方翻譯-01使用Java API開(kāi)始

https://ci.apache.org/projects/flink/flink-docs-release-1.3/quickstart/java_api_quickstart.html

創(chuàng)建Project

Use one of the following commands to create a project:

1.使用maven

mvn archetype:generate \

-DarchetypeGroupId=org.apache.flink \

-DarchetypeArtifactId=flink-quickstart-java \

-DarchetypeVersion=1.3.2

2.使用qucikstart 腳本

$ curl https://flink.apache.org/q/quickstart.sh | bash

檢查Project

There will be a new directory in your working directory. If you’ve used the curl approach, the directory is called quickstart. Otherwise, it has the name of your artifactId:

$ tree quickstart/ quickstart/ ├── pom.xml └── src └── main ├── java │ └── org │ └── myorg │ └── quickstart │ ├── BatchJob.java │ ├── SocketTextStreamWordCount.java │ ├── StreamingJob.java │ └── WordCount.java └── resources └── log4j.properties

這個(gè)sample項(xiàng)目是使用maven project,它包含了4個(gè)class。StreamingJob 和 BatchJob是基本的骨架項(xiàng)目瘾蛋,SocketTextStreamWordCount 是一個(gè)工作的流式例子,WordCountJob 是一個(gè)批量例子秒梳。可以直接在在本地環(huán)境運(yùn)行flink的example箕速。

We recommend you import this project into your IDE to develop and test it. If you use Eclipse, the m2e plugin allows to import Maven projects. Some Eclipse bundles include that plugin by default, others require you to install it manually. The IntelliJ IDE supports Maven projects out of the box.

A note to Mac OS X users: The default JVM heapsize for Java is too small for Flink. You have to manually increase it. In Eclipse, chooseRun Configurations -> Arguments and write into the VM Arguments box: -Xmx800m.

編譯 Project

可以輸入命令 mvn clean install -Pbuild-jar 酪碘,就可以編譯一個(gè)好的jar包在 target/original-your-artifact-id-your-version.jar,這個(gè)是沒(méi)有依賴(lài)的thin jar包盐茎,如果需要fat jar包arget/your-artifact-id-your-version.jar 兴垦。(fat jar包是指所有的依賴(lài)也包含在里面)

下一步

編寫(xiě)你的應(yīng)用

The quickstart project contains a WordCount implementation, the “Hello World” of Big Data processing systems. The goal of WordCount is to determine the frequencies of words in a text, e.g., how often do the terms “the” or “house” occur in all Wikipedia texts.

開(kāi)始項(xiàng)目包含一個(gè) wordcount的實(shí)現(xiàn),這相當(dāng)于大數(shù)據(jù)處理領(lǐng)域的“hello world”字柠。wordcount的目的是計(jì)算一個(gè)文本中單次的頻率探越。比如計(jì)算 “the” 或者 “house” 出現(xiàn)在Wikipedia texts的頻率

比如:

Sample Input:

big data is big

Sample Output:

big 2 data 1 is 1

下面的code展示了wordcount的實(shí)現(xiàn)處理每行數(shù)據(jù)包含兩個(gè)操作((a FlatMap and a Reduce operation 通過(guò)聚合求 sum),然后把 結(jié)果單詞 和 次數(shù) 輸出

public class WordCount {

public static void main(String[] args) throws Exception {

// set up the execution environment

final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

// get input data

DataSet<String> text = env.fromElements(

"To be, or not to be,--that is the question:--",

"Whether 'tis nobler in the mind to suffer",

"The slings and arrows of outrageous fortune",

"Or to take arms against a sea of troubles,"

);

DataSet<Tuple2<String, Integer>> counts =

// split up the lines in pairs (2-tuples) containing: (word,1)

text.flatMap(new LineSplitter())

// group by the tuple field "0" and sum up tuple field "1"

.groupBy(0)

.sum(1);

// execute and print result

counts.print();

}

}

The operations are defined by specialized classes, here the LineSplitter class.

public static final class LineSplitter implements FlatMapFunction<String, Tuple2<String, Integer>> {

@Override

public void flatMap(String value, Collector<Tuple2<String, Integer>> out) {

// normalize and split the line

String[] tokens = value.toLowerCase().split("\W+");

// emit the pairs

for (String token : tokens) {

if (token.length() > 0) {

out.collect(new Tuple2<String, Integer>(token, 1));

}

}

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末窑业,一起剝皮案震驚了整個(gè)濱河市钦幔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌常柄,老刑警劉巖鲤氢,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件搀擂,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡卷玉,警方通過(guò)查閱死者的電腦和手機(jī)哥倔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)揍庄,“玉大人,你說(shuō)我怎么就攤上這事东抹÷熳樱” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵缭黔,是天一觀(guān)的道長(zhǎng)食茎。 經(jīng)常有香客問(wèn)我,道長(zhǎng)馏谨,這世上最難降的妖魔是什么别渔? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮惧互,結(jié)果婚禮上哎媚,老公的妹妹穿的比我還像新娘。我一直安慰自己喊儡,他們只是感情好拨与,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著艾猜,像睡著了一般买喧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上匆赃,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天淤毛,我揣著相機(jī)與錄音,去河邊找鬼算柳。 笑死低淡,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的瞬项。 我是一名探鬼主播查牌,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼滥壕!你這毒婦竟也來(lái)了纸颜?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤绎橘,失蹤者是張志新(化名)和其女友劉穎胁孙,沒(méi)想到半個(gè)月后唠倦,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡涮较,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年稠鼻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狂票。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡候齿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出闺属,到底是詐尸還是另有隱情慌盯,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布掂器,位于F島的核電站亚皂,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏国瓮。R本人自食惡果不足惜灭必,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望乃摹。 院中可真熱鬧禁漓,春花似錦、人聲如沸孵睬。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)肪康。三九已至荚恶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間磷支,已是汗流浹背谒撼。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留雾狈,地道東北人廓潜。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像善榛,于是被迫代替她去往敵國(guó)和親辩蛋。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容