SQL API for Actual-Time Kafka Analytics in 3 Steps

0/5 No votes

Report this app



On this weblog we are going to arrange a real-time SQL API on Kafka utilizing AWS Lambda and Rockset.

On the time of writing (in early 2020) the San Francisco 49ers are doing remarkably effectively! To honor their success, we are going to concentrate on answering the next query.

What are the most well-liked hashtags in tweets that talked about the 49ers within the final 20 minutes?

As a result of Twitter strikes quick, we are going to solely have a look at very latest tweets.

We may also present how one can make the identical API and question work for any of the groups within the NFL.

There are 3 steps to get began.

  1. Load the Twitter Stream into Rockset.
  2. Get a quick SQL Desk
  3. Energy your API

Step 0: Setup

This tutorial assumes you’ve already arrange a Rockset Account, an AWS account (for the lambda), and a Twitter Improvement Account (for the twitter stream).

If you have already got information you need to construct an API on skip to Step 1.

The Kafka arrange directions for the rest of this tutorial are for Confluent Kafka. Observe the instructions beneath to set it up for those who don’t have a Kafka set up to experiment with.

Non-obligatory: Arrange Confluent Kafka domestically

First it’s important to arrange Confluent Hub Platform, the Confluent Hub consumer and the Confluent CLI.

# Begin up a neighborhood model of Kafka.
confluent native begin

You need to see a number of providers begin.
Navigate to the Kafka UI at http://localhost:9021/ to verify it really works correctly.

Establishing the Twitter Connector

Set up Kafka Join Twitter in addition to the Rockset Kafka Connector.

confluent-hub set up jcustenborder/kafka-connect-twitter:0.3.33
confluent-hub set up rockset/kafka-connect-rockset:1.2.1

# restart Kafka if you're operating domestically
confluent native cease
confluent native begin

Now navigate to the Kafka UI, choose your cluster, and create a subject known as “rockset-kafka”. Then add the Twitter connector to your cluster, and level them each to the subject you simply made. You will have to place in your twitter-dev account info for the Twitter Connector. Subscribe to the next subjects to see soccer associated outcomes.

49ers Broncos packers Raiders Giants Redskins MiamiDolphins Buccaneers Seahawks Nyjets Ravens Bengals Titans HoustanTexans Dallascowboys Browns Colts AtlantaFalcons Vikings Lions Patriots RamsNFL steelers Jaguars BuffaloBills Chiefs Saints AZCardinals Panthers Eagles ChicagoBears Chargers NFL SportsNews American soccer Draft ESPN

Click on create. You need to now see many tweets flowing by means of the subject “rockset-kafka”.

Troubleshooting: if in case you have points, double examine your credentials. You too can examine the Kafka logs.

# Domestically
confluent native log join

Step 1: Load the Kafka Stream into Rockset

Establishing the Rockset Connector

  1. In case you haven’t already, set up the Rockset Kafka Connector in your Kafka cluster, and level it to “rockset-kafka”
  2. Log in to Rockset Console and navigate to the Create Kafka Integration at Handle > Integrations > Kafka.
  3. Identify your integration, set the content material sort to AVRO, and add a subject known as “rockset-kafka”.
  4. Observe the Kafka connector set up directions on the Integration Particulars web page that you just land on once you end creating the mixing.
  5. There are additional directions at https://docs.rockset.com/apache-kafka/.

Non-obligatory: Native Kafka
If you’re utilizing a neighborhood standalone Kafka, skip to Step 3 of the directions on the set up web page. Set the Schema Registry Occasion to be http://localhost:8081 (which is the default port of the schema-registry service) and click on “Obtain Rockset Sink Connector Properties” to get a properties file that ought to offer you the entire variables you’ll want to arrange to make use of Rockset with Kafka.

Here’s a pattern properties file that ought to look just like yours:


On the Kafka UI join web page, choose your cluster and click on Add Connector. Choose the RocksetSinkConnector. Lastly, fill in the entire properties from the connect-rockset-sink.properties file you downloaded from the Rockset Console.

As soon as you’re achieved, click on Create. You at the moment are able to create a Kafka Assortment in Rockset!

Making a Kafka Assortment in Rockset

Navigate to the Rockset Console and go to Collections > Create Assortment > Kafka. Choose the mixing you created within the earlier step and click on Begin.

Identify your assortment, and choose the subject “rockset-kafka”. Watch for a number of moments and it is best to see paperwork coming into the preview. This implies your connectors are configured appropriately! Go forward and set a retention coverage and create your assortment.

2019-12-12 17.42.57

Congratulations! You may have efficiently created a Kafka assortment in Rockset.

Step 2: Get a quick SQL Desk.

Our driving query is: What are the most well-liked hashtags in tweets that talked about the 49ers within the final 20 minutes?

Let’s discover the information somewhat bit within the Rockset Console and develop our question.

If we glance on the left facet of the console, we see that the twitter information is a stream of big json objects nested in advanced methods. The fields we care about most to reply our driving query are:

  1. entities.user_mentions (an array of mentions for a specific tweet)
  2. entities.hashtags (an array of hashtags in a specific tweet)

We additionally care concerning the following two system fields

  1. _id (distinctive area for every tweet)
  2. _event_time (time {that a} tweet was ingested into Rockset)

To reply our query, we need to discover all tweets the place at the least one user_mention is @49ers. Then we have to mixture over all hashtags to rely what number of of every one there’s.

However how will we filter and mixture over nested arrays? Enter the UNNEST command. Unnest takes a single row in our desk that incorporates an array (of size n) and multiplies it into n rows that every comprise a component of the unique array.

We thus need to unnest mentions and hashtags, and filter by mentions.

    h.hashtags.textual content hashtag
    commons."twitter-kafka" t,
    unnest(t.entities.hashtags hashtags) h,
    unnest(t.entities.mentions mentions) mt

This returns the entire hashtags in tweets mentioning the 49ers. Let’s mixture over the hashtags, calculate a rely, then type by the rely.

    h.hashtags.textual content hashtag,
    rely(t._id) "rely"
    commons."twitter-kinesis" t,
    unnest(t.entities.user_mentions mentions) mt,
    unnest(t.entities.hashtags hashtags) h
    h.hashtags.textual content
    "rely" DESC;

Lastly we filter to solely embrace tweets within the final 20 minutes. Moreover, we substitute ’49ers’ with a Rockset Question Parameter. It will allow us to make use of totally different question parameters from the Rockset REST API.

    h.hashtags.textual content hashtag,
    rely(t._id) "rely",
    UNIX_MILLIS(MAX(t._event_time)) time
    commons."twitter-kinesis" t,
    unnest(t.entities.user_mentions mentions) mt,
    unnest(t.entities.hashtags hashtags) h
    t."_event_time" > CURRENT_TIMESTAMP() - INTERVAL 20 minute
    and mt.mentions.screen_name = :group
    h.hashtags.textual content
    "rely" DESC;

Go 49ers!

Step 3: Energy your API

We are able to already execute queries over HTTP with Rockset’s REST API. The issue is that utilizing the REST API requires you to move a secret API key. If we’re making a public dashboard, we don’t need to reveal our API key. We additionally don’t need to expose our Rockset account to a DOS assault, and management account bills.

The answer is to make use of an AWS Lambda to cover the API key and to set a Reserve Concurrency to restrict the quantity of compute we now have to make use of. Within the following part, we are going to undergo the method of writing a Node.js lambda utilizing the Rockset Node.js API.

Writing a lambda

Writing the lambda is fast and soiled utilizing the rockset-node-client v2. We simply run the question on the question route and move within the question string parameters. This could execute the specified question with the specified parameters. Rockset routinely handles escaping parameters for us, so we don’t want to fret about that.

  1. Be sure you have nodejs 12.x and npm put in
  2. In an empty listing, run npm i rockset
  3. Paste the lambda code beneath into index.js within the root of the listing
const rocksetConfigure = require("rockset").default;

const APIKEY = course of.env.ROCKSET_APIKEY;
const API_ENDPOINT = "https://api.rs2.usw2.rockset.com";

const consumer = rocksetConfigure(APIKEY, API_ENDPOINT);

const wrapResult = res => ({
  statusCode: 200,
  headers: {
    "Entry-Management-Permit-Origin": "*",
    "Entry-Management-Permit-Credentials": true
  physique: JSON.stringify(res),
  isBase64Encoded: false

const wrapErr = mes => ({
  statusCode: 400,
  errorMessage: JSON.stringify(mes),
  isBase64Encoded: false

const question = `
    h.hashtags.textual content hashtag,
    Rely(t._id) "rely",
    UNIX_MILLIS(MAX(t._event_time)) time
    commons."twitter-kinesis" t,
    unnest(t.entities.user_mentions mentions) mt,
    unnest(t.entities.hashtags hashtags) h
    t."_event_time" > CURRENT_TIMESTAMP() - INTERVAL 20 minute
    and mt.mentions.screen_name = :group
    h.hashtags.textual content
    "rely" DESC;

exports.handler = async (occasion, context) =>
  occasion.queryStringParameters && occasion.queryStringParameters.param
    ? consumer.queries
          sql: {
            parameters: [
                name: "team",
                type: "string",
                value: event.queryStringParameters.param
    : wrapErr("Want parameters for question");

Nonetheless, we have to bundle the rockset dependency together with the lambda code with a view to deploy to aws. So as to take action, we zip our complete listing, together with node_modules, and use that to deploy.

Our remaining listing construction seems to be like:

├── index.js
  ├── node_modules
  └── package-lock.json

Deploying our lambda to AWS

  1. First we are going to create a lambda. Go to your AWS console, go to the lambda software.
  2. Click on “Create Operate”, then click on “From Scratch”
  3. Choose a runtime of Node.js 12.x.
  4. Add the zip we created within the earlier step.
  5. Add an atmosphere variable known as ROCKSET_APIKEY, and set it to your API key
  6. Execute the lambda with the next check json.

    "queryStringParameters": {
    "param": "49ers"
  7. You need to see a inexperienced run with an output just like the next.

    "physique":"{"outcomes":[{"text":"to-mah-to", ..."
  8. Finally, make sure to set a Reserve Concurrency to limit costs associated with the lambda.

Congratulations! If the test JSON returned the correct output, you set up your lambda correctly. We can now move on to configuring an API gateway to use this lambda.

Configure an API Gateway

Next we will configure an API Gateway to receive api requests and pass them to our lambda. There are several tutorials that describe this process in detail.

Once you have set up your api, you can send a request directly from your browser, or use codepen. Just modify the lambda URL to be your lambda and you should see some result tweets!


In this tutorial, we created a SQL API on Kafka in 3 easy steps that let us slice the Twitter Streaming API in real time.

See the final result below!

2020-01-17 14.54.45


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.