我是新来激发SQL的人,
在MS SQL中,我们有LEFT关键字LEFT(Columnname,1) in('D','A') then 1 else 0
。
如何在SPARK SQL中实现相同的功能。
最佳答案
您可以将substring
函数与积极的pos
一起使用,从左侧获取:
import org.apache.spark.sql.functions.substring
substring(column, 0, 1)
负
pos
从右边取:substring(column, -1, 1)
因此,在Scala中,您可以定义
import org.apache.spark.sql.Column
import org.apache.spark.sql.functions.substring
def left(col: Column, n: Int) = {
assert(n >= 0)
substring(col, 0, n)
}
def right(col: Column, n: Int) = {
assert(n >= 0)
substring(col, -n, n)
}
val df = Seq("foobar").toDF("str")
df.select(
Seq(left _, right _).flatMap(f => (1 to 3).map(i => f($"str", i))): _*
).show
+--------------------+--------------------+--------------------+---------------------+---------------------+---------------------+
|substring(str, 0, 1)|substring(str, 0, 2)|substring(str, 0, 3)|substring(str, -1, 1)|substring(str, -2, 2)|substring(str, -3, 3)|
+--------------------+--------------------+--------------------+---------------------+---------------------+---------------------+
| f| fo| foo| r| ar| bar|
+--------------------+--------------------+--------------------+---------------------+---------------------+---------------------+
同样在Python中:
from pyspark.sql.functions import substring
from pyspark.sql.column import Column
def left(col, n):
assert isinstance(col, (Column, str))
assert isinstance(n, int) and n >= 0
return substring(col, 0, n)
def right(col, n):
assert isinstance(col, (Column, str))
assert isinstance(n, int) and n >= 0
return substring(col, -n, n)
关于scala - 如何在SPARK SQL中使用LEFT和RIGHT关键字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40136922/