spark字符串操作处理

一、字符串截取

  • 开始截取位置的索引是从1开始的;
  • 截取的不正确时,看开头和结尾是否有空格

1. sql语句中使用

1 返回字符串A从start位置到结尾的字符串
select substring('abcde',3);
cde
select substring('abcde',-2);
de

2 返回字符串A从start位置开始,长度为len的字符串
select substring('abcde',3,2);
cd
select substring('abcde',-2,2);
de

2. dsl语句中使用

// 1 构造测试数据
val arr = Array("abcde")
val df = spark.sparkContext.makeRDD(arr).toDF("id")

// 2 操作举例
val res = df
  .withColumn("id2", substring($"id", 2, 2)) // 结果bc, 注意截取起始位置索引是从1开始的
  .withColumn("id3", substring($"id", -2, 2)) // 结果的, 截取字符串的后两位
  .withColumn("id4", $"id".substr(2, 2)) // 结果bc, 注意截取起始位置索引是从1开始的
  .withColumn("id5", $"id".substr(-2, 2)) // 结果的, 截取字符串的后两位

// 3 展示结果
res.show()

+-----+---+---+---+---+
|   id|id2|id3|id4|id5|
+-----+---+---+---+---+
|abcde| bc| de| bc| de|
+-----+---+---+---+---+

二、字符串分隔

1. 分割成单字符数组

-- 方式01
select filter(split('abc', ''), x -> x!='') arr

+---------+
|      arr|
+---------+
|[a, b, c]|
+---------+

-- 方式02
select array_remove(split('abc', ''), '') arr

+---------+
|      arr|
+---------+
|[a, b, c]|
+---------+

三、 trim操作

-- ltrim(trimStr, str) - Removes the leading string contains the characters from the trim string
SELECT ltrim('Sp', 'SSparkSQLS');  # 删除字符串开头位置是'S'或'p'的字符
arkSQLS

四、 解析URL

1 regexp_extract

regexp_extract(str, regexp[, idx]) - Extract the first string in the str that match the regexp expression and corresponding to the regex group index.
> SELECT regexp_extract('100-200', '(\\d+)-(\\d+)', 1);
100
 1) 解析参数--URL不规范也可以解析
select regexp_extract('https://rasre.com?pageId=385','pageId=([^&]*)');


2  parse_url

parse_url(url, partToExtract[, key]) - Extracts a part from a URL.

Examples:

SELECT parse_url('http://spark.apache.org/path?query=1', 'HOST');
spark.apache.org

SELECT parse_url('http://spark.apache.org/path?query=1', 'QUERY');
query=1

SELECT parse_url('http://spark.apache.org/path?query=1', 'QUERY', 'query');
1

五、字符串拼接

1 concat和concat_ws对null的不同处理

1) concat只要有一个参数为null,结果就为null

select concat('620',null,'12','25');
NULL

2) concat_ws当有参数为null时, 当这个元素不存在, 不会导致结果为null

select concat_ws('-','620',null,'12','25');
620-12-25

六、正则提取和替换

1) 参考链接

正则表达参考01

2)regexp_extract

regexp_extract(str, regexp[, idx]) - Extracts a group that matches regexp.

Examples:

SELECT regexp_extract('100-200', '(\d+)-(\d+)', 1);

100

3)regexp_replace

regexp_replace(str, regexp, rep) - Replaces all substrings of str that match regexp with rep.

Examples:

> SELECT regexp_replace('100-200', '(\d+)', 'num');

num-num

4)replace 非正则,精确的替换

replace(str, search[, replace]) - Replaces all occurrences of search with replace.

Examples:

> SELECT replace('ABCabc', 'abc', 'DEF');

ABCDEF

5)translate 多个单个字符的替换

translate(input, from, to) - Translates the input string by replacing the characters present in the from string with the corresponding characters in the to string.

Examples:

> SELECT translate('AaBbCc', 'abc', '123');

A1B2C3

七、字符比较

在Java中,空串与其他字符串进行字母排序时,空串会被认为是最小的。

一百、 可能问题

字符串两边有空格:

字符串区分大小写:

字符串中有不可见字符:正则匹配(\\s)判断是否存在


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值