spark读取HDFS多个文件配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012036736/article/details/84998296

情况说明:数据以parquet文件形式保存在HDFS上,数据中的某一列包含了日期(例如:2017-12-12)属性,根据日期对数据分区存储,如下图所示: 

项目需求: 
在项目中想要读取某一个月的数据,肿么办? 
解决方法: 
spark中读取本地文件的方法如下:

sparkSession.read.parquet("hdfs://path")
1
方法一: 
要读取多个文件,文件的路径中有一段公共路径。这样,首先想到的方法就是通过通配符来读取多个文件,对应的代码如下:

sparkSession.read.parquet("hdfs://hzgc:9000/user/hive/warehouse/person_table/date=2017-12-*")

理想是美好的,但是现实是残酷的,代码运行结果报错: 

也就是说文件是查找到了,但是读取的方式是不对的,那怎么办呢?好在spark足够人性化,在后面给出了提示:

If provided paths are partition directories, please set "basePath" in the options of the data source to specify the root directory of the table. If there are multiple root directories, please load them separately and then union them.
1
什么意思呢?翻译过来就是:如果提供的路径是分区路径,那么请在数据源的option中设置“basePath”来单独指定表的根路径;如果根路径不同,那么就分别加载数据,然后采用union的方式加数据合并。

方法二: 
于是修改代码如下:

sparkSession.read.option("basePath", basePath).parquet(basePath + "date=2017-12-*")
1
运行结果没毛病,老铁!

方法三:

sparkSession.read.parquet("hdfs://hzgc:9000/user/hive/warehouse/person_table/date=2017-12-13")   .union(sparkSession.read.parquet("hdfs://hzgc:9000/user/hive/warehouse/person_table/date=2017-12-09"))  .union(sparkSession.read.parquet("hdfs://hzgc:9000/user/hive/warehouse/person_table/date=2017-12-07"))       .union(sparkSession.read.parquet("hdfs://hzgc:9000/user/hive/warehouse/person_table/date=2017-12-08"))
1
这样也没有问题,但是想想还是方法二更方便一些!

方法四:

sparkSession.read.parquet("url","url2",..."urlN")


spark 1.x可以采用如下方式:

val inputPath = List("hdfs://nameservice1/user/zhaoyulong/1627/UGM_GFM/part-00000","hdfs://nameservice1/user/zhaoyulong/1627/NGN_GP/part-00000")
  .mkString(",")
val stream = new SparkContext(conf).textFile(inputPath)

采用通配符可以提高效率:

Hadoop和spark读取多个文件通配符规则(正则表达式)joe,最近在公司需要计算手机信令数据 但是每次spark读取文件的时候都是把当天24小时从头到尾读取一遍 非常耗时,在一步操作中处理批量文件,这个要求很常见。举例来说,处理日志的MapReduce作业可能会分析一个月的文件,这些文件被包含在大量目录中。Hadoop有一个通配的操作,可以方便地使用通配符在一个表达式中核对多个文件,不需要列举每个文件和目录来指定输入如下图所示:

\

\

例如 我想读取hdfs://master:9000/population/unicom_phone/pekin/20150701/02

和hdfs://master:9000/population/unicom_phone/pekin/20150701/03的文件

也就是我想读20150701 下的02 和03文件 通过通配符可以写成如下:

hdfs://master:9000/population/unicom_phone/pekin/20150701/0[2-3]

再次执行 计算速度快了四倍。

猜你喜欢

转载自blog.csdn.net/u012036736/article/details/84998296