It is helpful to review the concepts for Pulsar I/O with running the steps in this guide to gain a deeper understanding.
At the end of this tutorial, you are able to:
Before connecting Pulsar to a database, you need to install Pulsar and the desired built-in connector.
For more information about how to install a standalone Pulsar and built-in connectors, see .
Start Pulsar locally.
All the components of a Pulsar service are start in order.
You can curl those pulsar service endpoints to make sure Pulsar service is up running correctly.
Check Pulsar binary protocol port.
Check Pulsar Function cluster.
curl -s http://localhost:8080/admin/v2/worker/cluster
Example output
[{"workerId":"c-standalone-fw-localhost-6750","workerHostname":"localhost","port":6750}]
Make sure a public tenant and a default namespace exist.
curl -s http://localhost:8080/admin/v2/namespaces/public
Example output
["public/default","public/functions"]
All built-in connectors should be listed as available.
curl -s http://localhost:8080/admin/v2/functions/connectors
Example output
[{"name":"aerospike","description":"Aerospike database sink","sinkClass":"org.apache.pulsar.io.aerospike.AerospikeStringSink"},{"name":"cassandra","description":"Writes data into Cassandra","sinkClass":"org.apache.pulsar.io.cassandra.CassandraStringSink"},{"name":"kafka","description":"Kafka source and sink connector","sourceClass":"org.apache.pulsar.io.kafka.KafkaStringSource","sinkClass":"org.apache.pulsar.io.kafka.KafkaBytesSink"},{"name":"kinesis","description":"Kinesis sink connector","sinkClass":"org.apache.pulsar.io.kinesis.KinesisSink"},{"name":"rabbitmq","description":"RabbitMQ source connector","sourceClass":"org.apache.pulsar.io.rabbitmq.RabbitMQSource"},{"name":"twitter","description":"Ingest data from Twitter firehose","sourceClass":"org.apache.pulsar.io.twitter.TwitterFireHose"}]
If an error occurs when starting Pulsar service, you may see an exception at the terminal running
pulsar/standalone
, or you can navigate to thelogs
directory under the Pulsar directory to view the logs.
This section demonstrates how to connect Pulsar to Cassandra.
Tip
Make sure you have Docker installed. If you do not have one, see .
The Cassandra sink connector reads messages from Pulsar topics and writes the messages into Cassandra tables. For more information, see Cassandra sink connector.
This example uses cassandra
Docker image to start a single-node Cassandra cluster in Docker.
Start a Cassandra cluster.
docker run -d --rm --name=cassandra -p 9042:9042 cassandra
Note
Before moving to the next steps, make sure the Cassandra cluster is running.
Make sure the Docker process is running.
docker ps
Check the Cassandra logs to make sure the Cassandra process is running as expected.
docker logs cassandra
Check the status of the Cassandra cluster.
docker exec cassandra nodetool status
Example output
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 172.17.0.2 103.67 KiB 256 100.0% af0e4b2f-84e0-4f0b-bb14-bd5f9070ff26 rack1
Use
cqlsh
to connect to the Cassandra cluster.$ docker exec -ti cassandra cqlsh localhost
Connected to Test Cluster at localhost:9042.
[cqlsh 5.0.1 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh>
Create a keyspace
pulsar_test_keyspace
.cqlsh> CREATE KEYSPACE pulsar_test_keyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};
Create a table
pulsar_test_table
.cqlsh> USE pulsar_test_keyspace;
cqlsh:pulsar_test_keyspace> CREATE TABLE pulsar_test_table (key text PRIMARY KEY, col text);
Configure a Cassandra sink
Now that we have a Cassandra cluster running locally.
In this section, you need to configure a Cassandra sink connector.
To run a Cassandra sink connector, you need to prepare a configuration file including the information that Pulsar connector runtime needs to know.
For example, how Pulsar connector can find the Cassandra cluster, what is the keyspace and the table that Pulsar connector uses for writing Pulsar messages to, and so on.
You can create a configuration file through one of the following methods.
-
{
"roots": "localhost:9042",
"keyspace": "pulsar_test_keyspace",
"columnFamily": "pulsar_test_table",
"keyname": "key",
"columnName": "col"
}
YAML
configs:
roots: "localhost:9042"
keyspace: "pulsar_test_keyspace"
columnFamily: "pulsar_test_table"
keyname: "key"
columnName: "col"
For more information, see Cassandra sink connector.
Create a Cassandra sink
You can use the Connector Admin CLI to create a sink connector and perform other operations on them.
Run the following command to create a Cassandra sink connector with sink type cassandra and the config file examples/cassandra-sink.yml created previously.
bin/pulsar-admin sinks create \
--tenant public \
--namespace default \
--name cassandra-test-sink \
--sink-type cassandra \
--sink-config-file examples/cassandra-sink.yml \
--inputs test_cassandra
Once the command is executed, Pulsar creates the sink connector cassandra-test-sink.
This sink connector runs as a Pulsar Function and writes the messages produced in the topic test_cassandra to the Cassandra table pulsar_test_table.
Inspect a Cassandra sink
You can use the Connector Admin CLI to monitor a connector and perform other operations on it.
Get the information of a Cassandra sink.
bin/pulsar-admin sinks get \
--tenant public \
--namespace default \
--name cassandra-test-sink
Example output
Check the status of a Cassandra sink.
bin/pulsar-admin sinks status \
--tenant public \
--namespace default \
--name cassandra-test-sink
Example output
{
"numInstances" : 1,
"numRunning" : 1,
"instanceId" : 0,
"status" : {
"running" : true,
"error" : "",
"numRestarts" : 0,
"numReadFromPulsar" : 0,
"numSystemExceptions" : 0,
"latestSystemExceptions" : [ ],
"numSinkExceptions" : 0,
"latestSinkExceptions" : [ ],
"numWrittenToSink" : 0,
"lastReceivedTime" : 0,
"workerId" : "c-standalone-fw-localhost-8080"
}
} ]
}
Produce some messages to the input topic of the Cassandra sink test_cassandra.
for i in {0..9}; do bin/pulsar-client produce -m "key-$i" -n 1 test_cassandra; done
Inspect the status of the Cassandra sink test_cassandra.
bin/pulsar-admin sinks status \
--tenant public \
--namespace default \
--name cassandra-test-sink
You can see 10 messages are processed by the Cassandra sink test_cassandra.
Example output
{
"numInstances" : 1,
"numRunning" : 1,
"instances" : [ {
"instanceId" : 0,
"status" : {
"running" : true,
"error" : "",
"numRestarts" : 0,
"numReadFromPulsar" : 10,
"numSystemExceptions" : 0,
"latestSystemExceptions" : [ ],
"numSinkExceptions" : 0,
"numWrittenToSink" : 10,
"lastReceivedTime" : 1551685489136,
"workerId" : "c-standalone-fw-localhost-8080"
}
} ]
}
Use
cqlsh
to connect to the Cassandra cluster.docker exec -ti cassandra cqlsh localhost
Check the data of the Cassandra table pulsar_test_table.
cqlsh> use pulsar_test_keyspace;
cqlsh:pulsar_test_keyspace> select * from pulsar_test_table;
key | col
--------+--------
key-5 | key-5
key-0 | key-0
key-9 | key-9
key-2 | key-2
key-1 | key-1
key-3 | key-3
key-6 | key-6
key-7 | key-7
key-4 | key-4
key-8 | key-8
Delete a Cassandra Sink
You can use the Connector Admin CLI to delete a connector and perform other operations on it.
bin/pulsar-admin sinks delete \
--tenant public \
--namespace default \
--name cassandra-test-sink
This section demonstrates how to connect Pulsar to PostgreSQL.
Tip
Make sure you have Docker installed. If you do not have one, see install Docker.
The JDBC sink connector pulls messages from Pulsar topics and persists the messages to ClickHouse, MariaDB, PostgreSQL, or SQlite. For more information, see .
Setup a PostgreSQL cluster
This example uses the PostgreSQL 12 docker image to start a single-node PostgreSQL cluster in Docker.
Pull the PostgreSQL 12 image from Docker.
$ docker pull postgres:12
Start PostgreSQL.
$ docker run -d -it --rm \
--name pulsar-postgres \
-p 5432:5432 \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_USER=postgres \
postgres:12
Tip
Tip
For more information about Docker commands, see .
Check if PostgreSQL has been started successfully.
$ docker logs -f pulsar-postgres
PostgreSQL has been started successfully if the following message appears.
2020-05-11 20:09:24.492 UTC [1] LOG: starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-05-11 20:09:24.492 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2020-05-11 20:09:24.492 UTC [1] LOG: listening on IPv6 address "::", port 5432
2020-05-11 20:09:24.499 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-05-11 20:09:24.523 UTC [55] LOG: database system was shut down at 2020-05-11 20:09:24 UTC
2020-05-11 20:09:24.533 UTC [1] LOG: database system is ready to accept connections
Access to PostgreSQL.
$ docker exec -it pulsar-postgres /bin/bash
Create a PostgreSQL table pulsar_postgres_jdbc_sink.
$ psql -U postgres postgres
postgres=# create table if not exists pulsar_postgres_jdbc_sink
(
id serial PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
Configure a JDBC sink
Now we have a PostgreSQL running locally.
In this section, you need to configure a JDBC sink connector.
Add a configuration file.
To run a JDBC sink connector, you need to prepare a YAML configuration file including the information that Pulsar connector runtime needs to know.
For example, how Pulsar connector can find the PostgreSQL cluster, what is the JDBC URL and the table that Pulsar connector uses for writing messages to.
Create a pulsar-postgres-jdbc-sink.yaml file, copy the following contents to this file, and place the file in the
pulsar/connectors
folder.configs:
userName: "postgres"
password: "password"
jdbcUrl: "jdbc:postgresql://localhost:5432/pulsar_postgres_jdbc_sink"
tableName: "pulsar_postgres_jdbc_sink"
Create a schema.
Create a avro-schema file, copy the following contents to this file, and place the file in the
pulsar/connectors
folder.{
"type": "AVRO",
"schema": "{\"type\":\"record\",\"name\":\"Test\",\"fields\":[{\"name\":\"id\",\"type\":[\"null\",\"int\"]},{\"name\":\"name\",\"type\":[\"null\",\"string\"]}]}",
"properties": {}
}
-
This example uploads the avro-schema schema to the pulsar-postgres-jdbc-sink-topic topic.
$ bin/pulsar-admin schemas upload pulsar-postgres-jdbc-sink-topic -f ./connectors/avro-schema
Check if the schema has been uploaded successfully.
$ bin/pulsar-admin schemas get pulsar-postgres-jdbc-sink-topic
The schema has been uploaded successfully if the following message appears.
{"name":"pulsar-postgres-jdbc-sink-topic","schema":"{\"type\":\"record\",\"name\":\"Test\",\"fields\":[{\"name\":\"id\",\"type\":[\"null\",\"int\"]},{\"name\":\"name\",\"type\":[\"null\",\"string\"]}]}","type":"AVRO","properties":{}}
You can use the to create a sink connector and perform other operations on it.
This example creates a sink connector and specifies the desired information.
Once the command is executed, Pulsar creates a sink connector pulsar-postgres-jdbc-sink.
This sink connector runs as a Pulsar Function and writes the messages produced in the topic pulsar-postgres-jdbc-sink-topic to the PostgreSQL table pulsar_postgres_jdbc_sink.
Tip
标记 | Description | This example |
---|---|---|
—archive | The path to the archive file for the sink. | pulsar-io-jdbc-postgres-2.6.1.nar |
—inputs | The input topic(s) of the sink. Multiple topics can be specified as a comma-separated list. | |
—name | The name of the sink. | pulsar-postgres-jdbc-sink |
—sink-config-file | The path to a YAML config file specifying the configuration of the sink. | pulsar-postgres-jdbc-sink.yaml |
—parallelism | The parallelism factor of the sink. For example, the number of sink instances to run. | 1 |
Tip
For more information about
pulsar-admin sinks create options
, see here.
The sink has been created successfully if the following message appears.
Inspect a JDBC sink
You can use the Connector Admin CLI to monitor a connector and perform other operations on it.
List all running JDBC sink(s).
$ bin/pulsar-admin sinks list \
--tenant public \
--namespace default
Tip
For more information about
pulsar-admin sinks list options
, see here.The result shows that only the postgres-jdbc-sink sink is running.
[
"pulsar-postgres-jdbc-sink"
]
Get the information of a JDBC sink.
$ bin/pulsar-admin sinks get \
--tenant public \
--namespace default \
--name pulsar-postgres-jdbc-sink
Tip
For more information about
pulsar-admin sinks get options
, see here.The result shows the information of the sink connector, including tenant, namespace, topic and so on.
{
"tenant": "public",
"namespace": "default",
"name": "pulsar-postgres-jdbc-sink",
"className": "org.apache.pulsar.io.jdbc.PostgresJdbcAutoSchemaSink",
"inputSpecs": {
"pulsar-postgres-jdbc-sink-topic": {
}
},
"configs": {
"password": "password",
"jdbcUrl": "jdbc:postgresql://localhost:5432/pulsar_postgres_jdbc_sink",
"userName": "postgres",
"tableName": "pulsar_postgres_jdbc_sink"
},
"parallelism": 1,
"processingGuarantees": "ATLEAST_ONCE",
"retainOrdering": false,
"autoAck": true
}
Get the status of a JDBC sink
$ bin/pulsar-admin sinks status \
--tenant public \
--namespace default \
--name pulsar-postgres-jdbc-sink
Tip
For more information about
pulsar-admin sinks status options
, see here.The result shows the current status of sink connector, including the number of instance, running status, worker ID and so on.
{
"numInstances" : 1,
"numRunning" : 1,
"instances" : [ {
"instanceId" : 0,
"status" : {
"running" : true,
"error" : "",
"numRestarts" : 0,
"numReadFromPulsar" : 0,
"numSystemExceptions" : 0,
"latestSystemExceptions" : [ ],
"numSinkExceptions" : 0,
"latestSinkExceptions" : [ ],
"numWrittenToSink" : 0,
"lastReceivedTime" : 0,
"workerId" : "c-standalone-fw-192.168.2.52-8080"
}
} ]
}
Stop a JDBC sink
You can use the Connector Admin CLI to stop a connector and perform other operations on it.
$ bin/pulsar-admin sinks stop \
--tenant public \
--namespace default \
--name pulsar-postgres-jdbc-sink
The sink instance has been stopped successfully if the following message disappears.
"Stopped successfully"
Restart a JDBC sink
You can use the Connector Admin CLI to restart a connector and perform other operations on it.
$ bin/pulsar-admin sinks restart \
--tenant public \
--namespace default \
--name pulsar-postgres-jdbc-sink
Tip
For more information about
pulsar-admin sinks restart options
, see here.
The sink instance has been started successfully if the following message disappears.
"Started successfully"
Tip
Optionally, you can run a standalone sink connector using
pulsar-admin sinks localrun options
.Note that
pulsar-admin sinks localrun options
runs a sink connector locally, whilepulsar-admin sinks start options
starts a sink connector in a cluster.For more information about
pulsar-admin sinks localrun options
, see here.
You can use the to update a connector and perform other operations on it.
This example updates the parallelism of the pulsar-postgres-jdbc-sink sink connector to 2.
$ bin/pulsar-admin sinks update \
--name pulsar-postgres-jdbc-sink \
--parallelism 2
Tip
For more information about
pulsar-admin sinks update options
, see .
The sink connector has been updated successfully if the following message disappears.
"Updated successfully"
This example double-checks the information.
$ bin/pulsar-admin sinks get \
--tenant public \
--namespace default \
--name pulsar-postgres-jdbc-sink
The result shows that the parallelism is 2.
{
"tenant": "public",
"namespace": "default",
"name": "pulsar-postgres-jdbc-sink",
"className": "org.apache.pulsar.io.jdbc.PostgresJdbcAutoSchemaSink",
"inputSpecs": {
"pulsar-postgres-jdbc-sink-topic": {
"isRegexPattern": false
}
},
"configs": {
"password": "password",
"jdbcUrl": "jdbc:postgresql://localhost:5432/pulsar_postgres_jdbc_sink",
"userName": "postgres",
"tableName": "pulsar_postgres_jdbc_sink"
},
"parallelism": 2,
"processingGuarantees": "ATLEAST_ONCE",
"retainOrdering": false,
"autoAck": true
}
Delete a JDBC sink
You can use the to delete a connector and perform other operations on it.
This example deletes the pulsar-postgres-jdbc-sink sink connector.
$ bin/pulsar-admin sinks delete \
--tenant public \
--namespace default \
--name pulsar-postgres-jdbc-sink
Tip
For more information about
pulsar-admin sinks delete options
, see .
The sink connector has been deleted successfully if the following message appears.
"Deleted successfully"
$ bin/pulsar-admin sinks get \
--tenant public \
--namespace default \
--name pulsar-postgres-jdbc-sink
The result shows that the sink connector does not exist.
HTTP 404 Not Found