- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在学习 Spark,遇到一个问题,说给两个文本文件找到文本评论超过 100 个单词的书籍,然后过滤结果以仅显示恐怖类别。
这是我的两个文本文件的示例。
BookInformation.data:
在此数据文件中,我有 4 个 key 。
userName, price, categories, title
每个键都有一个值,并且每个键由 ,
作为分隔符分隔。某些键使用字符串值,而其他键使用整数值。
{"username": "JAMES250", "price": 19.20, "categories": "Horror", "title": "Friday the 13th"}
{"username": "Bro2KXA1", "price": 09.21, "categories": "Fantasy", "title": "Wizard of Oz"}
{"username": "LucyLu1272", "price": 18.69, "categories": "Fiction", "title": "Not Real"}
{"username": "6302049040", "price": 08.86, "categories": "Fantasy", "title": "Fantastic"}
...
etc
...
ReviewerText.data
在此数据文件中,我有 5 个 key 。
reviewerID, userName, reviewText, overall, reviewTime
每个键都有一个值,并且每个键由 ,
作为分隔符分隔。某些键使用字符串值,而其他键使用整数值。
{"reviewerID": "A1R3P8MRFSN4X3", "username": "JAMES250", "reviewText": "Wow what a book blah blah… END", "overall": 4.0, "reviewTime": "08 9, 1997"}
{"reviewerID": "AVM91SKZ9M58T", " username ": " Bro2KXA1 ", "reviewText": "Different Blah Blah Blah Blah… So on… END", "overall": 5.0, "reviewTime": "08 10, 1997"}
{"reviewerID": "A1HC72VDRLANIW", " username ": "DiffUser09", "reviewText": "Another Review Blah Blah Blah Blah… So on… END", "overall": 1.0, "reviewTime": "08 19, 1997"}
{"reviewerID": "A2XBTS97FERY2Q", " username ": "MyNameIs01", "reviewText": "I love books. END", "overall": 5.0, "reviewTime": "08 23, 1997"}
...
etc
...
我的目标很简单。
reviewText
。reviewText
后,我想按照总体
评分的顺序对结果进行排序;从 5 开始到 1。然后我还需要为每个打印相应的 Title
。 类别
,以仅显示恐怖
类别。 reviewText
中出现的 恐怖
类别的平均单词数。代码:到目前为止,我正在为每个文件中的每个行条目创建一个 Key:Value 数组。这里的目标是创建一个可以解析任何键并接收其值的数组。
package main.scala
import org.apache.spark.{SparkConf, SparkContext}
import scala.io.StdIn.readLine
import scala.io.Source
object ReviewDataSpark {
def main(args: Array[String]) {
//Create a SparkContext to initialize Spark
val conf = new SparkConf()
conf.setMaster("local")
conf.setAppName("Word Count")
val sc = new SparkContext(conf)
val metaDataFile = sc.textFile("/src/main/resources/BookInformation.data")
val reviewDataFile = sc.textFile("/src/main/resources/ReviewText.data")
reviewDataFile.flatMap { line => {
val Array(label, rest) = line split ","
println(Array)
val items = rest.trim.split("\\s+")
println(items)
items.map(item => (label.trim -> item))
}
}
metaDataFile.flatMap { line => {
val Array(label, rest) = line split ","
println(Array)
val items = rest.trim.split("\\s+")
println(items)
items.map(item => (label.trim -> item))
}
}
}
}
问题:所以我的代码的主要问题是我不相信我正确使用了 flatMap。我似乎无法将键和值溢出到键数组中。
我的代码刚刚打印出来:进程已完成,退出代码为 0
这似乎不正确。
编辑:
所以我更新了我的代码以使用 JSON 库。
val jsonColName = "json"
// intermediate column name where we place each line of source data
val jsonCol = col(jsonColName) // its reusable ref
val metaDataSet = spark.read.textFile("src/main/resources/BookInformation.data")
.toDF(jsonColName).select(get_json_object(jsonCol, "$.username")
.alias("username"), get_json_object(jsonCol, "$.price")
.alias("price"), get_json_object(jsonCol, "$.categories")
.alias("categories"), get_json_object(jsonCol, "$.title")
.alias("title"))
val reviewDataSet = spark.read.textFile("src/main/resources/reviewText.data")
.toDF(jsonColName).select(get_json_object(jsonCol, "$.reviewerID")
.alias("reviewerID"), get_json_object(jsonCol, "$.username")
.alias("username"), get_json_object(jsonCol, "$.reviewText")
.alias("reviewText"), get_json_object(jsonCol, "$.overall")
.alias("overall").as[Double], get_json_object(jsonCol, "$.reviewTime")
.alias("reviewTime"))
reviewDataSet.show()
metaDataSet.show()
多亏了这些信息,我才能够合并。
val joinedDataSets = metaDataSet.join(reviewDataSet, Seq("username"))
joinedDataSets.show()
现在,我的下一步是能够计算 ReviewText
列中 joinedDataSets
内的单词数,并仅保留超过 100 个单词的单词。
如何从键 reviewText
中过滤 JSON 对象,然后计算所有条目并删除少于 100 个单词的条目。
最佳答案
首先,您需要以结构化方式从文件中加载数据。源文件的每一行都可以解析为 JSON,并且信息应正确放置在相应的列中。
例如,加载和解析BookInformation.data
:
import org.apache.spark.sql.functions._ // necessary for col, get_json_object functions and others below
val session = SparkSession.builder().appName("My app")
.master("local[*]")
.getOrCreate()
val bookInfoFilePath = // path to BookInformation.data
val jsonColName = "json" // intermediate column name where we place each line of source data
val jsonCol = col(jsonColName) // its reusable ref
val bookInfoDf = session.read.textFile(bookInfoFilePath).toDF(jsonColName).select(
get_json_object(jsonCol, "$.username").alias("username"),
get_json_object(jsonCol, "$.price").alias("price"),
get_json_object(jsonCol, "$.categories").alias("categories"),
get_json_object(jsonCol, "$.title").alias("title")
)
现在我们有一个包含正确结构化数据的图书信息 DataFrame:
bookInfoDf.show()
+----------+-----+----------+---------------+
| username|price|categories| title|
+----------+-----+----------+---------------+
| JAMES250| 19.2| Horror|Friday the 13th|
| Bro2KXA1| 9.21| Fantasy| Wizard of Oz|
|LucyLu1272|18.69| Fiction| Not Real|
|6302049040| 8.86| Fantasy| Fantastic|
+----------+-----+----------+---------------+
Q3 和 Q4 的答案变得显而易见。
val dfQuestion3 = bookInfoDf.where($"categories" === "Horror")
dfQuestion3.show()
+--------+-----+----------+---------------+
|username|price|categories| title|
+--------+-----+----------+---------------+
|JAMES250| 19.2| Horror|Friday the 13th|
+--------+-----+----------+---------------+
对于第四季度,您必须使用 username
列将 bookInfoDf
与从 ReviewerText.data
加载的 DataFrame 连接起来,然后聚合( .agg
)reviewText
列的平均长度数据(avg
和 length
函数)。
要加载ReviewerText.data
,您可以按照上面加载bookInfoDf
的方式进行类比。 overall
列应在 .alias
调用后使用 .as[Double]
转换为数字。
更新
I had a question about how to count the number of words within a JSON Key/Value. For example, in the key reviewText I have create and merged both BookInformation and ReviewText into one dataset. Now If i wanted to loop through each reviewText and count the number of words then filter either keep or remove depending on the amount of words within the Key's Value how would I go about doing that? I'm trying to learn how to extract value
一种可能的方法是计算单词数并将其存储在专用列中:
// reviewerTextDf is the DataFrame with original data from ReviewerText.data
val dfWithReviewWordsCount = reviewerTextDf.withColumn("nb_words_review", size(split($"reviewText", "\\s+")))
dfWithReviewWordsCount.show()
给出以下内容:
+--------------+--------+--------------------+-------+-----------+---------------+
| reviewerID|username| reviewText|overall| reviewTime|nb_words_review|
+--------------+--------+--------------------+-------+-----------+---------------+
|A1R3P8MRFSN4X3|JAMES250|Wow what a book b...| 4.0| 08 9, 1997| 7|
| AVM91SKZ9M58T| null|Different Blah Bl...| 5.0|08 10, 1997| 8|
|A1HC72VDRLANIW| null|Another Review Bl...| 1.0|08 19, 1997| 9|
|A2XBTS97FERY2Q| null| I love books. END| 5.0|08 23, 1997| 4|
+--------------+--------+--------------------+-------+-----------+---------------+
关于java - 如何在文本文件中查找键并使用 Spark 和 JSON 将它们与另一个文件进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49433834/
我有一个简单的 pyparsing 构造,用于提取日志消息的部分内容。看起来像这样 log_line = 时间戳 + task_info + Suppress(LineEnd()) 此结构可以很好地解
我想定义一个函数 scaryDict(),它接受一个参数(textfile)并返回 textfile 中的单词按字母顺序排列,基本上生成字典但不打印任何一个或两个字母的单词。 这是我目前所拥有的……不
我正在尝试弄清楚如何包含对外部数据文件(文本形式)的引用,我希望通过 Web Start (JNLP) 与我的应用程序一起分发该文件。筛选 JNLP 结构的文档,我发现您可以包含对 JAR、nativ
我尝试将 Java 程序从 Eclipse 导出到 .jar 文件,但遇到了问题。它运行良好,但由于某种原因它没有找到它应该从中获取数据的文本文件。如果有人能帮忙解决这个问题,我将非常感激。 最佳答案
在过去的 20 个小时里,我试图解决以下问题,所以在开始考虑跳出窗外之前我想,我最好在这里寻求帮助: I have a text file with following content: ID 1 T
今天我试图删除一个简单文本文件中的重复行,例如: input (list.txt): hello hello try output (list.txt): try 我尝试使用 Notepad++ 删除
我将一个文本文件添加到我的项目中,如下路径所示: Myproject/WebPages/stopwords.txt 图片: http://s7.postimg.org/w65vc3lx7/Untitl
所以我在我的程序上工作,现在我无法找到解决方案。我需要在 fext 文件中替换更多的符号,目前程序只将“TIT”替换为代码“*245$a”,如果我想用同样的方式替换其他字母,程序不会改变。有人知道如何
这是一个非常简单的问题,但无论我看哪里,我都会得到不同的答案(这是因为它在 c++0x 中已经改变还是将要改变?): 在 C++ 中,我如何从一个文本文件中读取两个数字并将它们输出到另一个文本文件中?
我有一个 C++ 项目应该添加 到每一行的开头和到每一行的末尾。这适用于普通英文文本,但我有一个中文文本文件,我想这样做,但它不起作用。我通常使用 .txt 文件,但为此我必须使用 .rtf 来保存中
所以我的驱动看起来像这样: #include "problem2.h" #include "problem1.h" #include "problem3.h" #include #include
我有一个包含字符串标识符的 ascii 数字文本文件(>50k 行),可以将其视为数据 vector 的集合。根据用户输入,应用程序在运行时只需要这些数据 vector 之一。 据我所知,我有 3 个
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Any decent text diff/merge engine for .NET ? 我有两个文本文件,
我正在尝试将对话选择器中的唤醒时间和 sleep 时间记录到这样的文本文件中,但是对方法 commitToFile2 的调用不会 append 文本文件“savedData.txt”。 我知道这段代码
我开发了一个 android webview 并尝试在单击 webview 中的链接时下载生成的数据:文本文件。 webView.setDownloadListener(new Downloa
我在一个文本文件中有 250 张图像/天 4000*3000 像素。 file '/home/user/camdata/nonseqdata.jpg' file '/home/user/camdata
我曾多次尝试将此配置文件转换为多维数组,这意味着我必须读取 config.txt 文件,然后必须将其转换为多维数组。我需要帮助或一些建议。 配置文件: id=www session.timeout=1
我正在尝试使用 sublime text 3 打开文件,我想用光标在具体行号处打开它。 我一直在查subl --help但我找不到混凝土线的选择。因此我只是使用:subl filename 有没有办法
我想在我的应用程序中快速显示一个大文本文件的内容,而不是将整个文件加载到内存中。 其他人是怎么做的? Total Commander是一个很棒的工具,它有一个很棒的内部查看器可以做到这一点。无论文件有
我是一名优秀的程序员,十分优秀!