Konektor sink Tablestore melakukan polling pesan dari topik yang telah berlangganan di Kafka, mengurai catatan pesan tersebut, dan mengimpor data secara batch ke tabel data Tablestore.
Prasyarat
-
Kafka telah terinstal, dan ZooKeeper serta Kafka sedang berjalan. Untuk informasi lebih lanjut, lihat dokumentasi resmi Kafka.
-
Aktifkan layanan Tablestore, lalu buat instans dan tabel data. Untuk informasi lebih lanjut, lihat Panduan Cepat untuk model tabel lebar.
CatatanAnda juga dapat menggunakan konektor sink Tablestore untuk membuat tabel data tujuan secara otomatis. Untuk melakukannya, atur parameter `auto.create` ke `true`.
-
Dapatkan AccessKey. Untuk informasi lebih lanjut, lihat Buat AccessKey.
Langkah 1: Deploy Konektor Sink Tablestore
-
Anda dapat memperoleh konektor sink Tablestore dengan salah satu cara berikut.
-
Unduh dan kompilasi kode sumber dari GitHub. Kode sumber tersedia di Kode sumber Konektor Sink Tablestore.
-
Jalankan perintah berikut untuk mengunduh kode sumber konektor sink Tablestore.
git clone https://github.com/aliyun/kafka-connect-tablestore.git -
Masuk ke direktori kode sumber yang telah diunduh dan jalankan perintah berikut untuk mengemas kode menggunakan Maven.
mvn clean package -DskipTestsSetelah kompilasi selesai, paket yang dihasilkan, seperti kafka-connect-tablestore-1.0.jar, disimpan di direktori target.
-
-
Unduh paket kafka-connect-tablestore yang telah dikompilasi.
-
-
Salin paket ke direktori $KAFKA_HOME/libs pada setiap node.
Langkah 2: Mulai Konektor Sink Tablestore
Konektor sink Tablestore mendukung dua mode menjalankan: standalone dan distributed. Anda dapat memilih mode sesuai kebutuhan.
Mode Standalone
-
Modifikasi file konfigurasi worker connect-standalone.properties dan file konfigurasi konektor connect-tablestore-sink-quickstart.properties sesuai kebutuhan.
-
Contoh file konfigurasi worker connect-standalone.properties
Konfigurasi worker mencakup parameter seperti pengaturan koneksi Kafka, format serialisasi, dan frekuensi commit offset. Contoh ini berdasarkan dokumentasi resmi Kafka. Untuk informasi lebih lanjut, lihat 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=true 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= -
Contoh file konfigurasi konektor: connect-tablestore-sink-quickstart.properties
Konfigurasi konektor mencakup parameter seperti kelas konektor, koneksi Tablestore, dan pemetaan data. Untuk informasi lebih lanjut, lihat Konfigurasi.
# Set the connector name. name=tablestore-sink # Specify the connector class. connector.class=TableStoreSinkConnector # Set the maximum number of tasks. tasks.max=1 # Specify the list of Kafka topics from which to export data. topics=test # The following are the Tablestore connection parameter settings. # The endpoint of the Tablestore instance. tablestore.endpoint=https://xxx.xxx.ots.aliyuncs.com # Your AccessKey ID and AccessKey secret. tablestore.access.key.id =xxx tablestore.access.key.secret=xxx # The Tablestore instance name. tablestore.instance.name=xxx # A format string for the destination Tablestore table name. <topic> is a placeholder for the original topic name. The default value is <topic>. # Examples: # table.name.format=kafka_<topic>, message records from the 'test' topic will be written to the 'kafka_test' data table. # table.name.format= # The primary key mode. The default value is kafka. # The primary key of the Tablestore data table will be composed of <topic>_<partition> (the Kafka topic and partition, separated by "_") and <offset> (the offset of the message record in the partition). # primarykey.mode= # Automatically create the destination table. The default value is false. auto.create=true
-
-
Masuk ke direktori $KAFKA_HOME dan jalankan perintah berikut untuk memulai konektor dalam mode standalone.
bin/connect-standalone.sh config/connect-standalone.properties config/connect-tablestore-sink-quickstart.properties
Mode Distributed
-
Modifikasi file konfigurasi worker connect-distributed.properties sesuai kebutuhan.
Konfigurasi worker mencakup parameter seperti pengaturan koneksi Kafka, format serialisasi, dan frekuensi commit offset. Konfigurasi ini juga menentukan topik yang menyimpan informasi untuk setiap konektor. Anda harus membuat topik-topik tersebut terlebih dahulu. Contoh ini berdasarkan dokumentasi resmi Kafka. Untuk informasi lebih lanjut, lihat Kafka Connect.
-
offset.storage.topic: Topik compact yang digunakan untuk menyimpan offset setiap konektor.
-
config.storage.topic: Topik compact yang digunakan untuk menyimpan konfigurasi konektor dan task. Jumlah partisi untuk topik ini harus diatur menjadi 1.
-
status.storage.topic: Topik compact yang digunakan untuk menyimpan informasi status 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. ## # This file contains some of the configurations for the Kafka Connect distributed worker. This file is intended # to be used with the examples, and some settings may differ from those used in a production system, especially # the `bootstrap.servers` and those specifying replication factors. # A list of host/port pairs to use for establishing the initial connection to the Kafka cluster. bootstrap.servers=localhost:9092 # unique name for the cluster, used in forming the Connect cluster group. Note that this must not conflict with consumer group IDs group.id=connect-cluster # 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=true # Topic to use for storing offsets. This topic should have many partitions and be replicated and compacted. # Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create # the topic before starting Kafka Connect if a specific topic configuration is needed. # Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. # Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able # to run this example on a single-broker cluster and so here we instead set the replication factor to 1. offset.storage.topic=connect-offsets offset.storage.replication.factor=1 #offset.storage.partitions=25 # Topic to use for storing connector and task configurations; note that this should be a single partition, highly replicated, # and compacted topic. Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create # the topic before starting Kafka Connect if a specific topic configuration is needed. # Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. # Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able # to run this example on a single-broker cluster and so here we instead set the replication factor to 1. config.storage.topic=connect-configs config.storage.replication.factor=1 # Topic to use for storing statuses. This topic can have multiple partitions and should be replicated and compacted. # Kafka Connect will attempt to create the topic automatically when needed, but you can always manually create # the topic before starting Kafka Connect if a specific topic configuration is needed. # Most users will want to use the built-in default replication factor of 3 or in some cases even specify a larger value. # Since this means there must be at least as many brokers as the maximum replication factor used, we'd like to be able # to run this example on a single-broker cluster and so here we instead set the replication factor to 1. status.storage.topic=connect-status status.storage.replication.factor=1 #status.storage.partitions=5 # Flush much faster than normal, which is useful for testing/debugging offset.flush.interval.ms=10000 # These are provided to inform the user about the presence of the REST host and port configs # Hostname & Port for the REST API to listen on. If this is set, it will bind to the interface used to listen to requests. #rest.host.name= #rest.port=8083 # The Hostname & Port that will be given out to other workers to connect to i.e. URLs that are routable from other servers. #rest.advertised.host.name= #rest.advertised.port= # 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 # Examples: # plugin.path=/usr/local/share/java,/usr/local/share/kafka/plugins,/opt/connectors, #plugin.path= -
-
Masuk ke direktori $KAFKA_HOME dan jalankan perintah berikut untuk memulai konektor dalam mode distributed.
PentingJalankan proses worker pada setiap node.
bin/connect-distributed.sh config/connect-distributed.properties -
Anda dapat mengelola konektor menggunakan REST API. Untuk informasi lebih lanjut, lihat REST API.
-
Buat file connect-tablestore-sink-quickstart.json di path config dan tambahkan konten contoh berikut.
File konfigurasi konektor menentukan parameter sebagai pasangan kunci-nilai dalam format JSON. Parameter tersebut mencakup kelas konektor, pengaturan koneksi Tablestore, dan pemetaan data. Untuk informasi lebih lanjut, lihat Konfigurasi.
{ "name": "tablestore-sink", "config": { "connector.class":"TableStoreSinkConnector", "tasks.max":"1", "topics":"test", "tablestore.endpoint":"https://xxx.xxx.ots.aliyuncs.com", "tablestore.access.key.id":"xxx", "tablestore.access.key.secret":"xxx", "tablestore.instance.name":"xxx", "table.name.format":"<topic>", "primarykey.mode":"kafka", "auto.create":"true" } } -
Jalankan perintah berikut untuk memulai konektor sink Tablestore.
curl -i -k -H "Content-type: application/json" -X POST -d @config/connect-tablestore-sink-quickstart.json http://localhost:8083/connectorshttp://localhost:8083/connectorsadalah alamat layanan REST Kafka. Sesuaikan alamat ini sesuai kebutuhan.
-
Langkah 3: Hasilkan catatan baru
-
Masuk ke direktori $KAFKA_HOME dan jalankan perintah berikut untuk memulai produsen konsol.
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic testUntuk deskripsi parameter, lihat tabel berikut.
Item Konfigurasi
Contoh
Deskripsi
--broker-list
localhost:9092
Alamat dan port broker kluster Kafka.
--topic
test
Nama topik. Topik dibuat secara otomatis secara default saat Konektor Sink Tablestore dimulai. Anda juga dapat membuat topik secara manual.
-
Tulis pesan ke topik test.
-
Pesan tipe struct
{ "schema":{ "type":"struct", "fields":[ { "type":"int32", "optional":false, "field":"id" }, { "type":"string", "optional":false, "field":"product" }, { "type":"int64", "optional":false, "field":"quantity" }, { "type":"double", "optional":false, "field":"price" } ], "optional":false, "name":"record" }, "payload":{ "id":1, "product":"foo", "quantity":100, "price":50 } } -
Pesan tipe map
{ "schema":{ "type":"map", "keys":{ "type":"string", "optional":false }, "values":{ "type":"int32", "optional":false }, "optional":false }, "payload":{ "id":1 } }
-
-
Masuk ke Konsol Tablestore untuk melihat data.
Tabel data bernama test dibuat secara otomatis di instans Tablestore. Tabel hasil kueri berisi kolom-kolom berikut: topic_partition (kunci primer), offset (kunci primer), id, price, product, dan quantity. Pada data sampel, catatan pertama memiliki topic_partition=
test_3, offset=0, id=AAAAAQ==, dan bidang lainnya kosong. Catatan kedua memiliki topic_partition=test_34, offset=0, id=1, price=50.0, product=foo, dan quantity=100.