spark.sql("select case when length(date)>0 then regexp_extract(date,'\\\\d ', 0) else '' end as date from input").show(false)
上面的 spark 代碼給出了數字字符的第一個實體的輸出。
樣本輸入:1234avf456wef 樣本輸出:1234456
它應該顯示字串中的所有數字字符。提前致謝
uj5u.com熱心網友回復:
您可以嘗試以下修改,用于regexp_replace洗掉所有非數字字符。
下面是一個作業示例,其中還包括從 '1234vf456wef &%'
spark.sql("with input as (select '1234vf456wef &%' as date union all select '123' union all select 'a very long 123 string with 456 content.') select date as original,case when length(date)>0 then regexp_replace(date,'[^0-9]', '') else '' end as date from input").show()
輸出
---------------------------------------- -------
|original |date |
---------------------------------------- -------
|1234vf456wef &% |1234456|
|123 |123 |
|a very long 123 string with 456 content.|123456 |
---------------------------------------- -------
讓我知道這是否適合您。
uj5u.com熱心網友回復:
最好使用REG_REPLACE
也許您需要使正則運算式適應您的需求
SELECT REGEXP_REPLACE('1234avf456wef ', '[a-zA-Z]', '');| REGEXP_REPLACE('1234avf456wef ', '[a-zA-Z]', '') | | :--------------------------------------------------------- | | 1234456 |
db<>在這里擺弄
將不需要的特殊字符添加到 reg 字串中
SELECT REGEXP_REPLACE('1234avf456wef 5§$$§%&"%%', '[a-zA-Z "§$%&/]', '');| REGEXP_REPLACE('1234avf456wef 5§$$§%&"%%', '[a-zA-Z "§$%&/]', '') | | :------------------------------------------------- ------------------ | | 12344565 |
db<>在這里擺弄
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331484.html
標籤:mysql 数据框 阿帕奇火花 apache-spark-sql
上一篇:Typescript的number或Math.ceil()的什么屬性會導致此算術錯誤?
下一篇:SQL連接與比較運算子問題
