我想使用基于SQL函数的语言集成SQL来过滤schemaRDD。例如,我想运行
SELECT name FROM people WHERE name LIKE '%AHSAN%' AND name regexp '^[A-Z]{20}$'
如何在people.where()中使用这样的SQL函数?
参考:
对于语言集成SQL,我遵循此处给出的示例。
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import sqlContext._
val people: RDD[Person] = ... // An RDD of case class objects, from the first example.
// The following is the same as 'SELECT name FROM people WHERE age >= 10 AND age <= 19'
val teenagers = people.where('age >= 10).where('age <= 19).select('name)
teenagers.map(t => "Name: " + t(0)).collect().foreach(println)
提前感谢!
您可以使用 SQL 函数,如数字运算符。 例如,
people.where('name like "%AHSAN%").where('name rlike "^[A-Z]{20}$").select('name)
Spark SQL中没有regexp
,但它与rlike
相同。