我试图复制oracle Instr函数,但是在我看来,Oracle中并不存在所有参数。我收到此错误,我想将此转换包含在表的“平台”字段中,但我不能:
The Apache Spark 2.0 database encountered an error while running this query.
Error running query: org.apache.spark.sql.AnalysisException: Invalid number of arguments for function instr. Expected: 2; Found: 4; line 8 pos 45
SELECT
SUBSTR(a.SOURCE, 0, INSTR(a.SOURCE, '-', 1, 2) - 1) AS plataforma,
COUNT(*) AS qtd
FROM db1.table AS as a
LEFT JOIN db1.table2 AS b ON a.ID=b.id
GROUP BY SUBSTR(a.SOURCE, 0, INSTR(a.SOURCE, '-', 1, 2) - 1)
ORDER BY qtd
我以这种方式进行了字段的转换,但我不知道它是否正确:
如何在Spark中复制相同的Oracle函数?我只需要这样做:
来源:
apache-spark-sql
sql-server-dw
结果:
apache-spark
sql-server
您正在寻找的是substring_index
函数:
substring_index
它在substring_index('apache-spark-sql', '-', 2)
出现2次之前返回子字符串。
我想您想在最后一次出现-
之前获取子字符串。因此,您可以计算输入字符串中的-
数,并将其与-
函数合并,如下所示:
substring_index
substring_index(col, '-', size(split(col, '-')) - 1)
给出size(split(col, '-')) - 1
的出现。