使用Spark读写SequenceFile时,有时需要根据一定的规则动态生成文件的输出路径,且需要将某一类的数据都输出到同一个Path下,这时就需要使用MultipleSequenceFileOutputFormat
SoftWare | Version |
---|---|
Scala | 2.11.11 |
Spark | 2.4.0 |
MultipleSequenceFileOutputFormat 源码预览
1 | // 继承自MultipleOutputFormat |
SequenceFileOutputFormat 源码预览
1 | public RecordWriter<K, V> getRecordWriter(FileSystem ignored, JobConf job, String name, Progressable progress) throws IOException { |
MultipleOutputFormat 源码预览
主要是对这个类中方法的重写
1 | // 内容略,就是上面的getRecordWriter,获取writer |
自定义OutputFormat
1 | // 自定义一个输出类,继承自MultipleSequenceFileOutputFormat |
自定义OutputFormat 使用
1 | // 第一步是读取数据源,可以是其他,我这里是读取一个txt文件然后根据一定规则转换成sequenceFile |
spark-file-split案例
【此Project的作用是】:
- 数据以竖线分隔的文本,且与avro schema字段对应
- 通过文本内容和avro schema,判断数据时间event_time是否符合入参标准
- 从文本内容中解析出表名、数据时间,生成分区日期、自定义输出路径
- 使得同一张表、同一日期分区的数据,落入同一个path
- 之后可以将输出目录的数据直接load到hive sequence表中
转载请注明出处:https://github.com/imperio-wxm