Construct Spark Structured Streaming functions with the open supply connector for Amazon Kinesis Information Streams

Apache Spark is a strong large knowledge engine used for large-scale knowledge analytics. Its in-memory computing makes it nice for iterative algorithms and interactive queries. You should utilize Apache Spark to course of streaming knowledge from quite a lot of streaming sources, together with Amazon Kinesis Data Streams to be used instances like clickstream evaluation, fraud detection, and extra. Kinesis Information Streams is a serverless streaming knowledge service that makes it simple to seize, course of, and retailer knowledge streams at any scale.

With the brand new open supply Amazon Kinesis Data Streams Connector for Spark Structured Streaming, you need to use the newer Spark Information Sources API. It additionally helps enhanced fan-out for devoted learn throughput and quicker stream processing. On this publish, we deep dive into the interior particulars of the connector and present you how you can use it to eat and produce data from and to Kinesis Information Streams utilizing Amazon EMR.

Introducing the Kinesis Information Streams connector for Spark Structured Streaming

The Kinesis Information Streams connector for Spark Structured Streaming is an open supply connector that helps each provisioned and On-Demand capability modes provided by Kinesis Information Streams. The connector is constructed utilizing the newest Spark Information Sources API V2, which makes use of Spark optimizations. Beginning with Amazon EMR 7.1, the connector comes pre-packaged on Amazon EMR on Amazon EKS, Amazon EMR on Amazon EC2, and Amazon EMR Serverless, so that you don’t must construct or obtain any packages. For utilizing it with different Apache Spark platforms, the connector is on the market as a public JAR file that may be immediately referred to whereas submitting a Spark Structured Streaming job. Moreover, you’ll be able to obtain and construct the connector from the GitHub repo.

Kinesis Information Streams helps two sorts of shoppers: shared throughput and devoted throughput. With shared throughput, 2 Mbps of learn throughput per shard is shared throughout shoppers. With devoted throughput, also referred to as enhanced fan-out, 2 Mbps of learn throughput per shard is devoted to every shopper. This new connector helps each shopper sorts out of the field with none extra coding, offering you the pliability to eat data out of your streams based mostly in your necessities. By default, this connector makes use of a shared throughput shopper, however you’ll be able to configure it to make use of enhanced fan-out within the configuration properties.

You can too use the connector as a sink connector to provide data to a Kinesis knowledge stream. The configuration parameters for utilizing the connector as a supply and sink differ—for extra info, see Kinesis Source Configuration. The connector additionally helps a number of storage choices, together with Amazon DynamoDB, Amazon Simple Service for Storage (Amazon S3), and HDFS, to retailer checkpoints and supply continuity.

For eventualities the place a Kinesis knowledge stream is deployed in an AWS producer account and the Spark Structured Streaming software is in a distinct AWS shopper account, you need to use the connector to do cross-account processing. This requires extra Identity and Access Management (IAM) belief insurance policies to permit the Spark Structured Streaming software within the shopper account to imagine the function within the producer account.

You also needs to contemplate reviewing the safety configuration along with your safety groups based mostly in your knowledge safety necessities.

How the connector works

Consuming data from Kinesis Information Streams utilizing the connector includes a number of steps. The next structure diagram exhibits the interior particulars of how the connector works. A Spark Structured Streaming software consumes data from a Kinesis knowledge stream supply and produces data to a different Kinesis knowledge stream.

A Kinesis knowledge stream consists of set of shards. A shard is a uniquely recognized sequence of knowledge data in a stream and offers a set unit of capability. The entire capability of the stream is the sum of the capability of all of its shards.

A Spark software consists of a driver and a set of executor processes. The Spark driver acts as a coordinator, and the duties working in executors are liable for producing and consuming data to and from shards.

The answer workflow consists of the next steps:

  1. Internally, by default, Structured Streaming queries are processed utilizing a micro-batch processing engine, which processes knowledge streams as a sequence of small batch jobs. Initially of a micro-batch run, the motive force makes use of the Kinesis Information Streams ListShard API to find out the newest description of all accessible shards. The connector exposes a parameter (kinesis.describeShardInterval) to configure the interval between two successive ListShard API calls.
  2. The motive force then determines the beginning place in every shard. If the appliance is a brand new job, the beginning place of every shard is set by kinesis.startingPosition. If it’s a restart of an current job, it’s learn from final report metadata checkpoint from storage (for this publish, DynamoDB) and ignores kinesis.startingPosition.
  3. Every shard is mapped to at least one process in an executor, which is liable for studying knowledge. The Spark software mechanically creates an equal variety of duties based mostly on the variety of shards and distributes it throughout the executors.
  4. The duties in an executor use both polling mode (shared) or push mode (enhanced fan-out) to get knowledge data from the beginning place for a shard.
  5. Spark duties working within the executors write the processed knowledge to the information sink. On this structure, we use the Kinesis Information Streams sink for example how the connector writes again to the stream. Executors can write to multiple Kinesis Information Streams output shard.
  6. On the finish of every process, the corresponding executor course of saves the metadata (checkpoint) concerning the final report learn for every shard within the offset storage (for this publish, DynamoDB). This info is utilized by the motive force within the development of the subsequent micro-batch.

Answer overview

The next diagram exhibits an instance structure of how you can use the connector to learn from one Kinesis knowledge stream and write to a different.

On this structure, we use the Amazon Kinesis Data Generator (KDG) to generate pattern streaming knowledge (random occasions per nation) to a Kinesis Information Streams supply. We begin an interactive Spark Structured Streaming session and eat knowledge from the Kinesis knowledge stream, after which write to a different Kinesis knowledge stream.

We use Spark Structured Streaming to depend occasions per micro-batch window. These occasions for every nation are being consumed from Kinesis Information Streams. After the depend, we are able to see the outcomes.


To get began, observe the directions within the GitHub repo. You want the next conditions:

After you deploy the answer utilizing the AWS CDK, you should have the next sources:

  • An EMR cluster with the Kinesis Spark connector put in
  • A Kinesis Information Streams supply
  • A Kinesis Information Streams sink

Create your Spark Structured Streaming software

After the deployment is full, you’ll be able to entry the EMR major node to start out a Spark software and write your Spark Structured Streaming logic.

As we talked about earlier, you employ the brand new open supply Kinesis Spark connector to eat knowledge from Amazon EMR. You will discover the connector code on the GitHub repo together with examples on how you can construct and arrange the connector in Spark.

On this publish, we use Amazon EMR 7.1, the place the connector is natively accessible. Should you’re not utilizing Amazon EMR 7.1 and above, you need to use the connector by working the next code:

cd /usr/lib/spark/jars 
sudo wget
sudo chmod 755 spark-streaming-sql-kinesis-connector_2.12-1.2.1.jar

Full the next steps:

  1. On the Amazon EMR console, navigate to the emr-spark-kinesis cluster.
  2. On the Cases tab, choose the first occasion and select the Amazon Elastic Compute Cloud (Amazon EC2) occasion ID.

You’re redirected to the Amazon EC2 console.

  1. On the Amazon EC2 console, choose the first occasion and select Join.
  2. Use Session Manager, a functionality of AWS Systems Manager, to hook up with the occasion.
  3. As a result of the person that’s used to attach is the ssm-user, we have to change to the Hadoop person:

  4. Begin a Spark shell both utilizing Scala or Python to interactively construct a Spark Structured Streaming software to eat knowledge from a Kinesis knowledge stream.

For this publish, we use Python for writing to a stream utilizing a PySpark shell in Amazon EMR.

  1. Begin the PySpark shell by getting into the command pyspark.

As a result of you have already got the connector put in within the EMR cluster, now you can create the Kinesis supply.

  1. Create the Kinesis supply with the next code:
    kinesis = spark.readStream.format("aws-kinesis") 
        .choice("kinesis.area", "<aws-region>") 
        .choice("kinesis.streamName", "kinesis-source") 
        .choice("kinesis.consumerType", "GetRecords") 
        .choice("kinesis.endpointUrl", "https://kinesis.<aws-region>") 
        .choice("kinesis.startingposition", "LATEST") 

For creating the Kinesis supply, the next parameters are required:

  • Title of the connector – We use the connector title aws-kinesis
  • kinesis.area – The AWS Area of the Kinesis knowledge stream you’re consuming
  • kinesis.consumerType – Use GetRecords (normal shopper) or SubscribeToShard (enhanced fan-out shopper)
  • kinesis.endpointURL – The Regional Kinesis endpoint (for extra particulars, see Service endpoints)
  • kinesis.startingposition – Select LATEST, TRIM_HORIZON, or AT_TIMESTAMP (confer with ShardIteratorType)

For utilizing an enhanced fan-out shopper, extra parameters are wanted, similar to the buyer title. The extra configuration could be discovered within the connector’s GitHub repo.

kinesis_efo = spark 
.choice("kinesis.area", "<aws-region>") 
.choice("kinesis.streamName", "kinesis-source") 
.choice("kinesis.consumerType", "SubscribeToShard") 
.choice("kinesis.consumerName", "efo-consumer") 
.choice("kinesis.endpointUrl", "https://kinesis.<aws-region>") 
.choice("kinesis.startingposition", "LATEST") 

Deploy the Kinesis Information Generator

Full the next steps to deploy the KDG and begin producing knowledge:

  1. Select Launch Stack:
    launch stack 1

You would possibly want to alter your Area when deploying. Guarantee that the KDG is launched in the identical Area as the place you deployed the answer.

  1. For the parameters Username and Password, enter the values of your selection. Word these values to make use of later while you log in to the KDG.
  2. When the template has completed deploying, go to the Outputs tab of the stack and find the KDG URL.
  3. Log in to the KDG, utilizing the credentials you set when launching the CloudFormation template.
  4. Specify your Area and knowledge stream title, and use the next template to generate check knowledge:
        "id": {{random.quantity(100)}},
        "knowledge": "{{random.arrayElement(
        "date": "{{"YYYY-MM-DD hh:mm:ss")}}"

  5. Return to Programs Supervisor to proceed working with the Spark software.
  6. To have the ability to apply transformations based mostly on the fields of the occasions, you first must outline the schema for the occasions:
    from pyspark.sql.sorts import *
    pythonSchema = StructType() 
     .add("id", LongType()) 
     .add("knowledge", StringType()) 
     .add("date", TimestampType())

  7. Run the next the command to eat knowledge from Kinesis Information Streams:
    from pyspark.sql.capabilities import *
    occasions= kinesis 
      .selectExpr("solid (knowledge as STRING) jsonData") 
      .choose(from_json("jsonData", pythonSchema).alias("occasions")) 

  8. Use the next code for the Kinesis Spark connector sink:
        .selectExpr("CAST(id AS STRING) as partitionKey","knowledge","date") 
        .choice("kinesis.area", "<aws-region>") 
        .choice("kinesis.streamName", "kinesis-sink") 
        .choice("kinesis.endpointUrl", "https://kinesis.<aws-region>") 
        .choice("checkpointLocation", "/kinesisCheckpoint") 

You may view the information within the Kinesis Information Streams console.

  1. On the Kinesis Information Streams console, navigate to kinesis-sink.
  2. On the Information viewer tab, select a shard and a beginning place (for this publish, we use Newest) and select Get data.

You may see the information despatched, as proven within the following screenshot. Kinesis Information Streams makes use of base64 encoding by default, so that you would possibly see textual content with unreadable characters.

Clear up

Delete the next CloudFormation stacks created throughout this deployment to delete all of the provisioned sources:

  • EmrSparkKinesisStack
  • Kinesis-Information-Generator-Cognito-Person-SparkEFO-Weblog

Should you created any extra sources throughout this deployment, delete them manually.


On this publish, we mentioned the open supply Kinesis Information Streams connector for Spark Structured Streaming. It helps the newer Information Sources API V2 and Spark Structured Streaming for constructing streaming functions. The connector additionally allows high-throughput consumption from Kinesis Information Streams with enhanced fan-out by offering devoted throughput as much as 2 Mbps per shard per shopper. With this connector, now you can effortlessly construct high-throughput streaming functions with Spark Structured Streaming.

The Kinesis Spark connector is open supply underneath the Apache 2.0 license on GitHub. To get began, go to the GitHub repo.

In regards to the Authors

Idan Maizlits is a Senior Product Supervisor on the Amazon Kinesis Information Streams staff at Amazon Internet Companies. Idan loves participating with prospects to find out about their challenges with real-time knowledge and to assist them obtain their enterprise objectives. Exterior of labor, he enjoys spending time together with his household exploring the outside and cooking.

Subham Rakshit is a Streaming Specialist Options Architect for Analytics at AWS based mostly within the UK. He works with prospects to design and construct search and streaming knowledge platforms that assist them obtain their enterprise goal. Exterior of labor, he enjoys spending time fixing jigsaw puzzles together with his daughter.

Francisco Morillo is a Streaming Options Architect at AWS. Francisco works with AWS prospects serving to them design real-time analytics architectures utilizing AWS providers, supporting Amazon MSK and AWS’s managed providing for Apache Flink.

Umesh Chaudhari is a Streaming Options Architect at AWS. He works with prospects to design and construct real-time knowledge processing programs. He has in depth working expertise in software program engineering, together with architecting, designing, and creating knowledge analytics programs. Exterior of labor, he enjoys touring, studying, and watching motion pictures.

Leave a Reply

Your email address will not be published. Required fields are marked *