【问题标题】:RDD filter in scala sparkscala spark中的RDD过滤器
【发布时间】:2015-06-27 07:44:25
【问题描述】:

我有一个数据集,我想提取那些(评论/时间)在 x 和 y 之间的(评论/文本),例如(1183334400

这是我的部分数据:

product/productId: B000278ADA
product/title: Jobst Ultrasheer 15-20 Knee-High Silky Beige Large
product/price: 46.34
review/userId: A17KXW1PCUAIIN
review/profileName: Mark Anthony "Mark"
review/helpfulness: 4/4
review/score: 5.0
review/time: 1174435200
review/summary: Jobst UltraSheer Knee High Stockings
review/text: Does a very good job of relieving fatigue.

product/productId: B000278ADB
product/title: Jobst Ultrasheer 15-20 Knee-High Silky Beige Large
product/price: 46.34
review/userId: A9Q3932GX4FX8
review/profileName: Trina Wehle
review/helpfulness: 1/1
review/score: 3.0
review/time: 1352505600
review/summary: Delivery was very long wait.....
review/text: It took almost 3 weeks to recieve the two pairs of stockings .

product/productId: B000278ADB
product/title: Jobst Ultrasheer 15-20 Knee-High Silky Beige Large
product/price: 46.34
review/userId: AUIZ1GNBTG5OB
review/profileName: dgodoy
review/helpfulness: 1/1
review/score: 2.0
review/time: 1287014400
review/summary: sizes recomended in the size chart are not real
review/text: sizes are much smaller than what is recomended in the chart. I tried to put it and sheer it!.

我的 Spark-Scala 代码:

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.io.{LongWritable, Text}
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat
import org.apache.spark.{SparkConf, SparkContext}

object test1 {
  def main(args: Array[String]): Unit = {
    val conf1 = new SparkConf().setAppName("golabi1").setMaster("local")
    val sc = new SparkContext(conf1)
    val conf: Configuration = new Configuration
    conf.set("textinputformat.record.delimiter", "product/title:")
    val input1=sc.newAPIHadoopFile("data/Electronics.txt",     classOf[TextInputFormat], classOf[LongWritable], classOf[Text], conf)
    val lines = input1.map { text => text._2}
    val filt = lines.filter(text=>(text.toString.contains(tt => tt in (startdate until enddate))))
    filt.saveAsTextFile("data/filter1")
  }
}

但是我的代码运行不好,

如何过滤这些行?

【问题讨论】:

  • 我在您的输入文件中没有看到分隔符字符串“product/productId:”。
  • 您期望输出什么以及您面临什么问题?

标签: scala apache-spark


【解决方案1】:

比这简单得多。试试这个:

object test1 
{
  def main(args: Array[String]): Unit = 
  {
    val conf1 = new SparkConf().setAppName("golabi1").setMaster("local")
    val sc = new SparkContext(conf1)

    def extractDateAndCompare(line: String): Boolean=
    {
        val from = line.indexOf("/time: ") + 7
        val to = line.indexOf("review/text: ") -1
        val date = line.substring(from, to).toLong
        date > startDate && date < endDate
    }

    sc.textFile("data/Electronics.txt")
        .filter(extractDateAndCompare)
        .saveAsTextFile("data/filter1")
  }
}

我通常会找到那些中间辅助方法来让事情变得更清晰。当然,这假设边界日期是在某处定义的,并且输入文件包含格式问题。我这样做是为了保持简单,但添加 try、返回 Option 子句并使用 flatMap() 可以帮助您避免错误(如果有)。

此外,您的原始文本有点繁琐,您可能想探索 Json、TSV 文件或其他一些替代的、更简单的格式。

【讨论】:

  • 注意,我在这里从头开始编码,可能有一些关于索引的小细节等。但我希望你能明白。
  • 亲爱的 Daniel,我有 1 GB 的评论数据集(文本)这是我的数据集示例:product/productId: B000278ADA product/title: Jobst Ultr product/price: 46.34 review/userId: A1ZJAH4 review /profileName:jud doolit 评论/帮助:0/0 评论/得分:5.0 评论/时间:1359936000 评论/总结:一站式购物评论/文本:找到您正在寻找的东西真是太好了。我想提取某个时间段内的评论/文本,例如我想提取 2002 年的评论/文本,对于这项工作,我编写了上面的代码,将一个完整的评论数据视为 RDD 的记录
  • 哦,我看到你更新了示例文本。所以这意味着每个“记录”都会产生多行?
  • 您的代码在这些行中确实有意义。但是,一旦你有一个 RDD[String] 这个解决方案应该可以工作,那么你的问题到底是什么?
  • 如果它解决了您的问题,您应该接受它,它可以帮助人们寻找未解决的问题。欢迎访问该网站!
猜你喜欢
  • 2017-06-26
  • 1970-01-01
  • 2020-12-14
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
相关资源
最近更新 更多