Scala 如何在sparksql中使用LEFT和RIGHT关键字

Scala 如何在sparksql中使用LEFT和RIGHT关键字,scala,apache-spark,apache-spark-sql,Scala,Apache Spark,Apache Spark Sql,我对spark SQL是新手 在MS SQL中,我们在('D','A')中有LEFT关键字,LEFT(Columnname,1),然后是1 else 0 如何在SPARK SQL中实现相同的功能。您可以使用带正数的pos函数从左侧获取: import org.apache.spark.sql.functions.substring substring(column, 0, 1) 从右侧取负pos: substring(column, -1, 1) 所以在Scala中,您可以定义 impor

我对spark SQL是新手

在MS SQL中,我们在('D','A')中有LEFT关键字,
LEFT(Columnname,1),然后是1 else 0


如何在SPARK SQL中实现相同的功能。

您可以使用带正数的
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
+--------------------+--------------------+--------------------+---------------------+---------------------+---------------------+
|子串(str,0,1)|子串(str,0,2)|子串(str,0,3)|子串(str,-1,1)|子串(str,-2,2)|子串(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)

要基于user6910411的答案构建,还可以使用isin,然后使用角色比较的结果构建一个新列

最终的完整代码如下所示

import org.apache.spark.sql.functions._

df.select(substring($"Columnname", 0, 1) as "ch")
    .withColumn("result", when($"ch".isin("D", "A"), 1).otherwise(0))
使用
子字符串(列,0,1)
而不是
函数

在哪里

  • 0:字符串中的起始位置
  • 1:要选择的字符数

例子:考虑左函数:

LEFT(upper(SKU),2)
相应的SparkSQL语句将是:

substring(upper(SKU),1,2) 

从Spark 2.3开始,Spark SQL有右和左两种功能

假设您有以下数据帧

+----------------------+
|some_string           |
+----------------------+
|this 23 has 44 numbers|
|no numbers            |
|null                  |
+----------------------+
下面介绍如何使用SQL
left
函数获取最左边的两个元素:

df.select(expr(“left(some_string,2)”).as(“left_two”)).show(false)
将SQL字符串传递到
expr()
并不理想。Scala API用户不想处理SQL字符串格式

我创建了一个名为的库,可以方便地访问左函数:

df.select(bebe_left(col(“some_string”),lit(2)).as(“left_two”).show()
Spark SQL
right
bebe\u right
函数的工作方式类似


您可以将Spark SQL函数与
expr
hack一起使用,但最好使用更灵活和类型安全的bebe函数。

如果您将JDBC用于MS SQL server,那么您可以运行完全相同的语句。我正在从Azure的拼花地板表读取数据,我想知道Spark SQL@cricket\u 007中的LEFT关键字
+----------------------+
|some_string           |
+----------------------+
|this 23 has 44 numbers|
|no numbers            |
|null                  |
+----------------------+
+--------+
|left_two|
+--------+
|th      |
|no      |
|null    |
+--------+
+--------+
|left_two|
+--------+
|th      |
|no      |
|null    |
+--------+