【问题标题】:Set path file as parameter didnt worked in python pyspark将路径文件设置为参数在 python pyspark 中不起作用
【发布时间】:2020-09-10 06:43:55
【问题描述】:

我想运行一个代码,它可以使用 jdbc 驱动程序摄取数据并将其保存到文件路径中。它成功摄取了数据,但保存功能不起作用。我知道我们可以使用这样的代码来保存数据:

a.write.mode("overwrite").parquet("test/partition_test.parquet")

有什么方法可以将文件路径设置为参数?我试过像下面这样设置参数,但是没有用。

我的代码:

def ingest(spark, db_url, tablename, username, password,destination, driver, save_format="parquet"):
    a = spark.read.format("jdbc").option("url",db_url).option("dbtable",tablename).option("user", username).option("password",password).option("path", destination).option("driver",driver).load()   
    return a


ingest(spark, "jdbc:mysql://192.168.122.1:3306/users", "users", "root", "123456@h21","/path", "com.mysql.jdbc.Driver", save_format="parquet")

【问题讨论】:

    标签: python apache-spark pyspark data-ingestion


    【解决方案1】:

    您在代码中将两件事混合在一起。您需要做的是分两步:

    1. 将数据读入数据帧
    2. 将数据帧写入文件

    所以代码需要是这样的:

    def ingest(spark, db_url, tablename, username, password, destination, 
        driver, save_format="parquet"):
        a = spark.read.format("jdbc").option("url",db_url)\
           .option("dbtable",tablename).option("user", username)\
           .option("password",password).option("driver",driver).load()
        a.write.format(save_format).save(destination)
        return a
    

    这个函数会返回dataframe,但是如果你只是需要读写数据,那么你可以返回None而不是dataframe。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      相关资源
      最近更新 更多