03_Flink Streaming SinkFunction
来源:互联网 发布:路易斯威廉姆斯数据 编辑:程序博客网 时间:2024/04/26 07:36
env对象的addSink(SinkFunction)。需要传入一个SinkFunction对象。这个对象处理的出口。之后无法再进行数据操作。
package com.alibaba.flink.train.streaming;import org.apache.flink.configuration.Configuration;import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;import org.apache.flink.streaming.api.functions.sink.SinkFunction;public class MemSink<T> implements SinkFunction<T> {/** * 没过来一条数据调用一次 */@Overridepublic void invoke(T value) throws Exception {System.out.println("MemSink:" + value);}}class RSink extends RichSinkFunction<String> {@Overridepublic void open(Configuration parameters) throws Exception {super.open(parameters);}@Overridepublic void invoke(String value) throws Exception {}@Overridepublic void close() throws Exception {super.close();}}
package com.alibaba.flink.train.streaming;import org.apache.flink.api.common.functions.FlatMapFunction;import org.apache.flink.api.java.tuple.Tuple2;import org.apache.flink.streaming.api.datastream.DataStream;import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;import org.apache.flink.util.Collector;public class HelloWorld {public static void main(String[] args) throws Exception {StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();// env.setParallelism(4);//并发度DataStream<String> dataStream = env.readTextFile("D:/flinkdata/helloworld"); // 1:(flink storm// )(hadoop hive)dataStream = env.addSource(new MemSource());dataStream.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {@Overridepublic void flatMap(String input,Collector<Tuple2<String, Integer>> collector)throws Exception {String[] objs = input.split(" ");for (String obj : objs) {collector.collect(new Tuple2<String, Integer>(obj, 1));// (这里很关键,表示0位置是word,1的位置是1次数)}}})// 2:(flink 1)(storm 1).keyBy(0)// 3:以第0个位置的值,做分区。.sum(1)// (flink:8)(storm:5),对第1个位置的值做sum的操作。.addSink(new MemSink());//.printToErr();env.execute();// 启动任务while (true) {}}}
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.flink.streaming.api.functions.sink;import java.io.Serializable;import org.apache.flink.annotation.Public;import org.apache.flink.api.common.functions.Function;/** * Interface for implementing user defined sink functionality. * * @param <IN> Input type parameter. */@Publicpublic interface SinkFunction<IN> extends Function, Serializable {/** * Function for standard sink behaviour. This function is called for every record. * * @param value The input record. * @throws Exception */void invoke(IN value) throws Exception;}
0 0
- 03_Flink Streaming SinkFunction
- 01_Flink Streaming env
- 02_Flink Streaming SourceFunction
- 04_Flink Streaming DataStream
- 05_Flink Streaming KeyedStream
- 06_Flink Streaming State
- 07_Flink Streaming transform
- 08_Flink Streaming window
- 09_Flink Streaming execute
- 10_Flink Streaming jobSubmit
- 11_Flink Streaming partition
- 12_Flink Streaming cluster
- 02_flink 流处理
- Streaming
- ubuntu 搭建Darwin Streaming Server6.03
- ubuntu12.04上搭建darwin streaming server6.03
- ubuntu 搭建darwin streaming server6.03(成功测试通过)
- Hadoop Streaming
- max submatrix which has all borders the same color.
- OpenJudge 1.11-0.7 和为给定数
- STL之优先队列
- 神经网络与BP算法推导
- 利用js实现注册问题中的一些验证
- 03_Flink Streaming SinkFunction
- 287. Find the Duplicate Number #Hard
- Redis数据库(string和hash类型)
- connection.prepareStatement(sql).execute()返回值boolen类型什么意思?
- 工业电表接线方法,光纤接线方法、97,、2007协议
- php面向对象
- Jquery-zTree的基本用法
- zTree简单使用和代码结构
- 简单万年历