Skip to main content
This QuickStart assumes that you have created a StreamNative cluster with the Kafka protocol enabled, created a service account, and granted the service account produce and consume permissions to a namespace for the target topic.
This document describes how to connect to a StreamNative cluster through a Kafka Java client, and use the Java producer and consumer to produce and consume messages to and from a topic. The Java client supports connecting to a StreamNative cluster using either OAuth2 or API Keys authentication.

Prerequisites

Connect to your cluster using API keys

This section describes how to connect to your StreamNative cluster using the Kafka Java client with SASL/PLAIN authentication.

Before you begin

  • Before using an API key, verify that the service account is authorized to access the resources, such as tenants, namespaces, and topics.
  • The password for different utilities as kcat will be equal to token:<API KEY>.
You can follow the instructions to create an API key for the service account you choose to use.

Steps

  1. Add Maven dependencies.
  2. Open a terminal and run a Kafka consumer to receive a message from the test-kafka-topic topic.
    • SERVER-URL: the Kafka service URL of your StreamNative cluster.
    • API-KEY: an API key of your service account.
  3. Open another terminal and run a Kafka producer to send a message to the test-kafka-topic topic.
    • SERVER-URL: the Kafka service URL of your StreamNative cluster.
    • API-KEY: an API key of your service account.

Connect to your cluster using OAuth2 authentication

This section describes how to connect to your StreamNative cluster using the Kafka Java client with OAuth2 authentication.

Before you begin

  • Get the OAuth2 credential file.
    1. On the left navigation pane, click Service Accounts.
    2. In the row of the service account you want to use, in the Key File column, click the Download icon to download the OAuth2 credential file to your local directory.
  • Get the service URL of your StreamNative cluster.
    1. On the left navigation pane, in the Admin area, click Pulsar Clusters.
    2. Select the Details tab, and in the Access Points area, click Copy at the end of the row of the Kafka Service URL (TCP).

Steps

  1. Add Maven dependencies.
  2. Open a terminal and run a Kafka consumer to receive a message from the test-topic topic.
    • YOUR-KEY-FILE-ABSOLUTE-PATH: the path to your downloaded OAuth2 credential file.
    • YOUR-KAFKA-SERVICE-URL: the Kafka service URL of your StreamNative cluster.
    • YOUR-AUDIENCE-STRING: the audience parameter is a combination of the urn:sn:pulsar, your organization name, and your Pulsar instance name.
  3. Open another terminal and run a Kafka producer to send a message to the test-topic topic.
    • YOUR-KEY-FILE-ABSOLUTE-PATH: the path to your downloaded OAuth2 credential file.
    • YOUR-KAFKA-SERVICE-URL: the Kafka service URL of your StreamNative cluster.
    • YOUR-AUDIENCE-STRING: the audience parameter is a combination of the urn:sn:pulsar, your organization name, and your Pulsar instance name.