如何使用spark-scala map / split有条件地将逗号更改为分号
how to conditionally change commas into semicolon with spark-scala map / split
我是 spark / scala 的新手,遇到了问题
我需要对一整月的数据进行一些数据操作。为此,我定义了一个 class
scala> case class zahiro(request_datetime: String, ip: String, host: String, request_uri: String, referer: String, useragent: String, uuid: String, country: String)
scala> val lines = sc.textFile("s3://{bucket}/whatever/2015/05/*.*").map(_.split(",")).map(p
=> zahiro(p(0),p(1),p(2),p(3),p(4),p(5),p(6),p(7))).toDF()
但是,useragent 字段可能包含也可能不包含“,”,并用双引号括起来。
我想实现的是有条件地将“,”更改为“;”基于它是 enclosed/encapsulated 带 dbl 引号的条件。
在 PIG 上,我可以使用:
xxx = FOREACH xxx GENERATE request_datetime, ip, host, uuid, country, impression_id, impression_datetime,REPLACE(用户代理,',',';');
直接在定义方案之后,但我真的想在Scala上做
而不是通过一些预处理正则表达式工作
如有任何帮助,我们将不胜感激...
分割前添加休闲地图
val lines = sc.textFile("s3://{bucket}/whatever/2015/05/*.*")
//add the map
.map(line => line.replaceAll("\",\"", ";"))
.map(_.split(","))
.map(p=> zahiro(p(0),p(1),p(2),p(3),p(4),p(5),p(6),p(7)))
扩展 https://whosebug.com/users/3187921/user52045 的评论 -
val lines = sc.textFile("s3://{bucket}/whatever/2015/05/*.*")
//add the map
.map(line => line.replaceAll("\".*,.*\"", ";"))
.map(_.split(","))
.map(p=> zahiro(p(0),p(1),p(2),p(3),p(4),p(5),p(6),p(7)))
完美运行。
我是 spark / scala 的新手,遇到了问题 我需要对一整月的数据进行一些数据操作。为此,我定义了一个 class
scala> case class zahiro(request_datetime: String, ip: String, host: String, request_uri: String, referer: String, useragent: String, uuid: String, country: String)
scala> val lines = sc.textFile("s3://{bucket}/whatever/2015/05/*.*").map(_.split(",")).map(p
=> zahiro(p(0),p(1),p(2),p(3),p(4),p(5),p(6),p(7))).toDF()
但是,useragent 字段可能包含也可能不包含“,”,并用双引号括起来。
我想实现的是有条件地将“,”更改为“;”基于它是 enclosed/encapsulated 带 dbl 引号的条件。
在 PIG 上,我可以使用: xxx = FOREACH xxx GENERATE request_datetime, ip, host, uuid, country, impression_id, impression_datetime,REPLACE(用户代理,',',';');
直接在定义方案之后,但我真的想在Scala上做 而不是通过一些预处理正则表达式工作
如有任何帮助,我们将不胜感激...
分割前添加休闲地图
val lines = sc.textFile("s3://{bucket}/whatever/2015/05/*.*")
//add the map
.map(line => line.replaceAll("\",\"", ";"))
.map(_.split(","))
.map(p=> zahiro(p(0),p(1),p(2),p(3),p(4),p(5),p(6),p(7)))
扩展 https://whosebug.com/users/3187921/user52045 的评论 -
val lines = sc.textFile("s3://{bucket}/whatever/2015/05/*.*")
//add the map
.map(line => line.replaceAll("\".*,.*\"", ";"))
.map(_.split(","))
.map(p=> zahiro(p(0),p(1),p(2),p(3),p(4),p(5),p(6),p(7)))
完美运行。