所用的软件版本:
spark2.3.0
hbase1.4.6
IDEA2019.1
今天遇到一个问题,sparkStreaming的DataFrame的有些column的内容有换行符\n,然后DF.show()显示时,发现column内容有换行符\n的column,显示有两行。网上查找如何解决的途径,发现filter()方法只是对column名的内容进行过滤选择,对column的内容无法修改,按照https://www.jianshu.com/p/6b8e5963f342和https://stackoverflow.com/questions/53016567/scala-dataframe-replace-spaces-with-null-value-using-regexp-replace文章提示,regexp_replace的方法可以进行正则修改regexp_replace(e: Column, pattern: String, replacement: String): Column
首先要导入下面的包:
import org.apache.spark.sql.functions.regexp_replace
然后利用withColum()的方法,能够把column的内容里的换行符\n进行去除
DF.withColumn("列名", regexp_replace(DF("列名"), "\n", ""))
类似的还有一个regexp_extract()方法进行提取!!
做好记录,供以后参考!!