Apache flink 如何将自定义表源和自定义表接收器与SQL客户端集成?

Apache flink 如何将自定义表源和自定义表接收器与SQL客户端集成?,apache-flink,Apache Flink,假设我们定义了一个自定义表源和表链接,那么如何与SQL客户机集成呢?我是否需要手动注册自定义表源\接收器名称,如下所示?如果不手动注册,连接器类型custom1 map\如何与custom1TableSource相关 StreamTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env); TableSource custom1TableSource = new custom1TableSource ( ); ta

假设我们定义了一个自定义表源和表链接,那么如何与SQL客户机集成呢?我是否需要手动注册自定义表源\接收器名称,如下所示?如果不手动注册,连接器类型custom1 map\如何与custom1TableSource相关

StreamTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env);
TableSource custom1TableSource  = new custom1TableSource ( );
tableEnv.registerTableSource("custom1", custom1TableSource);
然后配置下面的环境文件

   tables:
      - name: custom1TableSource
        type: Source
        update-mode: append
        connector:
          property-version: 1
          type: ***custom1****
我声明的源和接收器:

package com.abc;
public static class custom1TableSource implements StreamTableSource<Row>, DefinedRowtimeAttributes, DefinedProctimeAttribute {


package com.abc;
public static class custom1TableSink implements TableSink<Row>, AppendStreamTableSink<Row> {
package com.abc;
公共静态类custom1TableSource实现StreamTableSource、DefinedRowtimeAttributes、DefinedProctimeAttribute{
包com.abc;
公共静态类custom1TableSink实现了TableLink、AppendStreamTableLink{

更新:


在对源代码进行一些检查后,我发现Flink通过实现StreamTableSinkFactory和ServiceLoader创建的工厂来创建接收器和源实例,但是如何将接收器和源名称注册到TableSource和TabSink类?

我得到了答案,它需要覆盖requiredContext(),并手动写入connector.type,以kafka为例,需要将“kafka”指定给connector.type:

public abstract class KafkaTableSourceSinkFactoryBase implements
        StreamTableSourceFactory<Row>,
        StreamTableSinkFactory<Row> {

@Override
public Map<String, String> requiredContext() {
    Map<String, String> context = new HashMap<>();
    context.put(UPDATE_MODE(), UPDATE_MODE_VALUE_APPEND()); // append mode
    **context.put(CONNECTOR_TYPE(), CONNECTOR_TYPE_VALUE_KAFKA); // kafka**
    context.put(CONNECTOR_VERSION(), kafkaVersion()); // version
    context.put(CONNECTOR_PROPERTY_VERSION(), "1"); // backwards compatibility
    return context;
}
公共抽象类KafkaTableSourceSinkFactoryBase实现
StreamTableSourceFactory,
StreamTableSinkFactory{
@凌驾
公共地图requiredContext(){
Map context=newhashmap();
context.put(UPDATE_MODE(),UPDATE_MODE_VALUE_APPEND());//APPEND MODE
**context.put(CONNECTOR_TYPE(),CONNECTOR_TYPE_VALUE_KAFKA);//KAFKA**
context.put(CONNECTOR_VERSION(),kafkaVersion());//版本
context.put(CONNECTOR_PROPERTY_VERSION(),“1”);//向后兼容性
返回上下文;
}

请查看下表


SQL客户端和Table&SQL API都使用所谓的
TableFactory
s,它们是使用Java的服务提供者接口(SPI)发现的.

嗨,yufeng,我能举一个你如何完成客户表源/汇的完整例子吗?我有一个类似的要求,让流/批处理使用flink表API共享同一段代码,结果写入mongodb。如果没有应用程序开发的表API,我可以使用RichSinkFunction更容易操作。我叫刘向斌,我在深圳,你可以把代码发到我的邮箱/bronzels@hotmail.com同样,非常感谢您的帮助。@bronzels,我没有自定义表源/接收器的完整示例,如果您想通过表API\SQL将事件写入mongodb,请参考JDBCAppendTableLink。非常感谢yufeng按照您提示的方向完成。