0

我正在尝试通过 pyspark 中的以下代码将 sql server 表转换为 .csv 格式。

from pyspark import SparkContext
sc = SparkContext("local", "Simple App")
from pyspark.sql import SQLContext, Row
sqlContext = SQLContext(sc)

    df = sqlContext.read.format("jdbc").option("url","jdbc:sqlserver://server:port").option("databaseName","database").option("driver","com.microsoft.sqlserver.jdbc.SQLServerDriver").option("dbtable","table").option("user","uid").option("password","pwd").load()

    df.registerTempTable("test")
    df.write.format("com.databricks.spark.csv").save("full_path")

所以,如果我想转换多个表,我需要编写多个数据框。所以,为了避免这种情况,我想为数据库名称和用户的表名取命令行参数,同时通过迭代数据框for 循环。

甚至可能吗?如果是,有人可以通过 spark-submit 指导我如何做到这一点吗?

4

1 回答 1

5

只需对 spark-submit 命令和您的代码进行此更改:

测试.py

import sys
from pyspark import SparkContext
sc = SparkContext("local", "Simple App")
from pyspark.sql import SQLContext, Row
sqlContext = SQLContext(sc)
db_name = sys.argv[1]
table_name = sys.argv[2]
file_name = sys.argv[3]
df = sqlContext.read.format("jdbc").option("url","jdbc:sqlserver://server:port").option("databaseName",db_name).option("driver","com.microsoft.sqlserver.jdbc.SQLServerDriver").option("dbtable",table_name).option("user","uid").option("password","pwd").load()

df.registerTempTable("test")
df.write.format("com.databricks.spark.csv").save(file_name)

火花提交命令: spart-submit test.py <db_name> <table_name> <file_name>

于 2017-09-07T10:34:58.873 回答