|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.streamnative.flink.java.dynamic; |
| 20 | + |
| 21 | +import io.streamnative.flink.java.config.ApplicationConfigs; |
| 22 | +import io.streamnative.flink.java.models.LoadCreatedEvent; |
| 23 | +import org.apache.flink.connector.pulsar.source.PulsarSource; |
| 24 | +import org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor; |
| 25 | +import org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor; |
| 26 | +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; |
| 27 | + |
| 28 | +import static io.streamnative.flink.java.common.EnvironmentUtils.createEnvironment; |
| 29 | +import static io.streamnative.flink.java.config.ApplicationConfigs.loadConfig; |
| 30 | +import static java.time.Duration.ofMinutes; |
| 31 | +import static org.apache.flink.api.common.eventtime.WatermarkStrategy.forBoundedOutOfOrderness; |
| 32 | +import static org.apache.flink.configuration.Configuration.fromMap; |
| 33 | +import static org.apache.pulsar.client.api.SubscriptionType.Failover; |
| 34 | + |
| 35 | +/** |
| 36 | + * This example is used for consuming load event from Pulsar with different sub event class. |
| 37 | + */ |
| 38 | +public class DynamicSource { |
| 39 | + |
| 40 | + public static void main(String[] args) throws Exception { |
| 41 | + // Load application configs. |
| 42 | + ApplicationConfigs configs = loadConfig(args); |
| 43 | + |
| 44 | + // Create execution environment |
| 45 | + StreamExecutionEnvironment env = createEnvironment(configs); |
| 46 | + |
| 47 | + // Create a Pulsar source, it would consume messages from Pulsar on "sample/flink/simple-string" topic. |
| 48 | + PulsarSource<LoadCreatedEvent> pulsarSource = PulsarSource.builder() |
| 49 | + .setServiceUrl(configs.serviceUrl()) |
| 50 | + .setAdminUrl(configs.adminUrl()) |
| 51 | + .setStartCursor(StartCursor.earliest()) |
| 52 | + .setUnboundedStopCursor(StopCursor.never()) |
| 53 | + .setTopics("persistent://sample/flink/dynamic-load-event") |
| 54 | + .setDeserializationSchema(new DynamicDeserializationSchema()) |
| 55 | + .setSubscriptionName("flink-source") |
| 56 | + .setConsumerName("flink-source-%s") |
| 57 | + .setSubscriptionType(Failover) |
| 58 | + .setConfig(fromMap(configs.sourceConfigs())) |
| 59 | + .build(); |
| 60 | + |
| 61 | + // Pulsar Source doesn't require extra TypeInformation be provided. |
| 62 | + env.fromSource(pulsarSource, forBoundedOutOfOrderness(ofMinutes(5)), "pulsar-source") |
| 63 | + .print(); |
| 64 | + |
| 65 | + env.execute("Load Event Pulsar Source"); |
| 66 | + } |
| 67 | +} |
0 commit comments