如何将where子句作为变量传递给Spark SQL中的查询?

时间:2016-04-18 09:32:22

标签: scala apache-spark dataframe hive apache-spark-sql

样本表:

    +---------------------------------------------------------++------+
    |                  Name_Age                               ||  ID  | 
    +---------------------------------------------------------++------+
    |"SUBHAJIT SEN":28,"BINOY MONDAL":26,"SHANTANU DUTTA":35  ||  15  |
    |"GOBINATHAN SP":35,"HARSH GUPTA":27,"RAHUL ANAND":26     ||  16  |
    +---------------------------------------------------------++------+

如何将WHERE子句作为变量传递给查询?

我想要的查询是:Select Name_Age from table where ID=15所以变量是ID = 15。

2 个答案:

答案 0 :(得分:2)

如果数据已经注册为表格(A Hive表格或在registerTempTable上致电DataFrame后),您可以使用SQLContext.sql

val whereClause: String = "ID=15"
sqlContext.sql("Select Name_Age from table where " + whereClause)

如果您要查询df: DataFrame个对象:

// using a string filter:
df.filter(whereClause).select("Name_Age")

答案 1 :(得分:-2)

以下代码可以为您提供答案:

spark.sql("""Select Name_Age from table where ID='15'""")