|
| 1 | +--- |
| 2 | +title: Loading from Kafka with bend-ingest-kafka |
| 3 | +--- |
| 4 | + |
| 5 | +In this tutorial, we'll guide you through setting up a Kafka environment using Docker and loading messages from Kafka into Databend Cloud with [bend-ingest-kafka](https://github.com/databendcloud/bend-ingest-kafka). |
| 6 | + |
| 7 | +### Step 1: Setting up Kafka Environment |
| 8 | + |
| 9 | +Run the Apache Kafka Docker container on port 9092: |
| 10 | + |
| 11 | +```shell |
| 12 | +MacBook-Air:~ eric$ docker run -d \ |
| 13 | +> --name kafka \ |
| 14 | +> -p 9092:9092 \ |
| 15 | +> apache/kafka:latest |
| 16 | +Unable to find image 'apache/kafka:latest' locally |
| 17 | +latest: Pulling from apache/kafka |
| 18 | +690e87867337: Pull complete |
| 19 | +5dddb19fae62: Pull complete |
| 20 | +86caa4220d9f: Pull complete |
| 21 | +7802c028acb4: Pull complete |
| 22 | +16a3d1421c02: Pull complete |
| 23 | +ab648c7f18ee: Pull complete |
| 24 | +a917a90b7df6: Pull complete |
| 25 | +4e446fc89158: Pull complete |
| 26 | +f800ce0fc22f: Pull complete |
| 27 | +a2e5e46262c3: Pull complete |
| 28 | +Digest: sha256:c89f315cff967322c5d2021434b32271393cb193aa7ec1d43e97341924e57069 |
| 29 | +Status: Downloaded newer image for apache/kafka:latest |
| 30 | +0261b8f3d5fde74f5f20340b58cb85d29d9b40ee4f48f1df2c41a68b616d22dc |
| 31 | +``` |
| 32 | + |
| 33 | +### Step 2: Create Topic & Produce Messages |
| 34 | + |
| 35 | +1. Access the Kafka container: |
| 36 | + |
| 37 | +```shell |
| 38 | +MacBook-Air:~ eric$ docker exec --workdir /opt/kafka/bin/ -it kafka sh |
| 39 | +``` |
| 40 | + |
| 41 | +2. Create a new Kafka topic named `test-topic`: |
| 42 | + |
| 43 | +```shell |
| 44 | +/opt/kafka/bin $ ./kafka-topics.sh --bootstrap-server localhost:9092 --create --topic test-topic |
| 45 | +Created topic test-topic. |
| 46 | +``` |
| 47 | + |
| 48 | +3. Produce messages to the test-topic using the Kafka console producer: |
| 49 | + |
| 50 | +```shell |
| 51 | +/opt/kafka/bin $ ./kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic |
| 52 | +``` |
| 53 | + |
| 54 | +4. Enter messages in JSON format: |
| 55 | + |
| 56 | +```json |
| 57 | +{"id": 1, "name": "Alice", "age": 30} |
| 58 | +{"id": 2, "name": "Bob", "age": 25} |
| 59 | +``` |
| 60 | + |
| 61 | +5. Stop the producer with Ctrl+C once done. |
| 62 | + |
| 63 | +### Step 3: Create Table in Databend Cloud |
| 64 | + |
| 65 | +Create the target table in Databend Cloud: |
| 66 | + |
| 67 | +```sql |
| 68 | +CREATE DATABASE doc; |
| 69 | + |
| 70 | +CREATE TABLE databend_topic ( |
| 71 | + id INT NOT NULL, |
| 72 | + name VARCHAR NOT NULL, |
| 73 | + age INT NOT NULL |
| 74 | + ) ENGINE=FUSE; |
| 75 | +``` |
| 76 | + |
| 77 | +### Step 4: Install & Run bend-ingest-kafka |
| 78 | + |
| 79 | +1. Install the bend-ingest-kafka tool by running the following command: |
| 80 | + |
| 81 | +```shell |
| 82 | +go install github.com/databendcloud/bend-ingest-kafka@latest |
| 83 | +``` |
| 84 | + |
| 85 | +2. Run the following command to ingest messages from the `test-topic` Kafka topic into the target table in Databend Cloud: |
| 86 | + |
| 87 | +```shell |
| 88 | +MacBook-Air:~ eric$ bend-ingest-kafka \ |
| 89 | +> --kafka-bootstrap-servers="localhost:9092" \ |
| 90 | +> --kafka-topic="test-topic" \ |
| 91 | +> --databend-dsn="<your-dsn>" \ |
| 92 | +> --databend-table="doc.databend_topic" \ |
| 93 | +> --data-format="json" |
| 94 | +INFO[0000] Starting worker worker-0 |
| 95 | +WARN[0072] Failed to read message from Kafka: context deadline exceeded kafka_batch_reader=ReadBatch |
| 96 | +2024/08/20 15:10:15 ingest 2 rows (1.225576 rows/s), 75 bytes (45.959100 bytes/s) |
| 97 | +``` |
| 98 | + |
| 99 | +3. In Databend Cloud, verify that the data has been successfully loaded: |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments