Ankur Aggarwal
03/23/2024, 6:44 PMenv.add_jars("file:///C:/Users/ankur/PycharmProjects/pythonProject/FlinkLabs/flink-sql-connector-kafka-1.17.2.jar")
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?Ankur Aggarwal
03/23/2024, 6:45 PMimport 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)rmetzger
03/25/2024, 4:38 PMAnkur Aggarwal
03/26/2024, 4:17 AMAnkur Aggarwal
03/26/2024, 4:30 AMdef 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
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 πrmetzger
03/26/2024, 2:46 PM