Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Importing Data from a Database with Kafka Connect

Kafka Connect provides a framework for moving data between Kafka and other systems. Moreover, a variety of useful connectors already exist to make this process even easier for common use cases. In this hands-on lab, you will have the opportunity to use Kafka Connect to ingest data from an external database into Kafka. You will use the Java Database Connectivity (JDBC) Connector to automatically load data from a table in a PostgreSQL database into a Kafka topic.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Oct 18, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create a Connector to Load Data from the Database into Kafka

    1. Create a JDBC connector to load data from the PostgreSQL database.
    curl -X POST http://localhost:8083/connectors -H "Content-Type: application/json" -d '{
      "name": "jdbc_source_postgres",
      "config": {
        "connector.class": "io.confluent.connect.jdbc.JdbcSourceConnector",
        "connection.url": "jdbc:postgresql://10.0.1.102:5432/inventory",
        "connection.user": "kafka",
        "connection.password": "Kafka!",
        "topic.prefix": "postgres-",
        "mode":"timestamp",
        "timestamp.column.name": "update_ts"
      }
    }'
    
    1. List the topic. We should see a new topic called postgres-purchases.
    kafka-topics --bootstrap-server localhost:9092 --list
    
    1. Start a console consumer to read from the postgres-purchases topic:
    kafka-console-consumer --bootstrap-server localhost:9092 --topic postgres-purchases --from-beginning
    

    Note: We should see a series of records loaded from the database. Leave the consumer running so we can observe what happens when inserting a new record into the PostgreSQL table.

  2. Challenge

    Insert a New Record for a Purchase of Plums and Observe What Happens to the Topic as a Result

    1. Log in to the PostgreSQL database server.

    2. Change to the postgres user and connect to the inventory database:

    sudo -i -u postgres
    
    psql
    
    \c inventory;
    
    1. Insert a new record into the purchases table:
    insert into purchases (product, quantity, purchase_time, update_ts) VALUES ('plums', 8, current_timestamp, current_timestamp);
    

    Note: If we leave the console consumer running, we should see a new record appear in the postgres-purchases topic containing the data we just inserted.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans