文档

同步数据到时序表

更新时间:

您可以使用kafka-connect-tablestore包将Kafka中数据写入Tablestore的时序表中。本文主要介绍了如何配置Kafka写入时序数据。

前提条件

  • 已安装Kafka,并且已启动ZooKeeper和Kafka。更多信息,请参见Kafka官方文档

  • 已开通表格存储服务,创建实例以及创建时序表。具体操作,请参见使用流程

    说明

    您也可以通过Tablestore Sink Connector自动创建目标时序表,此时需要配置auto.create为true。

  • 已获取AccessKey。具体操作,请参见获取AccessKey

背景信息

表格存储支持对时序数据进行存储以及分析。更多信息,请参见时序模型概述

步骤一:部署Tablestore Sink Connector

  1. 通过以下任意一种方式获取Tablestore Sink Connector。

    • 通过GitHub下载源码并编译。源码的GitHub路径为Tablestore Sink Connector源码

      1. 通过Git工具执行以下命令下载Tablestore Sink Connector源码。

        git clone https://github.com/aliyun/kafka-connect-tablestore.git
      2. 进入到下载的源码目录后,执行以下命令进行Maven打包。

        mvn clean package -DskipTests

        编译完成后,生成的压缩包(例如kafka-connect-tablestore-1.0.jar)会存放在target目录。

    • 直接下载编译完成的kafka-connect-tablestore压缩包

  2. 将压缩包复制到各个节点的$KAFKA_HOME/libs目录下。

步骤二:启动Tablestore Sink Connector

Tablestore Sink Connector具有standalone模式和distributed模式两种工作模式。请根据实际选择。

由于写入时序数据时,Kafka侧的消息记录必须为JSON格式,因此启动Tablestore Sink Connector时需要使用Jsonconverter,且不需要提取schema以及不需要输入key,请在connect-standalone.properties和connect-distributed.properties中按照如下示例配置对应配置项。

说明

如果输入了key,请按照key的格式配置key.converter和key.converter.schemas.enable。

value.converter=org.apache.kafka.connect.json.JsonConverter
value.converter.schemas.enable=false

此处以配置standalone模式为例介绍,distributed模式的配置步骤与同步数据到数据表时的distributed模式配置步骤类似,只需按照上述示例在worker配置文件connect-distributed.properties中修改对应配置项以及在connector文件connect-tablestore-sink-quickstart.json中修改时序相关配置即可。具体操作,请参见步骤二:启动Tablestore Sink Connector中distributed模式的配置步骤。

standalone模式的配置步骤如下:

  1. 根据实际修改worker配置文件connect-standalone.properties和connector配置文件connect-tablestore-sink-quickstart.properties。

    • worker配置文件connect-standalone.properties的配置示例

      worker配置中包括Kafka连接参数、序列化格式、提交偏移量的频率等配置项。此处以Kafka官方示例为例介绍。更多信息,请参见Kafka Connect

      # 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.
      
      # These are defaults. This file just demonstrates how to override some settings.
      bootstrap.servers=localhost:9092
      
      # The converters specify the format of data in Kafka and how to translate it into Connect data. Every Connect user will
      # need to configure these based on the format they want their data in when loaded from or stored into Kafka
      key.converter=org.apache.kafka.connect.json.JsonConverter
      value.converter=org.apache.kafka.connect.json.JsonConverter
      # Converter-specific settings can be passed in by prefixing the Converter's setting with the converter we want to apply
      # it to
      key.converter.schemas.enable=true
      value.converter.schemas.enable=false
      
      offset.storage.file.filename=/tmp/connect.offsets
      # Flush much faster than normal, which is useful for testing/debugging
      offset.flush.interval.ms=10000
      
      # Set to a list of filesystem paths separated by commas (,) to enable class loading isolation for plugins
      # (connectors, converters, transformations). The list should consist of top level directories that include 
      # any combination of: 
      # a) directories immediately containing jars with plugins and their dependencies
      # b) uber-jars with plugins and their dependencies
      # c) directories immediately containing the package directory structure of classes of plugins and their dependencies
      # Note: symlinks will be followed to discover dependencies or plugins.
      # Examples: 
      # plugin.path=/usr/local/share/java,/usr/local/share/kafka/plugins,/opt/connectors,
      #plugin.path=
    • connector配置文件connect-tablestore-sink-quickstart.properties的配置示例

      connector配置中包括连接器类、表格存储连接参数、数据映射等配置项。更多信息,请参见配置说明

      # 设置连接器名称。
      name=tablestore-sink
      # 指定连接器类。
      connector.class=TableStoreSinkConnector
      # 设置最大任务数。 
      tasks.max=1
      # 指定导出数据的Kafka的Topic列表。
      topics=test
      
      # 以下为Tablestore连接参数的配置。
      # Tablestore实例的Endpoint。
      tablestore.endpoint=https://xxx.xxx.ots.aliyuncs.com
      # 指定认证模式。
      tablestore.auth.mode=aksk
      # 填写AccessKey ID和AccessKey Secret。如果使用aksk认证,则需要填入这两项。
      tablestore.access.key.id=xxx
      tablestore.access.key.secret=xxx
      # 指定Tablestore实例名称。
      tablestore.instance.name=xxx
      ## STS认证相关配置,如果使用STS认证,则下列各项必填。此外aksk还需要在环境变量中配置配入ACCESS_ID和ACCESS_KEY。
      #sts.endpoint=
      #region=
      #account.id=
      #role.name=
      
      # 定义目标表名称的格式字符串,字符串中可包含<topic>作为原始Topic的占位符。
      # topics.assign.tables配置的优先级更高,如果配置了topics.assign.tables,则忽略table.name.format的配置。
      # 例如当设置table.name.format为kafka_<topic>时,如果kafka中主题名称为test,则将映射到Tablestore的表名为kafka_test。
      table.name.format=<topic>
      # 指定Topic与目标表的映射关系,以"<topic>:<tablename>"格式映射Topic和表名,Topic和表名之间的分隔符为半角冒号(:),不同映射之间分隔符为半角逗号(,)。
      # 如果缺省,则采取table.name.format的配置。
      # topics.assign.tables=test:test_kafka
      
      
      # 是否自动创建目标表,默认值为false。
      auto.create=true
      
      
      # 以下为脏数据处理相关配置。
      # 在解析Kafka Record或者写入时序表时可能发生错误,您可以可通过以下配置进行处理。
      # 指定容错能力,可选值包括none和all,默认值为none。
      # none表示任何错误都将导致Sink Task立即失败。
      # all表示跳过产生错误的Record,并记录该Record。
      runtime.error.tolerance=none
      # 指定脏数据记录模式,可选值包括ignore、kafka和tablestore,默认值为ignore。
      # ignore表示忽略所有错误。
      # kafka表示将产生错误的Record和错误信息存储在Kafka的另一个Topic中。
      # tablestore表示将产生错误的Record和错误信息存储在Tablestore另一张数据表中。
      runtime.error.mode=ignore
      
      # 当脏数据记录模式为kafka时,需要配置Kafka集群地址和Topic。
      # runtime.error.bootstrap.servers=localhost:9092
      # runtime.error.topic.name=errors
      
      # 当脏数据记录模式为tablestore时,需要配置Tablestore中数据表名称。
      # runtime.error.table.name=errors
      
      ##以下为时序表新增配置。
      
      # connector工作模式,默认为normal。
      tablestore.mode=timeseries
      # 时序表主键字段映射。
      tablestore.timeseries.test.measurement=m
      tablestore.timeseries.test.dataSource=d
      tablestore.timeseries.test.tags=region,level
      # 时序表时间字段映射。
      tablestore.timeseries.test.time=timestamp
      tablestore.timeseries.test.time.unit=MILLISECONDS
      # 是否将时序数据字段(field)的列名转为小写,默认为true。由于当前时序模型中时序表的列名不支持大写字母,如果配置为false,且列名中有大写字母,写入会报错。
      tablestore.timeseries.toLowerCase=true
      # 是否将所有非主键以及时间的字段以field的形式存储在时序表,默认为true,如果为false,则只存储tablestore.timeseries.test.field.name中配置的字段
      tablestore.timeseries.mapAll=true
      # 配置field字段名称,多个字段名称之间用半角冒号(,)分隔。
      tablestore.timeseries.test.field.name=cpu
      # 配置field字段类型。取值范围为double、integer、string、binary和boolean。
      # 当field中包含多个字段时,字段类型必须和字段名称一一对应。多个字段类型之间用半角冒号(,)分隔。
      tablestore.timeseries.test.field.type=double
  2. 进入到$KAFKA_HOME目录后,执行以下命令启动standalone模式。

    bin/connect-standalone.sh config/connect-standalone.properties config/connect-tablestore-sink-quickstart.properties

步骤三:生产新的记录

  1. 进入到$KAFKA_HOME目录后,执行以下命令启动一个控制台生产者。

    bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

    配置项说明请参见下表。

    配置项

    示例值

    描述

    --broker-list

    localhost:9092

    Kafka集群broker地址和端口。

    --topic

    test

    主题名称。启动Tablestore Sink Connector时默认会自动创建Topic,您也可以选择手动创建。

  2. 向主题test中写入一些新的消息。

    重要

    如果要导入数据到时序表,则向主题中写入数据时必须输入JSON格式的数据。

    {"m":"cpu","d":"127.0.0.1","region":"shanghai","level":1,"timestamp":1638868699090,"io":5.5,"cpu":"3.5"}
  3. 登录表格存储控制台查看数据。

    fig_20220105

  • 本页导读 (1)
文档反馈