Spring XD Stream – PostgreSQL JDBC Sink

Spring XD JDBC sink can be used to insert message payload data into a relational database table. The tutorial will introduce you how to work with PostgreSQL JDBC Sink

Related Article: Spring XD Stream – How to start with Stream

I. Technologies for Spring XD – PostgreSQL JDBC Sink

– Spring XD 1.3.1.Release
– Postgresql 9.5

II. Practices

Create a stream http | jdbc

1. Start Spring XD with single-node mode

Open a CMD
Go to: ./spring-xd-1.3.1.RELEASE\xd\bin
Run xd-singlenode

2. Create a Stream

Before create a stream, create a sql file int_sink_postgre.sql in folder: \spring-xd-1.3.1.RELEASE\xd\config with content:

DROP TABLE IF EXISTS customer;
 
CREATE TABLE customer (
    CUST_ID Bigserial PRIMARY KEY NOT NULL,
    NAME varchar(100) NOT NULL,
    AGE smallint NOT NULL
);

Go to: .\spring-xd-1.3.1.RELEASE\shell\bin
Run xd-shell

Then run:

stream create --name http_postgres --definition "http --port=9091| jdbc --url=jdbc:postgresql://localhost:5432/testdb --driverClassName=org.postgresql.Driver --username=postgres --password=123 --tableName=customer --columns='cust_id, name, age' --initializeDatabase=true --initializerScript=int_sink_postgre.sql" --deploy

initializeDatabase: whether the database initialization script should be run (boolean, default: false)
initializerScript: the name of the SQL script (in /config) to run if ‘initializeDatabase’ is set (String, default: init_db.sql)

spring-xd postgresql jdbc sink deploy

After deployed, a customer table is created in PostgreSQL

3. Check result

Use xd-shell, run command:

http post --target http://localhost:9091 --data "{\"cust_id\":1, \"name\":\"Jack\", \"age\":\"Davis\"}"

spring xd postgresql jdbc sink post-http

Now, use pgAdminIII, see on customer table

spring-xd postgresql jdbc sink post-http-result

Leave a Reply

Your email address will not be published. Required fields are marked *