1

我有一个 SQL Server 表,其架构与我的数据框不同。我想从我的数据框中选择一些列并将我选择的值“插入”到表中。

基本上类似于下面的代码,但在 pyspark 中:

INSERT INTO Cust_Diff_Schema_tbl
(acct_num, name)
SELECT account_no, name
FROM customers
WHERE customer_id > 5000;

我可以使用 spark.read 使用 jdbc 读取数据。就像下面这样:

df_s3 = spark.read.format("jdbc")\
                .option("driver", db_driver_name)\
                .option("url", db_url+ ":1433;databaseName="+stage_db)\
                .option("dbtable", tbl_name)\
                .option("query", """(select * from customers)""")\
                .option("user", db_username)\
                .option("password", db_password)\
                .load()
    
    df_s3.printSchema()
    df_s3.show(20)

要将数据写入/附加到具有所选值的表中,我相信我仍然可以使用“df_s3.write”,但我需要一个关于如何使用“.option”函数或其他方法使用插入语句的示例,如果这不工作。

提前致谢。

4

1 回答 1

0
//create dataframe

val df = //fetch from  db,read file or other options

df.write.format("jdbc")
      .option("numPartitions", 20)
      .option("batchsize", 10000)
      .option("truncate", "true")
      .option("url", "jdbcURL")
      .option("driver", "Driver name")
      .option("dbtable", "tablename")
      .mode("append")
      .save()
于 2020-10-21T16:47:09.947 回答