pyspark 使用 s3 中的正则表达式/glob 选择文件子集 [英] pyspark select subset of files using regex/glob from s3
问题描述
我在亚马逊 s3 上有一个数字文件,每个文件都按日期 (date=yyyymmdd)
分隔.这些文件可以追溯到 6 个月前,但我想限制我的脚本仅使用最近 3 个月的数据.我不确定我是否能够使用正则表达式来做类似 sc.textFile("s3://path_to_dir/yyyy[m1,m2,m3]*")
其中 m1,m2,m3 表示从我想使用的当前日期算起的 3 个月.
一个讨论还建议使用类似 sc.textFile("s3://path_to_dir/yyyym1*","s3://path_to_dir/yyyym2*","s3://path_to_dir/yyyym3*")
但这似乎对我不起作用.
sc.textFile( )
是否采用正则表达式?我知道您可以使用 glob 表达式,但我不确定如何将上述情况表示为 glob 表达式?
对于你的第一个选项,使用花括号:
sc.textFile("s3://path_to_dir/yyyy{m1,m2,m3}*")
对于第二个选项,您可以将每个单独的 glob 读入一个 RDD,然后将这些 RDD 合并为一个:
m1 = sc.textFile("s3://path_to_dir/yyyym1*")m2 = sc.textFile("s3://path_to_dir/yyyym2*")m3 = sc.textFile("s3://path_to_dir/yyyym3*")全部 = m1.union(m2).union(m3)
您可以将 glob 与 sc.textFile
一起使用,但不能使用完整的正则表达式.
I have a number files each segregated by date (date=yyyymmdd)
on amazon s3. The files go back 6 months but I would like to restrict my script to only use the last 3 months of data. I am unsure as to whether I will be able to use regular expressions to do something like sc.textFile("s3://path_to_dir/yyyy[m1,m2,m3]*")
where m1,m2,m3 represents the 3 months from the current date that I would like to use.
One discussion also suggested using something like sc.textFile("s3://path_to_dir/yyyym1*","s3://path_to_dir/yyyym2*","s3://path_to_dir/yyyym3*")
but that doesn't seem to work for me.
Does sc.textFile( )
take regular expressions? I know you can use glob expressions but I was unsure how to represent the above case as a glob expression?
For your first option, use curly braces:
sc.textFile("s3://path_to_dir/yyyy{m1,m2,m3}*")
For your second option, you can read each single glob into an RDD and then union those RDDs into a single one:
m1 = sc.textFile("s3://path_to_dir/yyyym1*")
m2 = sc.textFile("s3://path_to_dir/yyyym2*")
m3 = sc.textFile("s3://path_to_dir/yyyym3*")
all = m1.union(m2).union(m3)
You can use globs with sc.textFile
but not full regular expressions.
这篇关于pyspark 使用 s3 中的正则表达式/glob 选择文件子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!