-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from harbby/dev
Dev
- Loading branch information
Showing
27 changed files
with
755 additions
and
450 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
configurations.all { | ||
resolutionStrategy { preferProjectModules() } | ||
} | ||
|
||
dependencies { | ||
compileOnly project(':sylph-runners:sylph-runner-flink') | ||
|
||
compileOnly project(":sylph-etl-api") | ||
|
||
compileOnly(group: 'org.apache.flink', name: 'flink-streaming-scala_2.11', version: deps.flink) { | ||
exclude(module: 'flink-shaded-hadoop2') | ||
} | ||
|
||
compile (group: 'org.apache.flink', name: 'flink-connector-kafka-base_2.11', version: deps.flink){ | ||
exclude(module: 'kafka-clients') | ||
} | ||
} |
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
106 changes: 106 additions & 0 deletions
106
sylph-base-kafka/src/main/java/ideal/sylph/plugins/kafka/flink/KafkaBaseSource.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,106 @@ | ||
/* | ||
* Copyright (C) 2018 The Sylph Authors | ||
* | ||
* Licensed 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 ideal.sylph.plugins.kafka.flink; | ||
|
||
import ideal.sylph.etl.SourceContext; | ||
import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
import org.apache.flink.api.common.typeinfo.Types; | ||
import org.apache.flink.api.java.typeutils.RowTypeInfo; | ||
import org.apache.flink.api.java.typeutils.TypeExtractor; | ||
import org.apache.flink.streaming.api.datastream.DataStream; | ||
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumerBase; | ||
import org.apache.flink.streaming.util.serialization.KeyedDeserializationSchema; | ||
import org.apache.flink.types.Row; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public abstract class KafkaBaseSource | ||
{ | ||
private static final long serialVersionUID = 2L; | ||
private static final String[] KAFKA_COLUMNS = new String[] {"_topic", "_key", "_message", "_partition", "_offset"}; | ||
|
||
public abstract FlinkKafkaConsumerBase<Row> getKafkaConsumerBase(List<String> topicSets, | ||
KeyedDeserializationSchema<Row> deserializationSchema, Properties properties); | ||
|
||
/** | ||
* 初始化(driver阶段执行) | ||
**/ | ||
public DataStream<Row> createSource(StreamExecutionEnvironment execEnv, KafkaSourceConfig config, SourceContext context) | ||
{ | ||
requireNonNull(execEnv, "execEnv is null"); | ||
requireNonNull(config, "config is null"); | ||
String topics = config.getTopics(); | ||
String groupId = config.getGroupid(); //消费者的名字 | ||
String offsetMode = config.getOffsetMode(); //latest earliest | ||
|
||
Properties properties = new Properties(); | ||
properties.put("bootstrap.servers", config.getBrokers()); //需要把集群的host 配置到程序所在机器 | ||
//"enable.auto.commit" -> (false: java.lang.Boolean), //不自动提交偏移量 | ||
// "session.timeout.ms" -> "30000", //session默认是30秒 超过5秒不提交offect就会报错 | ||
// "heartbeat.interval.ms" -> "5000", //10秒提交一次 心跳周期 | ||
properties.put("group.id", groupId); //注意不同的流 group.id必须要不同 否则会出现offect commit提交失败的错误 | ||
properties.put("auto.offset.reset", offsetMode); //latest earliest | ||
|
||
KeyedDeserializationSchema<Row> deserializationSchema = "json".equals(config.getValueType()) ? | ||
new JsonSchema(context.getSchema()) : new RowDeserializer(); | ||
|
||
List<String> topicSets = Arrays.asList(topics.split(",")); | ||
//org.apache.flink.streaming.api.checkpoint.CheckpointedFunction | ||
FlinkKafkaConsumerBase<Row> base = getKafkaConsumerBase(topicSets, deserializationSchema, properties); | ||
return execEnv.addSource(base); | ||
} | ||
|
||
private static class RowDeserializer | ||
implements KeyedDeserializationSchema<Row> | ||
{ | ||
@Override | ||
public boolean isEndOfStream(Row nextElement) | ||
{ | ||
return false; | ||
} | ||
|
||
@Override | ||
public Row deserialize(byte[] messageKey, byte[] message, String topic, int partition, long offset) | ||
{ | ||
return Row.of( | ||
topic, //topic | ||
messageKey == null ? null : new String(messageKey, UTF_8), //key | ||
new String(message, UTF_8), //message | ||
partition, | ||
offset | ||
); | ||
} | ||
|
||
@Override | ||
public TypeInformation<Row> getProducedType() | ||
{ | ||
TypeInformation<?>[] types = new TypeInformation<?>[] { | ||
TypeExtractor.createTypeInfo(String.class), | ||
TypeExtractor.createTypeInfo(String.class), //createTypeInformation[String] | ||
TypeExtractor.createTypeInfo(String.class), | ||
Types.INT, | ||
Types.LONG | ||
}; | ||
return new RowTypeInfo(types, KAFKA_COLUMNS); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
apply plugin: 'scala' | ||
|
||
dependencies { | ||
//--------------------------------------------------spark---------------------------------------------------- | ||
compileOnly(group: 'org.apache.spark', name: 'spark-sql_2.11', version: deps.spark) { | ||
exclude(module: 'spark-core_2.11') | ||
} | ||
compileOnly(group: 'org.apache.spark', name: 'spark-streaming_2.11', version: deps.spark) { | ||
exclude(module: 'spark-core_2.11') | ||
} | ||
compileOnly(group: 'org.apache.spark', name: 'spark-core_2.11', version: deps.spark) { | ||
exclude(module: 'hadoop-client') | ||
} | ||
compileOnly group: 'org.apache.hadoop', name: 'hadoop-client', version: '2.7.3' | ||
|
||
/** | ||
* spark 结构化流 kafka专用 | ||
* */ | ||
compile group: 'org.apache.spark', name: 'spark-sql-kafka-0-10_2.11', version: deps.spark | ||
|
||
/** | ||
* spark streaming kafka 依赖 | ||
* */ | ||
compile(group: 'org.apache.spark', name: 'spark-streaming-kafka-0-10_2.11', version: deps.spark) { | ||
exclude(group: 'org.spark-project.spark') | ||
exclude(group: 'org.scala-lang') | ||
exclude(module: 'spark-tags_2.11') | ||
exclude(module: 'slf4j-log4j12') | ||
exclude(module: 'slf4j-api') | ||
exclude(module: 'snappy-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
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
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 |
---|---|---|
@@ -1,36 +1,11 @@ | ||
apply plugin: 'scala' | ||
|
||
dependencies { | ||
compileOnly(group: 'org.apache.flink', name: 'flink-streaming-scala_2.11', version: deps.flink) { | ||
exclude(module: 'flink-shaded-hadoop2') | ||
} | ||
compile group: 'org.apache.flink', name: 'flink-connector-kafka-0.10_2.11', version: deps.flink | ||
//--------------------------------------------------spark---------------------------------------------------- | ||
compileOnly(group: 'org.apache.spark', name: 'spark-sql_2.11', version: deps.spark) { | ||
exclude(module: 'spark-core_2.11') | ||
} | ||
compileOnly(group: 'org.apache.spark', name: 'spark-streaming_2.11', version: deps.spark) { | ||
exclude(module: 'spark-core_2.11') | ||
} | ||
compileOnly(group: 'org.apache.spark', name: 'spark-core_2.11', version: deps.spark) { | ||
exclude(module: 'hadoop-client') | ||
} | ||
compileOnly group: 'org.apache.hadoop', name: 'hadoop-client', version: '2.7.3' | ||
|
||
/** | ||
* spark 结构化流 kafka专用 | ||
* */ | ||
compile group: 'org.apache.spark', name: 'spark-sql-kafka-0-10_2.11', version: deps.spark | ||
|
||
compile project(':sylph-base-kafka') | ||
|
||
/** | ||
* spark streaming kafka 老流依赖 | ||
* */ | ||
compile(group: 'org.apache.spark', name: 'spark-streaming-kafka-0-10_2.11', version: deps.spark) { | ||
exclude(group: 'org.spark-project.spark') | ||
exclude(group: 'org.scala-lang') | ||
exclude(module: 'spark-tags_2.11') | ||
exclude(module: 'slf4j-log4j12') | ||
exclude(module: 'slf4j-api') | ||
exclude(module: 'snappy-java') | ||
} | ||
testCompile project(':sylph-runners:sylph-runner-flink') | ||
testCompile project(':sylph-spi') | ||
} |
Oops, something went wrong.