forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pulsar-flink] Implements a batch program on Pulsar topic by writing …
…Flink DataSet as Avro (apache#3205) Implements a batch program on Pulsar topic by writing Flink DataSet as Avro data Type
- Loading branch information
1 parent
1414edd
commit 5493e2f
Showing
13 changed files
with
577 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...ava/org/apache/flink/batch/connectors/pulsar/example/FlinkPulsarBatchAvroSinkExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* 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.batch.connectors.pulsar.example; | ||
|
||
|
||
import org.apache.flink.api.common.io.OutputFormat; | ||
import org.apache.flink.api.java.DataSet; | ||
import org.apache.flink.api.java.ExecutionEnvironment; | ||
import org.apache.flink.batch.connectors.pulsar.PulsarAvroOutputFormat; | ||
import org.apache.flink.batch.connectors.pulsar.avro.generated.NasaMission; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Implements a batch program on Pulsar topic by writing Flink DataSet as Avro. | ||
*/ | ||
public class FlinkPulsarBatchAvroSinkExample { | ||
|
||
private static final List<NasaMission> nasaMissions = Arrays.asList( | ||
NasaMission.newBuilder().setId(1).setName("Mercury program").setStartYear(1959).setEndYear(1963).build(), | ||
NasaMission.newBuilder().setId(2).setName("Apollo program").setStartYear(1961).setEndYear(1972).build(), | ||
NasaMission.newBuilder().setId(3).setName("Gemini program").setStartYear(1963).setEndYear(1966).build(), | ||
NasaMission.newBuilder().setId(4).setName("Skylab").setStartYear(1973).setEndYear(1974).build(), | ||
NasaMission.newBuilder().setId(5).setName("Apollo–Soyuz Test Project").setStartYear(1975).setEndYear(1975).build()); | ||
|
||
private static final String SERVICE_URL = "pulsar://127.0.0.1:6650"; | ||
private static final String TOPIC_NAME = "my-flink-topic"; | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
// set up the execution environment | ||
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); | ||
|
||
// create PulsarAvroOutputFormat instance | ||
final OutputFormat<NasaMission> pulsarAvroOutputFormat = new PulsarAvroOutputFormat<>(SERVICE_URL, TOPIC_NAME); | ||
|
||
// create DataSet | ||
DataSet<NasaMission> nasaMissionDS = env.fromCollection(nasaMissions); | ||
// map nasa mission names to upper-case | ||
nasaMissionDS.map(nasaMission -> new NasaMission( | ||
nasaMission.getId(), | ||
nasaMission.getName(), | ||
nasaMission.getStartYear(), | ||
nasaMission.getEndYear())) | ||
// filter missions which started after 1970 | ||
.filter(nasaMission -> nasaMission.getStartYear() > 1970) | ||
// write batch data to Pulsar | ||
.output(pulsarAvroOutputFormat); | ||
|
||
// set parallelism to write Pulsar in parallel (optional) | ||
env.setParallelism(2); | ||
|
||
// execute program | ||
env.execute("Flink - Pulsar Batch Avro"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
examples/flink-consumer-source/src/main/resources/avro/NasaMission.avsc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{"namespace": "org.apache.flink.batch.connectors.pulsar.avro.generated", | ||
"type": "record", | ||
"name": "NasaMission", | ||
"fields": [ | ||
{"name": "id", "type": "int"}, | ||
{"name": "name", "type": "string"}, | ||
{"name": "start_year", "type": ["int", "null"]}, | ||
{"name": "end_year", "type": ["int", "null"]} | ||
] | ||
} |
72 changes: 72 additions & 0 deletions
72
...g/apache/flink/batch/connectors/pulsar/example/FlinkPulsarBatchAvroSinkScalaExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* 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.batch.connectors.pulsar.example | ||
|
||
import org.apache.flink.api.scala._ | ||
import org.apache.flink.batch.connectors.pulsar.PulsarAvroOutputFormat | ||
import org.apache.flink.batch.connectors.pulsar.avro.generated.NasaMission | ||
|
||
/** | ||
* Implements a batch Scala program on Pulsar topic by writing Flink DataSet as Avro. | ||
*/ | ||
object FlinkPulsarBatchAvroSinkScalaExample { | ||
|
||
private val SERVICE_URL = "pulsar://127.0.0.1:6650" | ||
private val TOPIC_NAME = "my-flink-topic" | ||
|
||
val nasaMissions = List( | ||
NasaMission.newBuilder.setId(1).setName("Mercury program").setStartYear(1959).setEndYear(1963).build, | ||
NasaMission.newBuilder.setId(2).setName("Apollo program").setStartYear(1961).setEndYear(1972).build, | ||
NasaMission.newBuilder.setId(3).setName("Gemini program").setStartYear(1963).setEndYear(1966).build, | ||
NasaMission.newBuilder.setId(4).setName("Skylab").setStartYear(1973).setEndYear(1974).build, | ||
NasaMission.newBuilder.setId(5).setName("Apollo–Soyuz Test Project").setStartYear(1975).setEndYear(1975).build) | ||
|
||
def main(args: Array[String]): Unit = { | ||
|
||
// set up the execution environment | ||
val env = ExecutionEnvironment.getExecutionEnvironment | ||
|
||
// create PulsarCsvOutputFormat instance | ||
val pulsarAvroOutputFormat = | ||
new PulsarAvroOutputFormat[NasaMission](SERVICE_URL, TOPIC_NAME) | ||
|
||
// create DataSet | ||
val textDS = env.fromCollection(nasaMissions) | ||
|
||
// map nasa mission names to upper-case | ||
textDS.map(nasaMission => new NasaMission( | ||
nasaMission.getId, | ||
nasaMission.getName, | ||
nasaMission.getStartYear, | ||
nasaMission.getEndYear)) | ||
|
||
// filter missions which started after 1970 | ||
.filter(_.getStartYear > 1970) | ||
|
||
// write batch data to Pulsar as Avro | ||
.output(pulsarAvroOutputFormat) | ||
|
||
// set parallelism to write Pulsar in parallel (optional) | ||
env.setParallelism(2) | ||
|
||
// execute program | ||
env.execute("Flink - Pulsar Batch Avro") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.