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
原创粉丝点击