The spark_stream.py file has two issues that prevent it from working correctly inside the Docker Compose network:
Issue 1: Hardcoded localhost for service connections
connect_to_kafka() uses kafka.bootstrap.servers, localhost:9092 — but the Kafka broker is a container with hostname broker. Inside Docker, localhost refers to the container itself, not the host. The correct connection string for inter-container communication would be broker:29092 (the internal listener port).
create_cassandra_connection() uses Cluster([localhost]) — but Cassandra is a container with hostname cassandra. The correct value would be cassandra.
create_spark_connection() uses spark.cassandra.connection.host, localhost — same issue.
spark-streaming-kafka-integration also presumably needs an appropriate config for Docker networking.
Issue 2: Schema mismatch — dob field
The Cassandra table definition in create_table() includes a dob column:
And insert_data() accepts dob as a parameter and inserts it:
However, the Spark StructType schema in create_selection_df_from_kafka() does not include a dob field — only registered_date. This means any dob data sent through Kafka will silently be dropped and never written to Cassandra.
To reproduce:
- Run
docker compose up
- The Spark streaming job connects to Kafka and Cassandra on
localhost, which resolves to the Spark container itself rather than the broker/cassandra services
- Even if connectivity is fixed, the
dob field from Kafka messages will never be written to the database
Suggested fix:
- Replace
localhost with Docker Compose service names (broker:29092 for Kafka, cassandra for Cassandra)
- Add
StructField("dob", StringType(), False) to the schema definition in create_selection_df_from_kafka()
The
spark_stream.pyfile has two issues that prevent it from working correctly inside the Docker Compose network:Issue 1: Hardcoded
localhostfor service connectionsconnect_to_kafka()useskafka.bootstrap.servers,localhost:9092— but the Kafka broker is a container with hostnamebroker. Inside Docker,localhostrefers to the container itself, not the host. The correct connection string for inter-container communication would bebroker:29092(the internal listener port).create_cassandra_connection()usesCluster([localhost])— but Cassandra is a container with hostnamecassandra. The correct value would becassandra.create_spark_connection()usesspark.cassandra.connection.host,localhost— same issue.spark-streaming-kafka-integrationalso presumably needs an appropriate config for Docker networking.Issue 2: Schema mismatch —
dobfieldThe Cassandra table definition in
create_table()includes adobcolumn:dob TEXT,And
insert_data()acceptsdobas a parameter and inserts it:However, the Spark StructType schema in
create_selection_df_from_kafka()does not include adobfield — onlyregistered_date. This means anydobdata sent through Kafka will silently be dropped and never written to Cassandra.To reproduce:
docker compose uplocalhost, which resolves to the Spark container itself rather than thebroker/cassandraservicesdobfield from Kafka messages will never be written to the databaseSuggested fix:
localhostwith Docker Compose service names (broker:29092for Kafka,cassandrafor Cassandra)StructField("dob", StringType(), False)to the schema definition increate_selection_df_from_kafka()