gpt4 book ai didi

scala - 使用文本文件第一行的一部分作为RDD中的key

转载 作者:行者123 更新时间:2023-12-04 17:53:51 24 4
gpt4 key购买 nike

我有一个数据集,由几个名为“01”到“15”的不同文件夹组成,每个文件夹都包含名为“00-00.txt”到“23-59.txt”的文件(每个文件夹描述 1 天).

在我的文件中有如下几行;(每个以!AIVDM开头的条目都是一行,除了第一个,它以数字开头)

1443650400.010568 !AIVDM,1,1,,B,15NOHL0P00J@uq6>h8Jr6?vN2><,0*4B
!AIVDM,1,1,,A,4022051uvOFD>RG7kDCm1iW0088i,0*23
!AIVDM,1,1,,A,23aIhd@P1@PHRwPM<U@`OvN2><,0*4C
!AIVDM,1,1,,A,13n1mSgP00Pgq3TQpibh0?vL2><,0*74
!AIVDM,1,1,,B,177nPmw002:<Tn<gk1toGL60><,0*2B
!AIVDM,1,1,,B,139eu9gP00PugK:N2BOP0?vL2><,0*77
!AIVDM,1,1,,A,13bg8N0P000E2<BN15IKUOvN2><,0*34
!AIVDM,1,1,,B,14bL20003ReKodINRret28P0><,0*16
!AIVDM,1,1,,B,15SkVl001EPhf?VQ5SUTaCnH0><,0*00
!AIVDM,1,1,,A,14eG;ihP00G=4CvL=7qJmOvN0><,0*25
!AIVDM,1,1,,A,14eHMQ@000G<cKrL=6nJ9QfN2><,0*30

我想要一个键值对的 RDD,长值 1443650400.010568 是键,以 !AIVDM... 开头的行是值。我怎样才能做到这一点?

最佳答案

假设每个文件足够小,可以包含在单个 RDD 记录中(不超过 2GB),您可以使用 SparkContext.wholeTextFiles 将每个文件读入单个记录,然后 flatMap 这些记录:

// assuming data/ folder contains folders 00, 01, ..., 15
val result: RDD[(String, String)] = sc.wholeTextFiles("data/*").values.flatMap(file => {
val lines = file.split("\n")
val id = lines.head.split(" ").head
lines.tail.map((id, _))
})

或者,如果该假设不正确(每个单独的文件可能很大,即数百 MB 或更多),您需要更加努力地工作:将所有数据加载到单个 RDD 中,将索引添加到数据,收集每个索引的“键”映射,然后使用这些索引为每个数据行找到正确的键:

// read files and zip with index to later match each data line to its key
val raw: RDD[(String, Long)] = sc.textFile("data/*").zipWithIndex().cache()

// separate data from ID rows
val dataRows: RDD[(String, Long)] = raw.filter(_._1.startsWith("!AIVDM"))
val idRows: RDD[(String, Long)] = raw.filter(!_._1.startsWith("!AIVDM"))

// collect a map if Index -> ID
val idForIndex = idRows.map { case (row, index) => (index, row.split(" ").head) }.collectAsMap()

// optimization: if idForIndex is very large - consider broadcasting it or not collecting it and using a join

// map each row to its key by looking up the MAXIMUM index which is < then row index
// in other words - find the LAST id record BEFORE the row
val result = dataRows.map { case (row, index) =>
val key = idForIndex.filterKeys(_ < index).maxBy(_._1)._2
(key, row)
}

关于scala - 使用文本文件第一行的一部分作为RDD中的key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40057094/

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