gpt4 book ai didi

scala - 在给定时间戳下以星火打开文件

转载 作者:行者123 更新时间:2023-12-02 21:16:20 24 4
gpt4 key购买 nike

我在scala类中使用newAPIHadoopFile从HDFS读取文本文件,如下所示

val conf = new SparkConf
val sc = new SparkContext(conf)
val hc = new Configuration(sc.hadoopConfiguration)
val dataFilePath = "/data/sample"
val input = sc.newAPIHadoopFile(dataFilePath, classOf[TextInputFormat], classOf[LongWritable], classOf[Text], hc)

但是现在我只需要在一个时间戳范围内打开文件。
关于我该怎么做的任何想法?

谢谢,
杰夫

最佳答案

如果您的文件直接在文件名中包含时间戳,则非常简单:

val path = "/hdfs/some_dir/2016-07-*/*" 
val data = sqlContext.jsonFile(data) // or textFile e.g.
data.count() // number of rows in all files matching pattern

这将读取2016年7月的所有目录以及这些目录中的所有文件。您甚至可以对文件名进行模式匹配,例如 val path = "/hdfs/some_dir/2016-07-01/file-*.json"
这是否有帮助,或者您正在寻找系统时间戳过滤?

编辑:
如果您需要使用系统时间戳进行过滤:
val path = "/hdfs/some_dir/"
val now: Long = System.currentTimeMillis / 1000
var files = new java.io.File(path).listFiles.filter(_.lastModified >= now)

或者,您可以构建更复杂的日期过滤,例如以“人工”方式选择日期。现在应该很容易。

关于scala - 在给定时间戳下以星火打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38690136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com