03_Flink Streaming SinkFunction

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> {

	/**
	 * 沒過來一條數據調用一次
	 */
	@Override
	public void invoke(T value) throws Exception {
		System.out.println("MemSink:" + value);
	}

}

class RSink extends RichSinkFunction<String> {
	@Override
	public void open(Configuration parameters) throws Exception {
		super.open(parameters);
	}

	@Override
	public void invoke(String value) throws Exception {

	}

	@Override
	public 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>>() {
							@Override
							public 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.
 */
@Public
public 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;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章