Amir Hossein Sharifzadeh
02/15/2023, 7:35 PM#### On Producer side:
for raw_id in tqdm.tqdm(range(0, Nt)):
data_array = fid.read(chunk_size)
b64_string = str(base64.b64encode(data_array), 'utf-8')
empad_json = create_sample_empad_json(raw_id, b64_string, nFileLen, chunk_size)
# print(b64_string)
producer.send(topic, empad_json)
producer.flush()
def create_sample_empad_json(raw_id, raw_data, file_size, chunk_size):
return {'raw_id': int(raw_id), 'raw_data': raw_data, 'file_size': file_size, 'chunk_size': chunk_size}
#### On Flink side:
def stream_processing():
env = StreamExecutionEnvironment.get_execution_environment()
t_env = StreamTableEnvironment.create(stream_execution_environment=env)
t_env.get_config().get_configuration().set_boolean("python.fn-execution.memory.managed", True)
t1 = f"""
CREATE TABLE raw_table(
raw_id INT,
raw_data STRING,
file_size INT,
chunk_size INT
) WITH (
'connector' = 'kafka',
'topic' = 'empadraw',
'properties.bootstrap.servers' = 'kafka:9092',
'properties.group.id' = 'MY_GRP',
'scan.startup.mode' = 'latest-offset',
'format' = 'json'
)
"""
t2 = f"""
CREATE TABLE bkgd_table(
raw_id INT,
raw_data STRING,
file_size INT,
chunk_size INT
) WITH (
'connector' = 'kafka',
'topic' = 'empadbkgd',
'properties.bootstrap.servers' = 'kafka:9092',
'properties.group.id' = 'MY_GRP',
'scan.startup.mode' = 'latest-offset',
'format' = 'json'
)
"""
t_env.execute_sql(t1)
t_env.execute_sql(t2)
query = "SELECT raw_table.raw_id as raw_id, raw_table.raw_data as raw_enc_val, raw_table.file_size as raw_file_size, raw_table.chunk_size as raw_chunk_size, " \
"bkgd_table.raw_data as bkgd_enc_val, bkgd_table.file_size as bkgd_file_size, bkgd_table.chunk_size as bkgd_chunk_size " \
"FROM raw_table JOIN bkgd_table ON raw_table.raw_id = bkgd_table.raw_id"
table_result = t_env.sql_query(query).execute().collect()
process_results(table_result)
def process_results(table_result):
with table_result as results:
for result in results:
chunk_id = int(result[0])
raw_chunk = str(result[1])
data_chunk = str(result[2])
file_size = int(result[3])
chunk_size = int(result[4])
process_data(chunk_id, raw_chunk, bkgd_chunk, file_size, chunk_size)Giannis Polyzos
02/15/2023, 7:49 PMAmir Hossein Sharifzadeh
02/15/2023, 9:49 PMDian Fu
02/16/2023, 3:20 AMprocess_data, is it efficient enough?
• What’s the current performance?
[1] https://nightlies.apache.org/flink/flink-docs-release-1.16/docs/dev/python/datastream/intro_to_datastream_api/#conversion-betwee[…]astream-and-tableAmir Hossein Sharifzadeh
02/16/2023, 4:23 AMDian Fu
02/16/2023, 4:52 AMto_append_stream
[1] https://nightlies.apache.org/flink/flink-docs-release-1.16/docs/dev/python/datastream/intro_to_datastream_api/#conversion-between-datastream-and-tableAmir Hossein Sharifzadeh
02/16/2023, 2:47 PMt1 = f"""
CREATE TABLE raw_table(
raw_id INT,
raw_data STRING,
file_size INT,
chunk_size INT
) WITH (
'connector' = 'kafka',
'topic' = 'empadraw',
'properties.bootstrap.servers' = 'kafka:9092',
'properties.group.id' = 'MY_GRP',
'scan.startup.mode' = 'latest-offset',
'format' = 'json'
)
"""
t2 = f"""
CREATE TABLE bkgd_table(
raw_id INT,
raw_data STRING,
file_size INT,
chunk_size INT
) WITH (
'connector' = 'kafka',
'topic' = 'empadbkgd',
'properties.bootstrap.servers' = 'kafka:9092',
'properties.group.id' = 'MY_GRP',
'scan.startup.mode' = 'latest-offset',
'format' = 'json'
)
"""
t_env.execute_sql(t1)
t_env.execute_sql(t2)
query = "SELECT raw_table.raw_id as raw_id, raw_table.raw_data as raw_enc_val, raw_table.file_size as raw_file_size, raw_table.chunk_size as raw_chunk_size, " \
"bkgd_table.raw_data as bkgd_enc_val, bkgd_table.file_size as bkgd_file_size, bkgd_table.chunk_size as bkgd_chunk_size " \
"FROM raw_table JOIN bkgd_table ON raw_table.raw_id = bkgd_table.raw_id"
table_result = t_env.sql_query(query)
ds = t_env.to_append_stream(table_result,
Types.ROW([<http://Types.INT|Types.INT>(), Types.STRING(), <http://Types.INT|Types.INT>(), <http://Types.INT|Types.INT>(), Types.STRING(), <http://Types.INT|Types.INT>(), <http://Types.INT|Types.INT>()]))
with ds.execute_and_collect() as results:
for result in results:
print(result)Dian Fu
02/17/2023, 1:52 AMds.process / ds.flat_map to perform the transformations you want to do in process_data.Amir Hossein Sharifzadeh
02/17/2023, 3:08 AM(raw_id: int , raw_enc_val: string , raw_file_size: int, raw_chunk_size: int, bkgd_enc_val: string, bkgd_file_size: int, bkgd_chunk_size: int) now, how should I pass process_data function to ds.process with those arguments?Dian Fu
02/17/2023, 5:23 AMAmir Hossein Sharifzadeh
02/17/2023, 5:25 AMlambda i: i[0] + i[1] I shall pass my function: process_data ?Amir Hossein Sharifzadeh
02/17/2023, 5:27 AMds = ds.flat_map(process_results) ?Dian Fu
02/17/2023, 5:33 AMAmir Hossein Sharifzadeh
02/17/2023, 5:39 AM.process() function.Dian Fu
02/17/2023, 5:43 AMDian Fu
02/17/2023, 5:44 AMAmir Hossein Sharifzadeh
02/17/2023, 5:48 AMprocess_results into the class. Does ds.flat_map(process_results) sound like a solution?Dian Fu
02/17/2023, 6:57 AMds.flat_map(process_results) , see https://github.com/apache/flink/blob/master/flink-python/pyflink/examples/datastream/word_count.py#L89 for a simple examples about flat_map.