样本表:
+---------------------------------------------------------++------+
| 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。
答案 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'""")