从Pyspark中的多个目录读取拼花地板文件
我需要从不是父目录或子目录的多个路径读取拼花地板文件 比如说,从Pyspark中的多个目录读取拼花地板文件,pyspark,parquet,Pyspark,Parquet,我需要从不是父目录或子目录的多个路径读取拼花地板文件 比如说, dir1 --- | ------- dir1_1 | ------- dir1_2 dir2 --- | ------- dir2_1 | ------- dir2_2 sqlContext.read.parquet(dir1)从dir1\u 1和dir1\u 2读取拼花文件 现在我正在读取每个目录并使用“unionA
dir1 ---
|
------- dir1_1
|
------- dir1_2
dir2 ---
|
------- dir2_1
|
------- dir2_2
sqlContext.read.parquet(dir1)
从dir1\u 1和dir1\u 2读取拼花文件
现在我正在读取每个目录并使用“unionAll”合并数据帧。
有没有一种方法可以不使用unionAll
就从dir1_2和dir2_1读取拼花地板文件,或者有没有什么奇特的方法可以使用unionAll
感谢SQLContext的方法和DataFrameReader的方法都采用了多个路径。因此,这两项工作中的任何一项:
df=sqlContext.parquetFile('/dir1/dir1_2','/dir2/dir2_1'))
或
df=sqlContext.read.parquet('/dir1/dir1_2','/dir2/dir2_1'))
有点晚了,但我在搜索时发现了这个,它可能会帮助其他人
您还可以尝试将参数列表解压缩到spark.read.parquet()
如果要将几个BLOB传递到path参数中,这很方便:
basePath='s3://bucket/'
paths=['s3://bucket/partition_value1=*/partition_value2=2017-04-*',
's3://bucket/partition_value1=*/partition_value2=2017-05-*'
]
df=spark.read.option("basePath",basePath).parquet(*paths)
这很酷,因为您不需要列出basePath中的所有文件,而且仍然可以进行分区推断。只需接受John Conley的答案,对其稍加修饰,并提供完整的代码(在Jupyter PySpark中使用),因为我发现他的答案非常有用
from hdfs import InsecureClient
client = InsecureClient('http://localhost:50070')
import posixpath as psp
fpaths = [
psp.join("hdfs://localhost:9000" + dpath, fname)
for dpath, _, fnames in client.walk('/eta/myHdfsPath')
for fname in fnames
]
# At this point fpaths contains all hdfs files
parquetFile = sqlContext.read.parquet(*fpaths)
import pandas
pdf = parquetFile.toPandas()
# display the contents nicely formatted.
pdf
对于ORC
spark.read.orc("/dir1/*","/dir2/*")
spark进入dir1/和dir2/文件夹并加载所有ORC文件
对于拼花地板
spark.read.parquet("/dir1/*","/dir2/*")
如果您有
文件列表
,您可以执行以下操作:
files = ['file1', 'file2',...]
df = spark.read.parquet(*files)
当我刚刚使用这段代码时,它正在搜索/home/directory中的目录,您能发布整个语法吗?@N00b当我尝试这段代码时,它给我一个错误,即加载只需要4个参数,但我有24个文件的路径。。是否有覆盖此选项的选项。我尽量不做多个加载和联合,这就是为什么我想用加载把多个文件放在一个DFM完美地为我工作@EB,您是否将其保存为列表,然后将其作为表达式运行
(*路径)
?这些都不适用于我。它会发现“可疑路径”,然后给我一长串java内容。嗨,我的任务与读取多个EJB文件类似,但这里提供的代码不起作用:(你找到解决方案了吗?
files = ['file1', 'file2',...]
df = spark.read.parquet(*files)