flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] yanghua commented on a change in pull request #6594: [FLINK-9311] [pubsub] Added PubSub source connector with support for checkpointing (ATLEAST_ONCE)
Date Tue, 21 Aug 2018 16:05:16 GMT
yanghua commented on a change in pull request #6594: [FLINK-9311] [pubsub] Added PubSub source
connector with support for checkpointing (ATLEAST_ONCE)
URL: https://github.com/apache/flink/pull/6594#discussion_r211664117
 
 

 ##########
 File path: flink-connectors/flink-connector-pubsub/src/main/java/org/apache/flink/streaming/connectors/pubsub/PubSubSink.java
 ##########
 @@ -0,0 +1,250 @@
+/*
+ * 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.connectors.pubsub;
+
+import org.apache.flink.api.common.serialization.SerializationSchema;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
+import org.apache.flink.streaming.api.functions.sink.SinkFunction;
+import org.apache.flink.streaming.connectors.pubsub.common.SerializableCredentialsProvider;
+
+import com.google.api.gax.core.CredentialsProvider;
+import com.google.api.gax.grpc.GrpcTransportChannel;
+import com.google.api.gax.rpc.FixedTransportChannelProvider;
+import com.google.api.gax.rpc.TransportChannel;
+import com.google.auth.Credentials;
+import com.google.cloud.pubsub.v1.Publisher;
+import com.google.protobuf.ByteString;
+import com.google.pubsub.v1.ProjectTopicName;
+import com.google.pubsub.v1.PubsubMessage;
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+
+import java.io.IOException;
+
+/**
+ * A sink function that outputs to PubSub.
+ *
+ * @param <IN> type of PubSubSink messages to write
+ */
+public class PubSubSink<IN> extends RichSinkFunction<IN> {
+
+	private SerializableCredentialsProvider serializableCredentialsProvider;
+	private SerializationSchema<IN> serializationSchema;
+	private String projectName;
+	private String topicName;
+	private String hostAndPort = null;
+
+	private transient Publisher publisher;
+
+	private PubSubSink() {
+	}
+
+	void setSerializableCredentialsProvider(SerializableCredentialsProvider serializableCredentialsProvider)
{
+		this.serializableCredentialsProvider = serializableCredentialsProvider;
+	}
+
+	void setSerializationSchema(SerializationSchema<IN> serializationSchema) {
+		this.serializationSchema = serializationSchema;
+	}
+
+	void setProjectName(String projectName) {
+		this.projectName = projectName;
+	}
+
+	void setTopicName(String topicName) {
+		this.topicName = topicName;
+	}
+
+	/**
+	 * Set the custom hostname/port combination of PubSub.
+	 * The ONLY reason to use this is during tests with the emulator provided by Google.
+	 *
+	 * @param hostAndPort The combination of hostname and port to connect to ("hostname:1234")
+	 */
+	void withHostAndPort(String hostAndPort) {
+		this.hostAndPort = hostAndPort;
+	}
+
+	void initialize() throws IOException {
+		if (serializableCredentialsProvider == null) {
+			serializableCredentialsProvider = SerializableCredentialsProvider.credentialsProviderFromEnvironmentVariables();
+		}
+		if (serializationSchema == null) {
+			throw new IllegalArgumentException("The serializationSchema has not been specified.");
+		}
+		if (projectName == null) {
+			throw new IllegalArgumentException("The projectName has not been specified.");
+		}
+		if (topicName == null) {
+			throw new IllegalArgumentException("The topicName has not been specified.");
+		}
+	}
+
+
+	private transient ManagedChannel managedChannel = null;
+	private transient TransportChannel channel = null;
+
+	@Override
+	public void open(Configuration configuration) throws Exception {
+		Publisher.Builder builder = Publisher
+			.newBuilder(ProjectTopicName.of(projectName, topicName))
+			.setCredentialsProvider(serializableCredentialsProvider);
+
+		if (hostAndPort != null) {
+			managedChannel = ManagedChannelBuilder
+				.forTarget(hostAndPort)
+				.usePlaintext(true) // This is 'Ok' because this is ONLY used for testing.
+				.build();
+			channel = GrpcTransportChannel.newBuilder().setManagedChannel(managedChannel).build();
+			builder.setChannelProvider(FixedTransportChannelProvider.create(channel));
+		}
+
+		publisher = builder.build();
+	}
+
+	@Override
+	public void close() throws Exception {
+		super.close();
+		publisher.shutdown();
+		if (channel != null) {
+			channel.close();
+			managedChannel.shutdownNow();
 
 Review comment:
   same as above

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message