簡體   English   中英

Pyspark 從 Kafka 流向 Hudi

[英]Pyspark streaming from Kafka to Hudi

我是 hudi 的新手,但遇到了問題。 我正在使用 pyspark、Kafka 在 AWS 中使用 EMR,我想做的是使用 pyspark 流從 Kafka 集群讀取主題,然后以 hudi 格式將其移動到 S3。 老實說,自幾周前以來我已經嘗試了很多,但我不知道這是否可行。 有人可以告訴我嗎? 我正在使用的代碼是:

    #Reading
    df_T = spark.readStream \
        .format("kafka") \
        .options(**options_read) \
        .option("subscribe", topic) \
        .load() 

....

    hudi_options = {
        'hoodie.table.name': MyTable,
        'hoodie.datasource.write.table.name': MyTable,
        'hoodie.datasource.write.recordkey.field': MyKeyInTable,
        'hoodie.datasource.write.partitionpath.field': MyPartitionKey,
        'hoodie.datasource.write.hive_style_partitioning': "true",
        'hoodie.datasource.write.row.writer.enable': "false",
        'hoodie.datasource.write.operation': 'bulk_insert',
        'hoodie.datasource.write.precombine.field': MyTimeStamp,
        'hoodie.insert.shuffle.parallelism': 1,
        'hoodie.consistency.check.enabled': "true",
        'hoodie.cleaner.policy': "KEEP_LATEST_COMMITS",
        'hoodie.datasource.write.storage.type': 'MERGE_ON_READ',
        'hoodie.compact.inline': "false",
        'hoodie.datasource.hive_sync.table': MyTable,
        'hoodie.datasource.hive_sync.partition_fields': MyPartitionKey,
        'hoodie.datasource.hive_sync.database' : Mydatabase,
        'hoodie.datasource.hive_sync.auto_create_database': "true",
        'hoodie.datasource.write.keygenerator.class': "org.apache.hudi.keygen.ComplexKeyGenerator",
        'hoodie.datasource.hive_sync.partition_extractor_class': "org.apache.hudi.hive.MultiPartKeysValueExtractor",
        'hoodie.datasource.hive_sync.enable': 'true',
        'hoodie.datasource.hive_sync.skip_ro_suffix': 'true'
    }

....

    ds = df_T \
        .writeStream \
        .outputMode('append') \
        .format("org.apache.hudi") \
        .options(**hudi_options)\
        .option('checkpointLocation', MyCheckpointLocation) \
        .start(MyPathLocation) \
        .awaitTermination(300)

....

EMR 中的這段代碼表示工作正常,但當我要查找 hudi 文件時,它不會創建任何文件。 我知道 kafka 配置有效,因為當我在 output 模式下設置“控制台”時,它工作正常,有人可以幫助我嗎?

大家好我可以修復這個錯誤,首先你必須清理 dataframe,不是所有的,但至少你在表中的主鍵是 null 的所有字段。作為第二點,在 hoodie.datasource 中。 write.precombine.field 你可以設置

...

import datetime

currentDate = datetime.datetime.now() 

#As for example:

    hudi_options = {
...
        'hoodie.datasource.write.precombine.field': currentDate,
...
    }

最后,如果你的 dataframe 中沒有時間戳,你可以這樣設置:

.withColumn('Loaded_Date', F.lit(currentDate).cast('timestamp')) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM