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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Big Data
  4. How to Stream Records to Kafka With Akka Streams and Alpakka

How to Stream Records to Kafka With Akka Streams and Alpakka

Welcome to Kafka streaming, with a helping hand from Akka Streams and the open source Alpakka.

Teena Vashist user avatar by
Teena Vashist
·
May. 20, 19 · Tutorial
Like (1)
Save
Tweet
Share
7.28K Views

Join the DZone community and get the full member experience.

Join For Free

This blog will show you how records can be streamed to Kafka using Akka Streams with Alpakka. Alpakka is an open source project that provides a number of connectors, and in this blog, we will use the Alpakka connector for Kafka.

Before digging into it further, you can read more about Kafka here.

This blog will cover the producer stream only. Prior to starting the implementation, let's take a quick look at the codebase. First, we will add the library dependency of the Alpakka Kafka connector to the build.sbt, then we'll create the producer stream. The producer is responsible for producing (writing/publishing) data to Kafka.

The prerequisites for this project are:

  • Scala Version: 2.12.8
  • Bootstrap servers of the Kafka cluster
  • Alpakka Kafka 1.0-RC2

Let's begin!

  1. Add your library dependencies:
    libraryDependencies += "com.typesafe.akka" %% "akka-stream-kafka" % "1.0-RC1"
  2. To create a producer stream, you need to define a ProducerSetting, which creates a setting from the configuration and consists of the Bootstrap server of a Kafka Cluster key and value serializer, which must be passed explicitly. Here, Kafka is running on localhost:9092. You may also pass a list of comma-separated host port pairs.
    val producerSettings =
      ProducerSettings(config, new StringSerializer, new StringSerializer)
        .withBootstrapServers("http://localhost:9092")
  3. Now we need to create a sink to write records to Kafka topics via Producer.plainSink. That will publish the records to our Kafka topic and take ProducerRecords, which consists of the Kafka topic for which the record needs to be sent and the value, i.e record content:
    object KafkaProducer extends App {
      implicit val system = ActorSystem("QuickStart")
      implicit val materializer = ActorMaterializer()
    
      val config = ConfigFactory.load.getConfig("akka.kafka.producer")
      val producerSettings =
        ProducerSettings(config, new StringSerializer, new StringSerializer)
          .withBootstrapServers("http://localhost:9092")
    
      val producerSink: Future[Done] =
        Source(1 to 10)
          .map(_.toString)
          .map(value => new ProducerRecord[String, String]("test", "Record:: " + value))
          .runWith(Producer.plainSink(producerSettings))
    
      println("************ Message produced ************")
    }

4.  Now to run and test the Kafka producer, please follow the steps below:

  • Start zookeeper: .\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties  
  • Start Kafka: .\bin\windows\kafka-server-start.bat .\config\server.properties  
  • After creating the topic, start the consumer to check the records: 
    .\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092
    --topic test --from-beginning
      

Thanks!

Reference

  • https://doc.akka.io/docs/akka-stream-kafka/current/producer.html

kafka Record (computer science) Stream (computing) Akka (toolkit)

Published at DZone with permission of Teena Vashist. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Important Takeaways for PostgreSQL Indexes
  • How Agile Architecture Spikes Are Used in Shift-Left BDD
  • Securing Cloud-Native Applications: Tips and Tricks for Secure Modernization
  • Key Elements of Site Reliability Engineering (SRE)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: