2015-10-06 40 views
3
from pyspark import SparkConf, SparkContext 
from pyspark.sql import SQLContext 

conf = SparkConf().setAppName("Test").set("spark.driver.memory", "1g") 
sc = SparkContext(conf = conf) 

sqlContext = SQLContext(sc) 

results = sqlContext.sql("/home/ubuntu/workload/queryXX.sql") 

當我使用:python test.py執行此命令時,它給了我一個error如何使用python執行spark中的.sql文件

y4j.protocol.Py4JJavaError: An error occurred while calling o20.sql. : java.lang.RuntimeException: [1.1] failure: ``with'' expected but `/' found

/home/ubuntu/workload/queryXX.sql

at scala.sys.package$.error(package.scala:27) 

我很新的火花,我需要幫助這裏繼續前進。

回答

0

我不確定它會回答你的問題。但是,如果你打算運行在現有的表查詢,您可以使用,

spark-sql -i <Filename_with abs path/.sql> 

還有一件事,如果你有pyspark腳本,您可以使用here火花提交的細節。

2

SqlContext.sql預計有效的SQL查詢不是文件的路徑。試試這個:

with open("/home/ubuntu/workload/queryXX.sql") as fr: 
    query = fr.read() 
results = sqlContext.sql(query) 
1

運行spark-sql --help會給你

CLI options: 
-d,--define <key=value>   Variable subsitution to apply to hive 
            commands. e.g. -d A=B or --define A=B 
    --database <databasename>  Specify the database to use 
-e <quoted-query-string>   SQL from command line 
-f <filename>     SQL from files 
-H,--help      Print help information 
    --hiveconf <property=value> Use value for given property 
    --hivevar <key=value>   Variable subsitution to apply to hive 
            commands. e.g. --hivevar A=B 
-i <filename>     Initialization SQL file 
-S,--silent      Silent mode in interactive shell 
-v,--verbose      Verbose mode (echo executed SQL to the 
            console) 

所以,你可以執行你這樣的SQL腳本:

spark-sql -f <your-script>.sql

相關問題