【发布时间】:2019-03-14 23:15:22
【问题描述】:
我是 scala 新手,我需要逐行读取 ttl 文件并在特定分隔符上拆分并提取值以放入数据框中的相应列中。
< http://website/Jimmy_Carter> <http://web/name> "James Earl Carter, Jr."@ko .
< http://website/Jimmy_Car> <http://web/country> <http://website/United_States> .
< http://website/Jimmy_Car> <http://web/birthPlace> <http://web/Georgia_(US)> .
我想要这个输出
+-------------------------------+---------------------------+-----------------------------
|S |P |O |
+-------------------------------+---------------------------+-----------------------------
|http://website/Jimmy_Car |http://web/name |"James Earl Carter |
|http:///website/Jimmy_Car |http://web/country |http://web/country |
|http://website/Jimmy_Car |http://web/birthPlace |http://web/Georgia_(US) |
|
我试过这段代码
case class T(S: Option[String], P: Option[String],O:Option[String])
val triples = sc.textFile("triples_test.ttl").map(_.split(" |\\< |\\> |\\ . ")).map(p =>
T(Try(p(0).toString()).toOption,Try(p(1).toString()).toOption,Try(p(2).toString()).toOption)).toDF()
我得到了这个结果
+-------------------------------+---------------------------+-----------------------------
|S |P |O |
+-------------------------------+---------------------------+-----------------------------
|<http://website/Jimmy_Car |<http://web/name |"James |
|<http:///website/Jimmy_Car |<http://web/country |<http://web/country |
|<http://website/Jimmy_Car |<http://web/birthPlace |<http://web/Georgia_(US)
为了删除每个三元组开头的分隔符“
val triples = sc.textFile("triples_test.ttl").map(_.split(" |\\< |\\> |\\ . |<")).map(p =>
T(Try(p(0).toString()).toOption,Try(p(1).toString()).toOption,Try(p(2).toString()).toOption)).toDF()
我得到了这个结果
+-------------------------------+---------------------------+-----------------------------
|S |P |O |
+-------------------------------+---------------------------+-----------------------------
| |http://web/name | |
| |http://web/country | |
| |http://web/birthPlace |
我该如何解决这个问题
【问题讨论】:
-
嗨@NTH 我将代码更改为Scala,以防您无法执行它!祝你好运
-
你好@AlexandrosBiratsis,非常感谢!因为我是 scala 编程的初学者,我也没有使用 python。但我忘记了我的问题的一个细节。在数据框的第三列(O 列)中,我只需要以“http://web/”开头的数据。因此,根据示例,我不需要将包含“James Earl Carter”的文件的所有行都放在数据框中。
-
那么您必须删除负责该操作的正则表达式部分:) 您只需要稍微更改 url_regex 即可。试一试你会需要它们,而且是非常强大的工具...
-
@AlexandrosBiratsis 不,我没有删除它,因为我需要它,但我添加了这行代码 "val DF = dfA.filter($"Object".contains("http://web/ “))”。非常感谢:)
-
那很好:)
标签: apache-spark split semantic-web