Giter Club home page Giter Club logo

kafka-connect-cdc-example's Introduction

Kafka connect CDC example from Postgresql to ElasticSearch

In this project we are going to sync a table in Postgresql with ElasticSerach index, such that every change in the table will change the index too. For the sync we will use kafka connect.

dependencies:

  • Docker
  • Curl or any other tool for sending http requests

Getting started

Clone repo, cd into directory, and checkout appropriate branch.

setup

From the kafka-connect-CDC-example directory:

$ docker-compose up -d

And that is it! Wait a few minute and then you can check that all the services are up.

I recommends to enter the Control Center and see that everithing is OK. it should look like this:

control-img.png

Note that the cluster mark as "Unhealthy" because it has less then 3 brokers.

Next let's verify that postgres and ES started fine. For the ES use $ curl http://localhost:9200.

For postgres we will try to connect the DB using another container:

$ docker run -it --network kafkaconnectcdcexample_default --name psql \
--link postgres:postgres --rm postgres sh -c 'psql -h postgres -U postgres -d cdc';

Password for user postgres:

Enter postgres as password.

psql (12.3 (Debian 12.3-1.pgdg100+1), server 11.8 (Debian 11.8-1.pgdg90+1))
Type "help" for help.

cdc=#

create the Connectors

We going to have 2 connectors. One is a source who read from Postgres, the other one is a sink connector who insert the data into ES. To create them we will use curl:

Postgres source:

$ curl -i -X POST -H "Accept:application/json" -H "Content-Type:application/json" -d @postgres-source.json http://localhost:8083/connectors;

And ES sink:

$ curl -i -X POST -H "Accept:application/json" -H "Content-Type:application/json" -d @es-sink.json http://localhost:8083/connectors;

try it out!

Finally we can try adding and editing records and see the changes in our ES. Let's connect to the Postgres cluster:

$ docker run -it --network kafkaconnectcdcexample_default --name psql \
--link postgres:postgres --rm postgres sh -c 'psql -h postgres -U postgres -d cdc';

Password for user postgres:

Enter postgres as password.

psql (12.3 (Debian 12.3-1.pgdg100+1), server 11.8 (Debian 11.8-1.pgdg90+1))
Type "help" for help.

cdc=#

next, create the table 'customers' and add some records:

CREATE TABLE customers (
	user_id serial PRIMARY KEY,
	username VARCHAR ( 50 ) UNIQUE NOT NULL,
	password VARCHAR ( 50 ) NOT NULL,
	email VARCHAR ( 255 ) UNIQUE NOT NULL,
	created_on TIMESTAMP NOT NULL,
    last_login TIMESTAMP 
);

insert into customers (username,password,email,created_on,last_login) 
values ('user1','pass3','[email protected]',current_timestamp,current_timestamp),
	   ('user2','pass3','[email protected]',current_timestamp,current_timestamp),
	   ('david','pass4','[email protected]',current_timestamp,current_timestamp);

Now, we can see them in ES:

curl http://localhost:9200/dbserver.public.customers/_search?pretty

And if we change soome records:

update customers 
set password='newpass2' 
where username='david';

delete from customers 
where username='user1';

The records will be updated in our ES!

Services list

service address
Control center http://localhost:9021
Kafka http://localhost:9092
Zookeeper http://localhost:2181
Kafka-Connect http://localhost:8083
Postgres http://localhost:5432
ElasticSearch http://localhost:9200

kafka-connect-cdc-example's People

Contributors

itamarg365 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.