下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Apache Flink TrainingDataSet API BasicsJune 3rd, 2015DataSet APIBatch ProcessingJava, Scala, and PythonAll examples here in JavaMany concepts can be translated to the DataStream APIDocumentation available at 2DataSet API by Example3WordCount: main methodpublic static void main(String args) throws Ex
2、ception / set up the execution environment final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); / get input data either from file or use example data DataSet inputText = env.readTextFile(args0); DataSetTuple2 counts = / split up the lines in tuples containing: (word,1) in
3、putText.flatMap(new Tokenizer() / group by the tuple field 0 .groupBy(0) /sum up tuple field 1 .reduceGroup(new SumWords(); / emit result counts.writeAsCsv(args1, n, ); / execute program env.execute(WordCount Example);4Execution Environmentpublic static void main(String args) throws Exception / set
4、up the execution environment final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); / get input data either from file or use example data DataSet inputText = env.readTextFile(args0); DataSetTuple2 counts = / split up the lines in tuples containing: (word,1) inputText.flatMa
5、p(new Tokenizer() / group by the tuple field 0 .groupBy(0) /sum up tuple field 1 .reduceGroup(new SumWords(); / emit result counts.writeAsCsv(args1, n, ); / execute program env.execute(WordCount Example);5Data Sourcespublic static void main(String args) throws Exception / set up the execution enviro
6、nment final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); / get input data either from file or use example data DataSet inputText = env.readTextFile(args0); DataSetTuple2 counts = / split up the lines in tuples containing: (word,1) inputText.flatMap(new Tokenizer() / gro
7、up by the tuple field 0 .groupBy(0) /sum up tuple field 1 .reduceGroup(new SumWords(); / emit result counts.writeAsCsv(args1, n, ); / execute program env.execute(WordCount Example);6Data typespublic static void main(String args) throws Exception / set up the execution environment final ExecutionEnvi
8、ronment env = ExecutionEnvironment.getExecutionEnvironment(); / get input data either from file or use example data DataSet inputText = env.readTextFile(args0); DataSetTuple2 counts = / split up the lines in tuples containing: (word,1) inputText.flatMap(new Tokenizer() / group by the tuple field 0 .
9、groupBy(0) /sum up tuple field 1 .reduceGroup(new SumWords(); / emit result counts.writeAsCsv(args1, n, ); / execute program env.execute(WordCount Example);7Transformationspublic static void main(String args) throws Exception / set up the execution environment final ExecutionEnvironment env = Execut
10、ionEnvironment.getExecutionEnvironment(); / get input data either from file or use example data DataSet inputText = env.readTextFile(args0); DataSetTuple2 counts = / split up the lines in tuples containing: (word,1) inputText.flatMap(new Tokenizer() / group by the tuple field 0 .groupBy(0) /sum up t
11、uple field 1 .reduceGroup(new SumWords(); / emit result counts.writeAsCsv(args1, n, ); / execute program env.execute(WordCount Example);8User functionspublic static void main(String args) throws Exception / set up the execution environment final ExecutionEnvironment env = ExecutionEnvironment.getExe
12、cutionEnvironment(); / get input data either from file or use example data DataSet inputText = env.readTextFile(args0); DataSetTuple2 counts = / split up the lines in tuples containing: (word,1) inputText.flatMap(new Tokenizer() / group by the tuple field 0 .groupBy(0) /sum up tuple field 1 .reduceG
13、roup(new SumWords(); / emit result counts.writeAsCsv(args1, n, ); / execute program env.execute(WordCount Example);9DataSinkspublic static void main(String args) throws Exception / set up the execution environment final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); / get
14、 input data either from file or use example data DataSet inputText = env.readTextFile(args0); DataSetTuple2 counts = / split up the lines in tuples containing: (word,1) inputText.flatMap(new Tokenizer() / group by the tuple field 0 .groupBy(0) /sum up tuple field 1 .reduceGroup(new SumWords(); / emi
15、t result counts.writeAsCsv(args1, n, ); / execute program env.execute(WordCount Example);10Execute!public static void main(String args) throws Exception / set up the execution environment final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); / get input data either from fi
16、le or use example data DataSet inputText = env.readTextFile(args0); DataSetTuple2 counts = / split up the lines in tuples containing: (word,1) inputText.flatMap(new Tokenizer() / group by the tuple field 0 .groupBy(0) /sum up tuple field 1 .reduceGroup(new SumWords(); / emit result counts.writeAsCsv
17、(args1, n, ); / execute program env.execute(WordCount Example);11WordCount: Mappublic static class Tokenizer implements FlatMapFunctionString, Tuple2 Override public void flatMap(String value, CollectorTuple2 out) / normalize and split the line String tokens = value.toLowerCase().split(W+); / emit t
18、he pairs for (String token : tokens) if (token.length() 0) out.collect( new Tuple2(token, 1); 12WordCount: Map: Interfacepublic static class Tokenizer implements FlatMapFunctionString, Tuple2 Override public void flatMap(String value, CollectorTuple2 out) / normalize and split the line String tokens
19、 = value.toLowerCase().split(W+); / emit the pairs for (String token : tokens) if (token.length() 0) out.collect( new Tuple2(token, 1); 13WordCount: Map: Typespublic static class Tokenizer implements FlatMapFunctionString, Tuple2 Override public void flatMap(String value, CollectorTuple2 out) / norm
20、alize 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(token, 1); 14WordCount: Map: Collectorpublic static class Tokenizer implements FlatMapFunctionString, Tuple2 Override public void flatMap
21、(String value, CollectorTuple2 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(token, 1); 15WordCount: Reducepublic static class SumWords implements GroupReduceFunctionTuple2
22、, Tuple2 Override public void reduce(IterableTuple2 values, CollectorTuple2 out) int count = 0; String word = null; for (Tuple2 tuple : values) word = tuple.f0; count+; out.collect(new Tuple2(word, count); 16WordCount: Reduce: Interfacepublic static class SumWords implements GroupReduceFunctionTuple
23、2, Tuple2 Override public void reduce(IterableTuple2 values, CollectorTuple2 out) int count = 0; String word = null; for (Tuple2 tuple : values) word = tuple.f0; count+; out.collect(new Tuple2(word, count); 17WordCount: Reduce: Typespublic static class SumWords implements GroupReduceFunctionTuple2,
24、Tuple2 Override public void reduce(IterableTuple2 values, CollectorTuple2 out) int count = 0; String word = null; for (Tuple2 tuple : values) word = tuple.f0; count+; out.collect(new Tuple2(word, count); 18WordCount: Reduce: Collectorpublic static class SumWords implements GroupReduceFunctionTuple2,
25、 Tuple2 Override public void reduce(IterableTuple2 values, CollectorTuple2 out) int count = 0; String word = null; for (Tuple2 tuple : values) word = tuple.f0; count+; out.collect(new Tuple2(word, count); 19DataSet API Concepts20Data TypesBasic Java TypesString, Long, Integer, Boolean,ArraysComposit
26、e TypesTuplesMany more (covered in the advanced slides)21TuplesThe easiest and most lightweight way of encapsulating data in FlinkTuple1 up to Tuple25Tuple2 person = new Tuple2(Max, Mustermann”);Tuple3 person = new Tuple3(Max, Mustermann, 42);Tuple4 person = new Tuple4(Max, Mustermann, 42, true);/ z
27、ero based index!String firstName = person.f0;String secondName = person.f1;Integer age = person.f2;Boolean fired = person.f3;22Transformations: MapDataSet integers = env.fromElements(1, 2, 3, 4);/ Regular Map - Takes one element and produces one elementDataSet doubleIntegers = integers.map(new MapFu
28、nction() Override public Integer map(Integer value) return value * 2; );doubleIntegers.print(); 2, 4, 6, 8/ Flat Map - Takes one element and produces zero, one, or more elements.DataSet doubleIntegers2 = integers.flatMap(new FlatMapFunction() Override public void flatMap(Integer value, Collector out
29、) out.collect(value * 2); );doubleIntegers2.print(); 2, 4, 6, 823Transformations: Filter/ The DataSetDataSet integers = env.fromElements(1, 2, 3, 4);DataSet filtered = integers.filter(new FilterFunction() Override public boolean filter(Integer value) return value != 3; );integers.print(); 1, 2, 424G
30、roupings and Reduce/ (name, age) of employeesDataSetTuple2 employees = / group by second field (age)DataSet grouped = employees.groupBy(1) / return a list of age groups with its counts .reduceGroup(new CountSameAge();25NameAgeStephan18Fabian23Julia27Romeo27Anna18DataSets can be split into groupsGrou
31、ps are defined using a common keyAgeGroupCount182231272GroupReducepublic static class CountSameAge implements GroupReduceFunction Tuple2, Tuple2 Overridepublic void reduce(IterableTuple2 values, CollectorTuple2 out) Integer ageGroup = 0;Integer countsInGroup = 0;for (Tuple2 person : values) ageGroup
32、 = person.f1;countsInGroup+;out.collect(new Tuple2(ageGroup, countsInGroup);26Joining two DataSets/ authors (id, name, email)DataSetTuple3 authors = .;/ posts (title, content, author_id)DataSetTuple3 posts = .;DataSetTuple2Tuple3, Tuple3 archive = authors.join(posts).where(0).equalTo(2);27AuthorsIdN
33、ameemail1Fabianfabian.2Juliajulia.3Maxmax.4Romeoromeo.PostsTitleContentAuthor id2.4.4.1.2Joining two DataSets/ authors (id, name, email)DataSetTuple3 authors = .;/ posts (title, content, author_id)DataSetTuple3 posts = .;DataSetTuple2Tuple3, Tuple3 archive = authors.join(posts).where(0).equalTo(2);2
34、8ArchiveIdNameemailTitleContentAuthor id1Fabianfabian.12Juliajulia.22Juliajulia.23Romeoromeo.44Romeoromeo.4Join with join function/ authors (id, name, email)DataSetTuple3 authors = .;/ posts (title, content, author_id)DataSetTuple3 posts = .;/ (title, author name)DataSetTuple2 archive = authors.join
35、(posts).where(0).equalTo(2).with(new PostsByUser();public static class PostsByUser implements JoinFunctionTuple3, Tuple3, Tuple2 Overridepublic Tuple2 join( Tuple3 left, Tuple3 right) return new Tuple2(left.f1, right.f0);29ArchiveNameTitleFabian.Julia.Julia.Romeo.Romeo.Data SourcesTextreadTextFile(“
36、/path/to/file”)CSVreadCsvFile(“/path/to/file”)CollectionfromCollection(collection)fromElements(1,2,3,4,5)30Data Sources: CollectionsExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();/ read from elementsDataSet names = env.fromElements(“Some”, “Example”, “Strings”);/ read from
37、 Java collectionList list = new ArrayList(); list.add(“Some”); list.add(“Example”); list.add(“Strings”);DataSet names = env.fromCollection(list);31Data Sources: File-BasedExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();/ read text file from local or distributed file systemD
38、ataSet localLines = env.readTextFile(”/path/to/my/textfile);/ read a CSV file with three fieldsDataSetTuple3 csvInput = env.readCsvFile(“/the/CSV/file).types(Integer.class, String.class, Double.class);/ read a CSV file with five fields, taking only two of themDataSetTuple2 csvInput = env.readCsvFile(“/the/CSV/file) / take the firs
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 校外培训学生安全离校课件
- 暴雨防范安全培训课件
- 励志风个人工作总结
- 校园防溺水知识课件
- 金山词霸课件
- 青色渐变国潮风中国风幼儿家风家训小故事动态
- 亲子运动会亲子趣味活动
- ebay商业策划书课件
- 金属仿古培训课件
- 2025 小学六年级数学下册圆柱的实物观察与认识课件
- 四川长江担保集团有限公司及其子公司2025年第六批员工公开招聘的备考题库及一套参考答案详解
- 2026内蒙古包头市昆区残联残疾人专职委员招聘2人参考考试试题及答案解析
- 2025年物业管理师物业管理实务真题及试题及答案
- 2026届吉林省长春市第150中学高二生物第一学期期末达标检测试题含解析
- 2026年二级建造师之二建水利水电实务考试题库300道含完整答案【典优】
- 2024年北京日报社招聘真题
- 甲氨蝶呤冲击课件
- 珠宝采购合同协议
- 2026年长沙电力职业技术学院单招职业技能测试题库及参考答案详解一套
- 2026年白城医学高等专科学校单招职业技能考试题库带答案
- 2025年武夷学院期末题库及答案
评论
0/150
提交评论