此内容没有您所选择的语言版本。
Chapter 2. Kafka Bridge quickstart
Use this quickstart to try out the Kafka Bridge in your local development environment.
You will learn how to do the following:
- Produce messages to topics and partitions in your Kafka cluster
- Create a Kafka Bridge consumer
- Perform basic consumer operations, such as subscribing the consumer to topics and retrieving the messages that you produced
In this quickstart, HTTP requests are formatted as curl commands that you can copy and paste to your terminal.
Ensure you have the prerequisites and then follow the tasks in the order provided in this chapter.
In this quickstart, you will produce and consume messages in JSON format.
Prerequisites for the quickstart
- A Kafka cluster is running on the host machine.
2.1. Downloading a Kafka Bridge archive 复制链接链接已复制到粘贴板!
A zipped distribution of the Kafka Bridge is available for download.
Procedure
- Download the latest version of the Kafka Bridge archive from the Customer Portal.
2.2. Installing the Kafka Bridge 复制链接链接已复制到粘贴板!
Use the script provided with the Kafka Bridge archive to install the Kafka Bridge. The application.properties file provided with the installation archive provides default configuration settings.
The following default property values configure the Kafka Bridge to listen for requests on port 8080.
Default configuration properties
http.host=0.0.0.0 http.port=8080
http.host=0.0.0.0
http.port=8080
Prerequisites
Procedure
- If you have not already done so, unzip the Kafka Bridge installation archive to any directory.
Run the Kafka Bridge script using the configuration properties as a parameter:
For example:
./bin/kafka_bridge_run.sh --config-file=<path>/application.properties
./bin/kafka_bridge_run.sh --config-file=<path>/application.propertiesCopy to Clipboard Copied! Toggle word wrap Toggle overflow Check to see that the installation was successful in the log.
HTTP-Kafka Bridge started and listening on port 8080 HTTP-Kafka Bridge bootstrap servers localhost:9092
HTTP-Kafka Bridge started and listening on port 8080 HTTP-Kafka Bridge bootstrap servers localhost:9092Copy to Clipboard Copied! Toggle word wrap Toggle overflow
What to do next
2.3. Producing messages to topics and partitions 复制链接链接已复制到粘贴板!
Use the Kafka Bridge to produce messages to a Kafka topic in JSON format by using the topics endpoint.
You can produce messages to topics in JSON format by using the topics endpoint. You can specify destination partitions for messages in the request body. The partitions endpoint provides an alternative method for specifying a single destination partition for all messages as a path parameter.
In this procedure, messages are produced to a topic called bridge-quickstart-topic.
Prerequisites
The Kafka cluster has a topic with three partitions.
You can use the
kafka-topics.shutility to create topics.Example topic creation with three partitions
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic bridge-quickstart-topic --partitions 3 --replication-factor 1
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic bridge-quickstart-topic --partitions 3 --replication-factor 1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verifying the topic was created
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic bridge-quickstart-topic
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic bridge-quickstart-topicCopy to Clipboard Copied! Toggle word wrap Toggle overflow
If you deployed Streams for Apache Kafka on OpenShift, you can create a topic using the KafkaTopic custom resource.
Procedure
Using the Kafka Bridge, produce three messages to the topic you created:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
sales-lead-0001is sent to a partition based on the hash of the key. -
sales-lead-0002is sent directly to partition 2. -
sales-lead-0003is sent to a partition in thebridge-quickstart-topictopic using a round-robin method.
-
If the request is successful, the Kafka Bridge returns an
offsetsarray, along with a200code and acontent-typeheader ofapplication/vnd.kafka.v2+json. For each message, theoffsetsarray describes:- The partition that the message was sent to
The current message offset of the partition
Example response
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional topic requests
Make other curl requests to find information on topics and partitions.
- List topics
curl -X GET \ http://localhost:8080/topics
curl -X GET \ http://localhost:8080/topicsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example response
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Get topic configuration and partition details
curl -X GET \ http://localhost:8080/topics/bridge-quickstart-topic
curl -X GET \ http://localhost:8080/topics/bridge-quickstart-topicCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example response
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - List the partitions of a specific topic
curl -X GET \ http://localhost:8080/topics/bridge-quickstart-topic/partitions
curl -X GET \ http://localhost:8080/topics/bridge-quickstart-topic/partitionsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example response
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - List the details of a specific topic partition
curl -X GET \ http://localhost:8080/topics/bridge-quickstart-topic/partitions/0
curl -X GET \ http://localhost:8080/topics/bridge-quickstart-topic/partitions/0Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example response
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - List the offsets of a specific topic partition
curl -X GET \ http://localhost:8080/topics/bridge-quickstart-topic/partitions/0/offsets
curl -X GET \ http://localhost:8080/topics/bridge-quickstart-topic/partitions/0/offsetsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example response
{ "beginning_offset": 0, "end_offset": 1 }{ "beginning_offset": 0, "end_offset": 1 }Copy to Clipboard Copied! Toggle word wrap Toggle overflow
What to do next
After producing messages to topics and partitions, create a Kafka Bridge consumer.
2.4. Creating a Kafka Bridge consumer 复制链接链接已复制到粘贴板!
Before you can perform any consumer operations in the Kafka cluster, you must first create a consumer by using the consumers endpoint. The consumer is referred to as a Kafka Bridge consumer.
Procedure
Create a Kafka Bridge consumer in a new consumer group named
bridge-quickstart-consumer-group:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
The consumer is named
bridge-quickstart-consumerand the embedded data format is set asjson. - Some basic configuration settings are defined.
The consumer will not commit offsets to the log automatically because the
enable.auto.commitsetting isfalse. You will commit the offsets manually later in this quickstart.If the request is successful, the Kafka Bridge returns the consumer ID (
instance_id) and base URL (base_uri) in the response body, along with a200code.Example response
#... { "instance_id": "bridge-quickstart-consumer", "base_uri":"http://<bridge_id>-bridge-service:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumer" }#... { "instance_id": "bridge-quickstart-consumer", "base_uri":"http://<bridge_id>-bridge-service:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumer" }Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
The consumer is named
-
Copy the base URL (
base_uri) to use in the other consumer operations in this quickstart.
What to do next
Now that you have created a Kafka Bridge consumer, you can subscribe it to topics.
2.5. Subscribing a Kafka Bridge consumer to topics 复制链接链接已复制到粘贴板!
After you have created a Kafka Bridge consumer, subscribe it to one or more topics by using the subscription endpoint. When subscribed, the consumer starts receiving all messages that are produced to the topic.
Procedure
Subscribe the consumer to the
bridge-quickstart-topictopic that you created earlier, in Producing messages to topics and partitions:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
topicsarray can contain a single topic (as shown here) or multiple topics. If you want to subscribe the consumer to multiple topics that match a regular expression, you can use thetopic_patternstring instead of thetopicsarray.If the request is successful, the Kafka Bridge returns a
204(No Content) code only.
When using an Apache Kafka client, the HTTP subscribe operation adds topics to the local consumer’s subscriptions. Joining a consumer group and obtaining partition assignments occur after running multiple HTTP poll operations, starting the partition rebalance and join-group process. It’s important to note that the initial HTTP poll operations may not return any records.
What to do next
After subscribing a Kafka Bridge consumer to topics, you can retrieve messages from the consumer.
Retrieve the latest messages from the Kafka Bridge consumer by requesting data from the records endpoint. In production, HTTP clients can call this endpoint repeatedly (in a loop).
Procedure
- Produce additional messages to the Kafka Bridge consumer, as described in Producing messages to topics and partitions.
Submit a
GETrequest to therecordsendpoint:curl -X GET http://localhost:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumer/records \ -H 'accept: application/vnd.kafka.json.v2+json'
curl -X GET http://localhost:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumer/records \ -H 'accept: application/vnd.kafka.json.v2+json'Copy to Clipboard Copied! Toggle word wrap Toggle overflow After creating and subscribing to a Kafka Bridge consumer, a first GET request will return an empty response because the poll operation starts a rebalancing process to assign partitions.
Repeat step two to retrieve messages from the Kafka Bridge consumer.
The Kafka Bridge returns an array of messages — describing the topic name, key, value, partition, and offset — in the response body, along with a
200code. Messages are retrieved from the latest offset by default.Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf an empty response is returned, produce more records to the consumer as described in Producing messages to topics and partitions, and then try retrieving messages again.
What to do next
After retrieving messages from a Kafka Bridge consumer, try committing offsets to the log.
2.7. Commiting offsets to the log 复制链接链接已复制到粘贴板!
Use the offsets endpoint to manually commit offsets to the log for all messages received by the Kafka Bridge consumer. This is required because the Kafka Bridge consumer that you created earlier, in Creating a Kafka Bridge consumer, was configured with the enable.auto.commit setting as false.
Procedure
Commit offsets to the log for the
bridge-quickstart-consumer:curl -X POST http://localhost:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumer/offsets
curl -X POST http://localhost:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumer/offsetsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Because no request body is submitted, offsets are committed for all the records that have been received by the consumer. Alternatively, the request body can contain an array of (OffsetCommitSeek) that specifies the topics and partitions that you want to commit offsets for.
If the request is successful, the Kafka Bridge returns a
204code only.
What to do next
After committing offsets to the log, try out the endpoints for seeking to offsets.
2.8. Seeking to offsets for a partition 复制链接链接已复制到粘贴板!
Use the positions endpoints to configure the Kafka Bridge consumer to retrieve messages for a partition from a specific offset, and then from the latest offset. This is referred to in Apache Kafka as a seek operation.
Procedure
Seek to a specific offset for partition 0 of the
quickstart-bridge-topictopic:Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the request is successful, the Kafka Bridge returns a
204code only.Submit a
GETrequest to therecordsendpoint:curl -X GET http://localhost:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumer/records \ -H 'accept: application/vnd.kafka.json.v2+json'
curl -X GET http://localhost:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumer/records \ -H 'accept: application/vnd.kafka.json.v2+json'Copy to Clipboard Copied! Toggle word wrap Toggle overflow The Kafka Bridge returns messages from the offset that you seeked to.
Restore the default message retrieval behavior by seeking to the last offset for the same partition. This time, use the positions/end endpoint.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the request is successful, the Kafka Bridge returns another
204code.
You can also use the positions/beginning endpoint to seek to the first offset for one or more partitions.
What to do next
In this quickstart, you have used the Kafka Bridge to perform several common operations on a Kafka cluster. You can now delete the Kafka Bridge consumer that you created earlier.
2.9. Deleting a Kafka Bridge consumer 复制链接链接已复制到粘贴板!
Delete the Kafka Bridge consumer that you used throughout this quickstart.
Procedure
Delete the Kafka Bridge consumer by sending a
DELETErequest to the instances endpoint.curl -X DELETE http://localhost:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumer
curl -X DELETE http://localhost:8080/consumers/bridge-quickstart-consumer-group/instances/bridge-quickstart-consumerCopy to Clipboard Copied! Toggle word wrap Toggle overflow If the request is successful, the Kafka Bridge returns a
204code.