我是stackoverflow的新手(老读者,但我现在想参与)。我也是Scala、Spark和函数式编程的新手。期待着在各方面做出贡献和学习。
我的问题是:
我正在使用可变记录长度(文件中的多个部分)和固定位置字段(也称为固定宽度-其中格式由列宽度指定)。例如,myfile.txt布局(从1开始)是: 1-5 =第1列,5-6 =第2列,6-20 =第3列,20-28 =第4列;而子头-a2至子脚注-z2具有完全不同的布局1-3 =第1列,3-6 =第2列,6-11 =第3列
myfile.txt示例:
header
sub-header-a1
1234a Mr. John Doe 19770101
4321a Mrs. Jane Doe19770101
sub-footer-z1
sub-header-a2
1203400001
4302100001
sub-footer-z2
footer应该从baseRDDInput创建两个独立的RDD。
第一个RDD
1234a Mr. John Doe 19770101
4321a Mrs. Jane Doe19770101第二个RDD
1203400001
4302100001我到处寻找代码示例,从一个基本RDD中选择一个范围并转换为另一个RDD。找到了this,但我有一个StringRDD,但我不知道RangePartitioner部分。我找到的所有其他文件读取示例都是csv,并且没有嵌套的节。
这是我到目前为止所知道的:
// created a base RDD from raw file, I assumed that I need an index
val baseRDDinput = sc.textFile("myfile.txt") zipWithIndex ()
// get the start and end point of my range
val (start, end) = ("sub-header-a1", "sub-footer-z1")
// get the index of start and end point
???
// iterator over index in order (index is stable based on comments https://stackoverflow.com/questions/26828815/how-to-get-element-by-index-in-spark-rdd-java) and select elements between start and end index and create RDD-1 then do the same with next section.
???
// next based on code examples from (https://stackoverflow.com/questions/8299885/how-to-split-a-string-given-a-list-of-positions-in-scala) I will parse the element and make k/v using the first column of file as the key任何关于方法和/或代码的建议都将不胜感激。我只需要在正确的方向上推动一下。提前谢谢。
更新:固定链接
发布于 2015-08-06 17:28:19
你用这种方式存储数据的方法是错误的,你需要有两个单独的文件,而不是把所有的东西都放在一个文件中。
在本文(http://0x0fff.com/spark-hdfs-integration/)中,我获得了一个使用Hadoop从InputFormats中读取数据的示例。你需要使用org.apache.hadoop.mapreduce.lib.input.TextInputFormat,它会返回一对值-第一个值很长,表示从文件开始的偏移量,第二个值是文件本身的行。这样你就可以找到带有"sub-header-a1","sub-footer-a1“等值的行,记住它们的偏移量,并在它们上面过滤,形成两个独立的RDDs
https://stackoverflow.com/questions/31838727
复制相似问题