This page shows you how to do the following:
- Create Lite topics and Lite subscriptions using the Google Cloud Console.
- Send and receive messages using the Pub/Sub Lite client library for Java.
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Pub/Sub Lite API.
-
Set up authentication:
-
In the Cloud Console, go to the Create service account key page.
Go to the Create Service Account Key page - From the Service account list, select New service account.
- In the Service account name field, enter a name.
From the Role list, select Project > Owner.
- Click Create. A JSON file that contains your key downloads to your computer.
-
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.
Install the client library
Java
If you are using Maven, add
the following to your pom.xml
file. For more information about
BOMs, see The Google Cloud Platform Libraries BOM.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
If you're using IntelliJ or Eclipse, you can add client libraries to your project using the following IDE plugins:
The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.
Python
pip install --upgrade google-cloud-pubsublite
Create a Lite topic
To create a Lite topic with the Cloud Console, follow these steps:
In the Cloud Console, go to the Lite Topics page.
Click Create Lite topic.
Select a region and a zone in the region.
In the Name section, enter
your-lite-topic
as the Lite topic ID. The Lite topic name includes the Lite topic ID, the zone, and the project number.Click Create.
Create a Lite subscription
To create a Lite subscription with Cloud Console, follow these steps:
In the Cloud Console, go to the Lite Subscriptions page.
Click Create Lite subscription.
In the Lite subscription ID field, enter
your-lite-subscription
.Select a Lite topic to receive messages from.
In the Delivery requirement section, select Deliver messages after stored.
Click Create.
The Lite subscription is in the same zone as the Lite topic.
Send messages
Send messages to the Lite topic using the following publisher application:
Java
Python
The publisher sends 100 messages to a Lite topic and prints the number of messages that the Pub/Sub Lite service receives.
Receive messages
Receive messages from the Lite subscription using the following subscriber application:
Java
Python
After the subscriber receives a message, the subscriber prints the message ID and the message data.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, follow these steps.
In the Cloud Console, go to the Lite Topics page.
Click your-lite-topic.
In the Lite topic details page, click Delete.
In the field that appears, enter
delete
to confirm that you want to delete the Lite topic.Click Delete.
What's next
- Learn more about Lite topics and Lite subscriptions.
- Learn more about sending and receiving messages.
- Look through code samples for the client library.