Hi all, I am exploring apache flink. I have done a...
# troubleshooting
s
Hi all, I am exploring apache flink. I have done a basic program to send some message to a kafka topic and consume it using FlinkKafkaConsumer. But I am experiencing some slowness in receiving the data. Following is my code.
Copy code
from pyflink.datastream import StreamExecutionEnvironment, RuntimeExecutionMode
from pyflink.datastream.connectors import FlinkKafkaConsumer
from pyflink.common.serialization import SimpleStringSchema
from pyflink.datastream.window import TimeWindow, Window
from pyflink.datastream import WindowedStream
from pyflink.table.window import Tumble
from pyflink.datastream.window import Time
from pyflink.datastream.functions import ProcessFunction, KeyedProcessFunction, ProcessWindowFunction
from pyflink.datastream import WindowFunction
# from pyflink.datastream.util import Collector
from pyflink.datastream.window import SlidingEventTimeWindows
from pyflink.common import Types, WatermarkStrategy, Time, Encoder
from pyflink.common.watermark_strategy import TimestampAssigner
from kafka import KafkaConsumer
from typing import Iterable
import json
import datetime
import time

# Helper function to parse the JSON string
def parse_json(json_str):
    try:
        return json.loads(json_str)
    except json.JSONDecodeError:
        return None
topic = 'tick_data'
env = StreamExecutionEnvironment.get_execution_environment()

properties = {
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'my_consumer_group'
}

# env.add_jars("file:///Users/.../Downloads/flink-sql-connector-kafka-1.17.1.jar")
env.set_runtime_mode(RuntimeExecutionMode.STREAMING)
env.set_parallelism(2)
# env.add_jars(*jar_paths)
env.add_jars("file:///Users/stanlythomas/Documents/MyProjects/DataAnalyzerSheets/server/data/flink-sql-connector-kafka-1.17.2.jar")
kafka_consumer = FlinkKafkaConsumer(
    topic,
    SimpleStringSchema(),
    properties=properties
)
# data_stream = env.add_source(kafka_consumer)

data_stream = env.add_source(kafka_consumer) \
    .map(lambda x: (json.loads(x), datetime.datetime.now().isoformat()))

data_stream.print()
env.execute("My Flink Job")
I am printing out the data which I am receiving. Following is some sample output. There are 2 timestamps I am printing here. The first one comes with the data and it is the time at which data is pushed to kafka. Second time is the time which the data is consumed at FlinkKafkaConsumer. If you notice, for some data, there is more than 1 second between producer and consumer.
Copy code
1> ({'data_id': 999920000, 'data_type': 1, 'data_mode': 1, 'index': 'NIFTY', 'expiry_date': '', 'symbol': 'NIFTY', 'price': 22302.5, 'timestamp': '2024-05-08T23:14:56.840544'}, '2024-05-08T23:14:56.941591')
1> ({'data_id': 999920000, 'data_type': 1, 'data_mode': 1, 'index': 'NIFTY', 'expiry_date': '', 'symbol': 'NIFTY', 'price': 22302.5, 'timestamp': '2024-05-08T23:14:58.016418'}, '2024-05-08T23:14:58.936558')
1> ({'data_id': 999920000, 'data_type': 1, 'data_mode': 1, 'index': 'NIFTY', 'expiry_date': '', 'symbol': 'NIFTY', 'price': 22302.5, 'timestamp': '2024-05-08T23:14:59.224268'}, '2024-05-08T23:15:00.938550')
Both the producer and consumer is in the same system. Data is very less but it comes mostly every second. One thing I have explored is to run a basic kafka consumer without flink as follows
Copy code
consumer = KafkaConsumer('tick_data', bootstrap_servers='localhost:9092', value_deserializer=lambda x: json.loads(x.decode('utf-8')))

for message in consumer:
    print(f"Received message {message} at: {datetime.datetime.now().isoformat()}")
Here the data is consumed immediately. Following is the sample output. It can be seen that there is very low latency between first and second timestamp.
Copy code
Received message ConsumerRecord(topic='tick_data', partition=0, offset=430811, timestamp=1715190620206, timestamp_type=0, key=None, value={'data_id': 999920000, 'data_type': 1, 'data_mode': 1, 'index': 'NIFTY', 'expiry_date': '', 'symbol': 'NIFTY', 'price': 22302.5, 'timestamp': '2024-05-08T23:20:20.206248'}, headers=[], checksum=None, serialized_key_size=-1, serialized_value_size=171, serialized_header_size=-1) at: 2024-05-08T23:20:20.212899
Received message ConsumerRecord(topic='tick_data', partition=0, offset=430812, timestamp=1715190621358, timestamp_type=0, key=None, value={'data_id': 999920000, 'data_type': 1, 'data_mode': 1, 'index': 'NIFTY', 'expiry_date': '', 'symbol': 'NIFTY', 'price': 22302.5, 'timestamp': '2024-05-08T23:20:21.356333'}, headers=[], checksum=None, serialized_key_size=-1, serialized_value_size=171, serialized_header_size=-1) at: 2024-05-08T23:20:21.365014
I was expecting the flinkKafkaConsumer to receive the data immediately. Is there something wrong I am doing?