DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Replay, Scheduling, and Throttling: Engineering a Self-Service Kafka Streaming Tool at Scale
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • Consumer Lag Is the Wrong Finish Line

Trending

  • Genkit Flows as senro Steps: Extremely Powerful AI Pipelines in Go
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Big Data
  4. Querying Kafka Topics Using Presto

Querying Kafka Topics Using Presto

In this article, we will discuss how Presto can be used to query Kafka topics with a step-by-step process to connect Kafka and Presto together for MacOS.

By 
Praful Khandelwal
·
May. 05, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Presto is a distributed query engine that allows querying different data sources such as Kafka, MySQL, MongoDB, Oracle, Cassandra, Hive, etc. using SQL. It has the ability to analyze big data and query multiple data sources together.

In this article, we will discuss how Presto can be used to query Kafka topics. Below is the step-by-step process to set up Presto and Kafka, and connect them together. Here, I have considered MacOS, but similar setups can be done on any other system.

Prerequisites

  • A MacOS machine with Homebrew and Java installed.

Presto Installation

  • Install Presto:
$ brew install presto
  • Presto will get installed at /usr/local/Cellar/prestodb by default.
  • The following 4 configuration files will be created by default at /usr/local/Cellar/prestodb/<presto version>/libexec/etc:
    - node.properties
    - jvm.config
    - config.properties
    - log.properties

Note: The settings in the above files can be updated based on the requirements. Though, for the basic working, the default settings will suffice. So we can leave the settings unchanged and proceed. Here, the Presto version refers to the latest version of Presto that has been downloaded. A directory is created by that name (e.g., 0.270). So the actual path, in this case, would be /usr/local/Cellar/prestodb/0.270/libexec/etc

  • The data source connectors need to be created at /usr/local/Cellar/prestodb/<presto version>/libexec/etc/catalog. By default, one connector (JMX) is created. Other connectors can be setup as per the requirements. We’ll cover this in later sections of this article.
  • Start the Presto server.
$ presto-server start

Note: The above command will start the Presto server in the background. Alternatively, use the following command to start the Presto server in the foreground. Sometimes it is useful to run the server in the foreground to check for any issues that are causing the server start to fail. Any start-up issues will be shown in the console log and can be debugged.

$ presto-server run
  • Use the following command to stop the Presto server any time you need to $ presto-server stop.Note: Alternatively, the Presto server can be stopped using ctrl+c while running in the foreground.
  • Access Presto Web UI at http://localhost:8080 while the Presto server is running.
  • Presto CLI is downloaded automatically as part of the Presto installation at /usr/local/Cellar/prestodb/<presto version>/libexec. It is in the form of an executable jar (e.g.: presto-cli-0.270-executable.jar).
  • Rename the CLI jar to simply ‘presto’ for convenience.
$ mv presto-cli-0.270-executable.jar presto
  • Provide execute permissions to CLI.
$ chmod +x presto
  • Launch Presto CLI. Presto prompt will appear if CLI is launched successfully.
$ ./presto --server localhost:8080 --catalog <catalog name>
presto>

Note: Here, catalog name refers to data source connector (e.g., Kafka, MySQL, jmx, etc.).

If you are able to launch the Presto server, Presto UI, and Presto CLI seamlessly, that means the Presto setup was successful.

Kafka Installation

  • Install Kafka.
$ brew install kafka
  • Start Zookeeper service (in terminal 1).
$ zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties
  • Start Kafka broker service (in terminal 2).
$ kafka-server-start /usr/local/etc/kafka/server.properties
  • Create a topic (in terminal 3).
$ kafka-topics --create --topic test_events --bootstrap-server localhost:9092
  • Start the Kafka producer (in terminal 3) and write events to the above topic.
$ kafka-console-producer --topic test_events --bootstrap-server localhost:9092
  • Start Kafka consumer (in terminal 4) and read events from the above topic.
$ kafka-console-consumer --topic test_events --from-beginning --bootstrap-server localhost:9092

If you are able to produce and consume the events seamlessly, that means the Kafka setup is successful and we can move on.

Connecting Presto to Kafka and Querying Kafka Topics

  • Set up Kafka connector at /usr/local/Cellar/prestodb/<presto version>/libexec/etc/catalog by creating the file kafka.properties and adding the following configurations to it:
connector.name=kafka
kafka.nodes=localhost:9092
kafka.table-names=test_schema.test_events
kafka.hide-internal-columns=false

Note: Make sure there are no trailing whitespaces in the above settings, else the Presto server may not start successfully. In kafka.table-names, the Kafka topic names are provided (comma separated). Optionally, the schema name can be prefixed to the topic names. If no schema name is prefixed, the topic tables will go into default schema.

  • Restart the Presto server after creating the above connector. The Presto server needs to be restarted each time new connectors are added or existing connectors are updated to pick up the latest settings.
$ presto-server start
  • Launch Presto CLI.
$ ./presto --server localhost:8080 --catalog kafka --schema test_schema
presto:test_schema>

Note: Alternatively, Presto CLI can be launched without providing the schema and schema can be chosen afterwards.

$ ./presto --server localhost:8080 --catalog kafka
presto> use test_schema;
presto:test_schema>
  • Execute SQL Query.
presto:test_schema> select * from test_events;
  • Produce new events to the above Kafka topic and see that they appear in the results of the above query.
$ kafka-console-producer --topic test_events --bootstrap-server localhost:9092
>message1
>message2
>message3

If you are able to produce and query the events seamlessly, that means the above setup was successful.

Conclusion

This brings us to the end of this article. We saw how we can set up Presto and Kafka and query the Kafka topics using Presto. This can also be done programatically using JDBC connection (jdbc:presto://localhost:8080/kafka/test_schema).

kafka Presto (SQL query engine)

Published at DZone with permission of Praful Khandelwal. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Replay, Scheduling, and Throttling: Engineering a Self-Service Kafka Streaming Tool at Scale
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • Consumer Lag Is the Wrong Finish Line

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook