Hello everyone, have recently started learning pyf...
# troubleshooting
a
Hello everyone, have recently started learning pyflink, while going through the examples was trying to implement kafka based connector example, where it has 2 functions to first write data to kafka topic and then second function reads the same data from the topic, what's strange is that writes happen smooth after few tweaks and proper jar inclusion in the flink env instance but the reads are giving error
Copy code
env.add_jars("file:///C:/Users/ankur/PycharmProjects/pythonProject/FlinkLabs/flink-sql-connector-kafka-1.17.2.jar")
Copy code
Traceback (most recent call last):
  File "C:\Users\ankur\PycharmProjects\pythonProject\FlinkLabs\KafkaPrac.py", line 57, in <module>
    read_from_kafka(env)
  File "C:\Users\ankur\PycharmProjects\pythonProject\FlinkLabs\KafkaPrac.py", line 44, in read_from_kafka
    env.execute()
  File "C:\Users\ankur\PycharmProjects\pythonProject\Lab-Kafka\kafka-env\lib\site-packages\pyflink\datastream\stream_execution_environment.py", line 773, in execute
    return JobExecutionResult(self._j_stream_execution_environment.execute(j_stream_graph))
  File "C:\Users\ankur\PycharmProjects\pythonProject\Lab-Kafka\kafka-env\lib\site-packages\py4j\java_gateway.py", line 1322, in __call__
    return_value = get_return_value(
  File "C:\Users\ankur\PycharmProjects\pythonProject\Lab-Kafka\kafka-env\lib\site-packages\pyflink\util\exceptions.py", line 146, in deco
    return f(*a, **kw)
  File "C:\Users\ankur\PycharmProjects\pythonProject\Lab-Kafka\kafka-env\lib\site-packages\py4j\protocol.py", line 326, in get_return_value
    raise Py4JJavaError(
py4j.protocol.Py4JJavaError: An error occurred while calling o0.execute.
: org.apache.flink.runtime.client.JobExecutionException: Job execution failed.
	at org.apache.flink.runtime.jobmaster.JobResult.toJobExecutionResult(JobResult.java:144)
have tried looking at multiple places but didn't get anything useful, can someone please help?
Below is the example script that was working upon, comes bundled with apache-flink lib
Copy code
import logging
import sys

from pyflink.common import Types
from pyflink.datastream import StreamExecutionEnvironment
from pyflink.datastream.connectors.kafka import FlinkKafkaProducer, FlinkKafkaConsumer
from pyflink.datastream.formats.json import JsonRowSerializationSchema, JsonRowDeserializationSchema


# Make sure that the Kafka cluster is started and the topic 'test_json_topic' is
# created before executing this job.
def write_to_kafka(env):
    type_info = Types.ROW([Types.INT(), Types.STRING()])
    ds = env.from_collection(
        [(1, 'hi'), (2, 'hello'), (3, 'hi'), (4, 'hello'), (5, 'hi'), (6, 'hello'), (6, 'hello')],
        type_info=type_info)

    serialization_schema = JsonRowSerializationSchema.Builder() \
        .with_type_info(type_info) \
        .build()
    kafka_producer = FlinkKafkaProducer(
        topic='test_json_topic',
        serialization_schema=serialization_schema,
        producer_config={'bootstrap.servers': 'localhost:29092', 'group.id': 'test_group'}
    )

    # note that the output type of ds must be RowTypeInfo
    ds.add_sink(kafka_producer)
    env.execute()


def read_from_kafka(env):
    deserialization_schema = JsonRowDeserializationSchema.Builder() \
        .type_info(Types.ROW([Types.INT(), Types.STRING()])) \
        .build()
    kafka_consumer = FlinkKafkaConsumer(
        topics='test_json_topic',
        deserialization_schema=deserialization_schema,
        properties={'bootstrap.servers': 'localhost:29092', 'group.id': 'test_group'}
    )
    kafka_consumer.set_start_from_earliest()

    env.add_source(kafka_consumer).print()
    env.execute()


if __name__ == '__main__':
    logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")

    env = StreamExecutionEnvironment.get_execution_environment()
    env.add_jars("file:///C:/Users/ankur/PycharmProjects/pythonProject/FlinkLabs/flink-sql-connector-kafka-1.17.2.jar")
    print("start writing data to kafka")
    write_to_kafka(env)

    print("start reading data from kafka")
    read_from_kafka(env)
r
Can you check the Flink Jobmanager logs to see if there's an error?
a
Hey @rmetzger thanks for response, was running it through IDE without using dedicated job manager, writer works fine but reader was getting issue while running from windows host, but is working from Ubuntu Docker that have created with scripts from base image as a workaround
BTW, need guidance on events storage for processing inside code, in this code
Copy code
def read_from_kafka(env):
    deserialization_schema = JsonRowDeserializationSchema.Builder() \
        .type_info(Types.ROW([Types.INT(), Types.STRING()])) \
        .build()
    kafka_consumer = FlinkKafkaConsumer(
        topics='test_json_topic',
        deserialization_schema=deserialization_schema,
        properties={'bootstrap.servers': 'localhost:29092', 'group.id': 'test_group'}
    )
    kafka_consumer.set_start_from_earliest()

    env.add_source(kafka_consumer).print()
    env.execute()
this line, is directly sending output to stdout but want to store the events for further processing
Copy code
env.add_source(kafka_consumer).print()
have tried using
execute_and_collect()
with auto commit set to false for the previous reads, means fresh writes and prev events in topic are available for read but the task execution halts at this stage, using `print()`it works fine, can you please suggest if there is a way to achieve the same. It should be straight forwars given the basic necessity, but somehow not able to find it πŸ˜‹