用于数据导入的源连接器
学习配置和部署源连接器,将数据库、文件及其他来源的数据导入 Kafka
用于数据导入的源连接器 是 CoddyKit 上的免费 Apache Kafka & Stream Processing Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Apache Kafka & Stream Processing Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Data Ingestion with Connectors
Welcome! In this lesson, we'll dive into Source Connectors, a powerful feature of Kafka Connect.
Source connectors are like bridges. They help you bring data from external systems, such as databases or files, into Kafka topics.
This allows your data to flow seamlessly into your real-time data pipelines.
Why Use Source Connectors?
Imagine you need to move data from a database into Kafka. You could write custom code, but that takes time and effort.
Source Connectors simplify this process by:
- Reducing boilerplate code: No need to write custom producers.
- Providing fault tolerance: They handle failures and resume data transfer.
- Scaling easily: Distribute work across multiple Kafka Connect workers.
- Offering pre-built solutions: Many common connectors are already available.
How Source Connectors Work
At a high level, a source connector operates by:
- Polling Data: It continuously checks the external system for new or updated data.
- Converting Data: It transforms the external data format into Kafka records.
- Producing to Kafka: These records are then sent to a specified Kafka topic.
The Kafka Connect framework manages the connector's lifecycle and distributes its tasks.
Common Source Connector Types
Kafka Connect has a rich ecosystem of connectors. Here are a few popular examples:
- FileStreamSourceConnector: Reads data from local files. Great for initial testing!
- JdbcSourceConnector: Connects to relational databases (like PostgreSQL, MySQL) to pull data.
- S3 Source Connector: Ingests data from Amazon S3 buckets.
- Cloud-specific connectors: For Google Cloud Storage, Azure Blob Storage, etc.
Each connector is designed for a specific data source.
Essential Connector Configuration
When you set up a connector, you provide a configuration. This is typically a JSON or properties file.
Key properties include:
name: A unique name for your connector instance.connector.class: The fully qualified class name of the connector to use (e.g.,FileStreamSourceConnector).tasks.max: The maximum number of tasks the connector can use to parallelize data ingestion.
Other properties are specific to the connector's type.
Example: FileStreamSource Connector
Let's use the FileStreamSourceConnector for a practical example. It's simple and helps demonstrate the core concepts.
This connector reads new lines appended to a specified file and publishes each line as a message to a Kafka topic.
First, let's create a simple input file named test.txt with some initial content.
Configuring Our FileStreamSource
To tell the FileStreamSourceConnector what to do, we create a configuration file. Let's call it file-source-connector.json:
{
"name": "local-file-source",
"config": {
"connector.class": "org.apache.kafka.connect.file.FileStreamSourceConnector",
"tasks.max": "1",
"file": "/path/to/your/test.txt",
"topic": "file-input-topic"
}
}Remember to replace /path/to/your/test.txt with the actual path to your file.
Deploying the Connector via REST
Kafka Connect clusters expose a REST API to manage connectors. We can use curl to deploy our connector.
Assuming your Kafka Connect worker is running on localhost:8083, you'd send a POST request:
curl -X POST -H "Content-Type: application/json" \
--data @file-source-connector.json \
http://localhost:8083/connectorsThis command tells Kafka Connect to create a new connector using the configuration in our JSON file.
Checking Connector Status
After deploying, you'll want to ensure your connector started correctly. You can check its status using another REST API call:
curl http://localhost:8083/connectors/local-file-source/statusLook for the "state": "RUNNING" in the response. If it's FAILED, check the Kafka Connect worker logs for error details.
Once running, any new lines added to test.txt will appear in the file-input-topic in Kafka!
Quick Check: Source Connector Role
What is the primary function of a Kafka Connect Source Connector?
Recap: Ingesting Data with Connectors
Congratulations! You've learned the essentials of Kafka Connect Source Connectors.
- Source Connectors ingest data from external systems into Kafka.
- They offer a code-free, fault-tolerant, and scalable way to integrate data.
- You configure them with properties like
connector.class,name, andtasks.max. - Deployment and monitoring are done via the Kafka Connect REST API.
Next, we'll explore the other side of the coin: Sink Connectors, for exporting data from Kafka!
常见问题解答
「用于数据导入的源连接器」课时是免费的吗?
是的 — 「用于数据导入的源连接器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Apache Kafka & Stream Processing Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
「用于数据导入的源连接器」这节课中我会学到什么?
学习配置和部署源连接器,将数据库、文件及其他来源的数据导入 Kafka 你通过在浏览器中直接运行的动手代码来练习 Apache Kafka & Stream Processing Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Apache Kafka & Stream Processing Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Apache Kafka & Stream Processing Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「用于数据导入的源连接器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Apache Kafka & Stream Processing Fundamentals 课中编写并运行代码吗?
能。每节 Apache Kafka & Stream Processing Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Kafka Connect 简介
- 用于数据导入的源连接器
- 用于数据导出的接收器连接器
- 单消息转换(SMT)