In this article16
Apache Spark, Kafka, Scala and Java in data architecture
Apache Spark, Apache Kafka, Scala and Java underpin many data platforms. Spark handles distributed batch and stream processing, including ETL and ELT, SQL and machine learning. Kafka supports event streaming for real-time ingestion and publish/subscribe. Scala and Java run on the JVM and have established libraries for building scalable data pipelines and microservices.
What are Spark, Kafka, Scala and Java?
Apache Spark
Spark is a distributed processing engine for batch and streaming workloads. It supports Spark SQL, DataFrames, MLlib for machine learning and Structured Streaming. It runs on clusters such as YARN or Kubernetes and reads from and writes to data lakes and warehouses.
Apache Kafka
Kafka is a distributed event streaming platform with topics, partitions, producers, consumers and consumer groups. It offers durable, scalable publish/subscribe for real-time data and event-driven architectures.
Scala and Java
Scala supports functional and object-oriented programming. Java is widely used in enterprise software. Both run on the JVM and have extensive libraries for integrating with Spark and Kafka. Both work for data engineering and stream processing. Scala is popular for expressive transformations and Java for microservices.
How do they work together in a data platform?
A typical pipeline uses Kafka to receive events, such as clickstream, IoT or change data capture records. Spark Structured Streaming reads, enriches and transforms them in near real time and writes them to a data lake or lakehouse, warehouse or serving layer, such as dashboards or APIs. Scala or Java microservices publish and consume events, for example through Kafka Streams, and respond as data arrives.
Step | Purpose | Examples |
|---|---|---|
Ingestion | Receive events and handle backpressure | Kafka producers (Java or Scala), connectors |
Processing | Transform and aggregate data | Spark SQL, Structured Streaming, Kafka Streams |
Storage | Keep history and support analytics | Lakehouse (Parquet, Iceberg or Delta), warehouse |
Consumption | Serve real-time applications and BI | Microservices (Java or Scala), dashboards, ML serving |
When to use each tool
- Spark: Large ETL and ELT jobs, data preparation, Spark SQL and ML workloads. Structured Streaming brings batch and streaming into one API.
- Kafka: Real-time ingestion, event-driven integrations and buffering. Decoupling helps systems handle failures independently.
- Scala: Type-safe, expressive data transformations, often used in Spark jobs.
- Java: Enterprise microservices and Kafka Streams applications, with widespread use and tooling.
Architecture patterns
Kappa architecture
This uses a single streaming path for live events and replaying historical data. It can be simpler to maintain than Lambda architecture and suits systems organised around events.
Lakehouse
A lakehouse brings data lake flexibility together with warehouse capabilities such as ACID transactions, schema evolution and time travel. Spark handles processing and Kafka feeds events into the bronze layer.
Event-driven architecture (EDA)
Microservices exchange events through Kafka. They remain loosely coupled and can scale independently. Stream processing aggregates data, detects patterns and enriches records.
Libraries and components
Spark
- Spark SQL and DataFrames for declarative transformations.
- MLlib for conventional machine learning, including feature engineering and pipelines.
- Structured Streaming for micro-batch or continuous processing. Exactly-once delivery through micro-batches depends on the destination and configuration. Continuous Processing offers at-least-once guarantees.
Kafka
- Kafka Connect for integrating systems such as CDC sources, object stores and warehouses.
- Kafka Streams (Java or Scala) for stateful stream processing in applications.
- Schema registries and serialisers (Avro, Protobuf or JSON) for managing schemas and compatibility.
Choosing a JVM language
Scala is expressive, supports functional patterns and immutability and fits Spark's APIs well. Java has established tools, broad adoption and support for long-term maintenance. Kotlin is another JVM language gaining use, while Scala and Java remain widely used in enterprise data systems.
- Team skills: Choose a language the team knows and can maintain.
- Libraries: Scala is common for Spark jobs and Java for Kafka Streams microservices.
- Hiring: Java expertise is widespread. Scala expertise can be harder to find.
Deployment and operations
Spark and Kafka run on premises or in the cloud. Teams can use distributed or managed deployments on Kubernetes or cloud services. Consider autoscaling, storage formats such as Parquet, Delta, Iceberg and Hudi, cost control and observability.
- Orchestration: Airflow, managed schedulers or notebook and job services.
- Cluster management: Autoscaling policies, spot or pre-emptible nodes and workload isolation.
- Storage: Object storage as the authoritative data source for a lakehouse.
Reliability and data quality
- Schemas and contracts: Use Avro or Protobuf with a schema registry. Manage backward and forward compatibility.
- Idempotency and exactly-once processing: Set up transactions and offset commits correctly. Use checkpointing in Spark.
- Partitions and keys: Choose keys that distribute work evenly. Monitor lag and busy partitions.
- Quality gates: Validate nullability and ranges, define expectations and use dead-letter queues for unprocessable records.
- Observability: Track metrics such as throughput and latency, traces, structured logs and data lineage.
- Security and governance: Use TLS, encryption at rest, ACLs, secrets management, PII masking, RBAC or ABAC, and audit trails.
Performance and costs
- Spark: Tune shuffles, use broadcast joins and predicate pushdown where appropriate, choose a columnar format such as Parquet and cache only when it helps.
- Kafka: Choose the number of partitions, tune batch.size and linger.ms, and consider compression with LZ4 or Zstandard and the number of consumers processing in parallel.
- Compute costs: Size resources to fit, use spot instances where suitable and set idle clusters to stop automatically. Process only the rows and columns required.
Common problems
- Schema drift: Unmanaged changes can break pipelines.
- Hot partitions: Poorly chosen keys can create uneven load and lag.
- Over-engineering: Too many tools and patterns make the system harder to run.
- Insufficient monitoring: Users find problems before alerts do.
- No backfill strategy: Reprocessing historical data takes more work than planned.
Roles, skills and certifications
Roles include Data Engineer, Streaming Engineer, Platform Engineer, ML Engineer and Software Engineer specialising in EDA. Core skills include JVM languages (Scala or Java), SQL, Spark, Kafka, CI/CD, containers and Kubernetes, data modelling, security and governance.
- Certifications: Vendor certifications in Spark and Kafka can help you demonstrate your skills.
- Practice: Build a project from event ingestion through streaming ETL and a lakehouse to a dashboard or API.
A project to get started
- Install a Kafka distribution locally and create a topic.
- Write a simple producer in Java or Scala that publishes JSON events.
- Build a Spark Structured Streaming job that reads and enriches events and writes them to Parquet.
- Analyse the data with Spark SQL and add basic feature engineering for machine learning.
- Show the results in a BI tool or expose them through a simple REST API.
Related guides
Read more about data tools and technologies or browse our knowledge base. For roles using Spark, Kafka, Scala or Java, see vacancies on DataJobs.nl.
FAQ
What is the difference between Apache Spark and Apache Kafka?
Spark processes batch and streaming data for ETL, SQL and ML. Kafka is an event streaming platform for reliable, scalable publish/subscribe and data transport.
Can I use Spark and Kafka together?
Yes. A common approach is to use Kafka as the ingestion layer and event log, with Spark Structured Streaming to process and aggregate data and send it to a lakehouse or warehouse.
Should I choose Scala or Java?
Scala is good for expressive Spark transformations. Java is widely used for enterprise microservices and Kafka Streams. Choose a language the team can maintain.
Vacancies for data and analytics specialists
See all current vacancies on DataJobs.nl here