读写parquet格式文件的几种方式

摘要

本文将介绍常用parquet文件读写的几种方式
1.用spark的hadoopFile api读取hive中的parquet格式
2.用sparkSql读写hive中的parquet
3.用新旧MapReduce读写parquet格式文件

读parquet文件

首先创建hive表,数据用tab分隔

create table test(name string,age int) 
 row format delimited
 fields terminated by '\t';

加载数据
load data local inpath '/home/work/test/ddd.txt' into table test;
数据样例格式:

hive> select * from test limit 5; 
OK
leo 27
jim 38
leo 15
jack    22
jay 7
Time taken: 0.101 seconds, Fetched: 5 row(s)

创建parquet格式表
create table test_parquet(name string,age int) stored as parquet
查看表结构

hive> show create table test_parquet;
OK
CREATE TABLE `test_parquet`(
  `name` string, 
  `age` int)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'
STORED AS INPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
LOCATION
  'hdfs://localhost:9000/user/hive/warehouse/test_parquet'
TBLPROPERTIES (
  'transient_lastDdlTime'='1495038003')

可以看到数据的inputFormat是MapredParquetInputFormat,之后我们将用这个类来解析数据文件
往parquet格式表中插入数据
insert into table test_parquet select * from test;


a.用spark中hadoopFile api解析hive中parquet格式文件

如果是用spark-shell中方式读取文件一定要将hive-exec-0.14.0.jar加入到启动命令行中(MapredParquetInputFormat在这个jar中),还有就是要指定序列化的类,启动命令行如下:

spark-shell --master spark://xiaobin:7077 --jars /home/xiaobin/soft/apache-hive-0.14.0-bin/lib/hive-exec-0.14.0.jar
   --conf spark.serializer=org.apache.spark.serializer.KryoSerializer

具体读取代码如下

scala> import org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat
import org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat
 
scala> import org.apache.hadoop.io.{ArrayWritable, NullWritable, Text}
import org.apache.hadoop.io.{ArrayWritable, NullWritable, Text}
 
scala>     val file =sc.hadoopFile("hdfs://localhost:9000/user/hive/warehouse/test_parquet/000000_0",
     |       classOf[MapredParquetInputFormat],classOf[Void],classOf[ArrayWritable])
file: org.apache.spark.rdd.RDD[(Void, org.apache.hadoop.io.ArrayWritable)] =
      hdfs://localhost:9000/user/hive/warehouse/test_parquet/000000_0 HadoopRDD[0] at hadoopFile at <console>:29
 
scala> file.take(10).foreach{case(k,v)=>
     |       val writables = v.get()
     |       val name = writables(0)
     |       val age = writables(1)
     |       println(writables.length+"    "+name+"   "+age)
     |     }

用MapredParquetInputFormat解析hive中parquet格式文件,每行数据将会解析成一个key和value,这里的key是空值,value是一个ArrayWritable,value的长度和表的列个数一样,value各个元素对应hive表中行各个字段的值

b.用spark DataFrame 解析parquet文件

val conf = new SparkConf().setAppName("test").setMaster("local")
val sc = new SparkContext(conf)
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
val parquet: DataFrame =
     sqlContext.read.parquet("hdfs://192.168.1.115:9000/user/hive/warehouse/test_parquet")
parquet.printSchema()
parquet.select(parquet("name"), parquet("age") + 1).show()
 
root
 |-- name: string (nullable = true)
 |-- age: integer (nullable = true)
 
+----+---------+
|name|(age + 1)|
+----+---------+
| leo|       28|
| jim|       39|
| leo|       16|
|jack|       23|
| jay|        8|
| jim|       38|
|jack|       37|
| jay|       12|

c.用hivesql直接读取hive表

在local模式下没有测试成功,打包用spark-submit测试,代码如下

val conf = new SparkConf().setAppName("test")
val sc = new SparkContext(conf)
val hiveContext = new HiveContext(sc)
val sql: DataFrame = hiveContext.sql("select * from test_parquet limit 10")
sql.take(10).foreach(println)
 
[leo,27]                                                                        
[jim,38]
[leo,15]
[jack,22]
[jay,7]
[jim,37]
[jack,36]
[jay,11]
[leo,35]
[leo,33]

提交任务命令行
spark-submit --class quickspark.QuickSpark02 --master spark://192.168.1.115:7077 sparkcore-1.0-SNAPSHOT.jar

写parquet文件

a.用spark写parquet文件

val conf = new SparkConf().setAppName("test").setMaster("local")
val sc = new SparkContext(conf)
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
 
//    读取文件生成RDD
val file = sc.textFile("hdfs://192.168.1.115:9000/test/user.txt")
 
 //定义parquet的schema,数据字段和数据类型需要和hive表中的字段和数据类型相同,否则hive表无法解析
val schema = (new StructType)
      .add("name", StringType, true)
      .add("age", IntegerType, false)
 
val rowRDD = file.map(_.split("\t")).map(p => Row(p(0), Integer.valueOf(p(1).trim)))
//    将RDD装换成DataFrame
val peopleDataFrame = sqlContext.createDataFrame(rowRDD, schema)
peopleDataFrame.registerTempTable("people")
    peopleDataFrame.write.parquet("hdfs://192.168.1.115:9000/user/hive/warehouse/test_parquet/")

用hivesql读取用spark DataFrame生成的parquet文件

hive> select * from test_parquet limit 10;
OK
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
leo 27
jim 38
leo 15
jack    22
jay 7
jim 37
jack    36
jay 11
leo 35
leo 33

b.用MapReduce写parquet文件

用MR读写parquet文件,刚开始打算使用hive中指定的org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat这个类,但是这个类的getRecordWriter方法没实现,直接抛出异常

@Override
public RecordWriter<Void, ArrayWritable> getRecordWriter(
    final FileSystem ignored,
    final JobConf job,
    final String name,
    final Progressable progress
    ) throws IOException {
  throw new RuntimeException("Should never be used");
}

所以使用官方提供的parquet解析方式,github地址:https://github.com/apache/parquet-mr/,导入依赖

            <dependency>
                <groupId>org.apache.parquet</groupId>
                <artifactId>parquet-common</artifactId>
                <version>1.8.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.parquet</groupId>
                <artifactId>parquet-encoding</artifactId>
                <version>1.8.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.parquet</groupId>
                <artifactId>parquet-column</artifactId>
                <version>1.8.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.parquet</groupId>
                <artifactId>parquet-hadoop</artifactId>
                <version>1.8.1</version>
            </dependency>

Parquet读写有新旧两个版本,主要是新旧MR api之分,我们用新旧老版本的MR实现下parquet文件的读写

旧版本如下

package com.fan.hadoop.parquet;
 
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.parquet.hadoop.example.GroupWriteSupport;
import org.apache.parquet.example.data.Group;
import org.apache.parquet.example.data.simple.SimpleGroupFactory;
import org.apache.parquet.hadoop.mapred.DeprecatedParquetOutputFormat;
import org.apache.parquet.schema.MessageTypeParser;
/**
 * Created by fanlegefan.com on 17-7-17.
 */
public class ParquetMR {
 
    public static class Map extends MapReduceBase implements
            Mapper<LongWritable, Text, Text, IntWritable> {
 
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();
 
        public void map(LongWritable key, Text value,
                        OutputCollector<Text, IntWritable> output,
                            Reporter reporter) throws IOException {
            String line = value.toString();
            StringTokenizer tokenizer = new StringTokenizer(line);
            while (tokenizer.hasMoreTokens()) {
                word.set(tokenizer.nextToken());
                output.collect(word, one);
            }
        }
    }
 
    public static class Reduce extends MapReduceBase implements
            Reducer<Text, IntWritable, Void, Group> {
        private SimpleGroupFactory factory;
        public void reduce(Text key, Iterator<IntWritable> values,
                           OutputCollector<Void, Group> output,
                           Reporter reporter) throws IOException {
            int sum = 0;
            while (values.hasNext()) {
                sum += values.next().get();
            }
 
            Group group = factory.newGroup()
                    .append("name",  key.toString())
                    .append("age", sum);
            output.collect(null,group);
        }
 
        @Override
        public void configure(JobConf job) {
            factory = new SimpleGroupFactory(GroupWriteSupport.getSchema(job));
        }
    }
 
    public static void main(String[] args) throws Exception {
        JobConf conf = new JobConf(ParquetMR.class);
        conf.setJobName("wordcount");
 
        String in = "hdfs://localhost:9000/test/wordcount.txt";
        String out = "hdfs://localhost:9000/test/wd";
 
 
        String writeSchema = "message example {\n" +
                "required binary name;\n" +
                "required int32 age;\n" +
                "}";
 
        conf.setMapOutputKeyClass(Text.class);
        conf.setMapOutputValueClass(IntWritable.class);
 
        conf.setOutputKeyClass(NullWritable.class);
        conf.setOutputValueClass(Group.class);
 
        conf.setMapperClass(Map.class);
        conf.setReducerClass(Reduce.class);
 
        conf.setInputFormat(TextInputFormat.class);
        conf.setOutputFormat(DeprecatedParquetOutputFormat.class);
 
        FileInputFormat.setInputPaths(conf, new Path(in));
        DeprecatedParquetOutputFormat.setWriteSupportClass(conf, GroupWriteSupport.class);
        GroupWriteSupport.setSchema(MessageTypeParser.parseMessageType(writeSchema), conf);
 
        DeprecatedParquetOutputFormat.setOutputPath(conf, new Path(out));
 
        JobClient.runJob(conf);
    }
 
}

生成的文件:

hadoop dfs -ls /test/wd
Found 2 items
-rw-r--r--   3 work supergroup          0 2017-07-18 17:41 /test/wd/_SUCCESS
-rw-r--r--   3 work supergroup        392 2017-07-18 17:41 /test/wd/part-00000-r-00000.parquet

将生成的文件复制到hive表test_parquet的路径下:

hadoop dfs -cp /test/wd/part-00000-r-00000.parquet /user/work/warehouse/test_parquet/

测试hive表读取parquet文件

hive> select * from test_parquet limit 10;
OK
action  2
hadoop  2
hello   3
in  2
presto  1
spark   1
world   1
Time taken: 0.056 seconds, Fetched: 7 row(s)

新版本如下

新版本的MR读写Parquet和老版本有点区别,schema必须用在conf中设置,其他的区别不大
conf.set("parquet.example.schema",writeSchema);
还是贴下完整的代码

package com.fan.hadoop.parquet;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.parquet.example.data.Group;
import org.apache.parquet.example.data.simple.SimpleGroupFactory;
import org.apache.parquet.hadoop.ParquetOutputFormat;
import org.apache.parquet.hadoop.example.GroupWriteSupport;
import java.io.IOException;
import java.util.StringTokenizer;
 
/**
 * Created by fanglegefan.com on 17-7-18.
 */
public class ParquetNewMR {
 
    public static class WordCountMap extends
            Mapper<LongWritable, Text, Text, IntWritable> {
 
        private final IntWritable one = new IntWritable(1);
        private Text word = new Text();
 
        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String line = value.toString();
            StringTokenizer token = new StringTokenizer(line);
            while (token.hasMoreTokens()) {
                word.set(token.nextToken());
                context.write(word, one);
            }
        }
    }
 
    public static class WordCountReduce extends
            Reducer<Text, IntWritable, Void, Group> {
        private SimpleGroupFactory factory;
 
        public void reduce(Text key, Iterable<IntWritable> values,
                           Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            Group group = factory.newGroup()
                    .append("name",  key.toString())
                    .append("age", sum);
            context.write(null,group);
        }
 
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            super.setup(context);
            factory = new SimpleGroupFactory(GroupWriteSupport.getSchema(context.getConfiguration()));
 
        }
    }
 
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String writeSchema = "message example {\n" +
                "required binary name;\n" +
                "required int32 age;\n" +
                "}";
        conf.set("parquet.example.schema",writeSchema);
 
        Job job = new Job(conf);
        job.setJarByClass(ParquetNewMR.class);
        job.setJobName("parquet");
 
        String in = "hdfs://localhost:9000/test/wordcount.txt";
        String out = "hdfs://localhost:9000/test/wd1";
 
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
 
        job.setOutputValueClass(Group.class);
 
        job.setMapperClass(WordCountMap.class);
        job.setReducerClass(WordCountReduce.class);
 
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(ParquetOutputFormat.class);
 
        FileInputFormat.addInputPath(job, new Path(in));
        ParquetOutputFormat.setOutputPath(job, new Path(out));
        ParquetOutputFormat.setWriteSupportClass(job, GroupWriteSupport.class);
 
        job.waitForCompletion(true);
    }
}

查看生成的文件

hadoop dfs -ls /user/work/warehouse/test_parquet
Found 4 items
-rw-r--r--   1 work work          0 2017-07-18 18:27 /user/work/warehouse/test_parquet/_SUCCESS
-rw-r--r--   1 work work        129 2017-07-18 18:27 /user/work/warehouse/test_parquet/_common_metadata
-rw-r--r--   1 work work        275 2017-07-18 18:27 /user/work/warehouse/test_parquet/_metadata
-rw-r--r--   1 work work        392 2017-07-18 18:27 /user/work/warehouse/test_parquet/part-r-00000.parquet

将生成的文件复制到hive表test_parquet的路径下:

hadoop dfs -cp /test/wd/part-00000-r-00000.parquet /user/work/warehouse/test_parquet/

测试hive

hive> select name,age from test_parquet limit 10;
OK
action  2
hadoop  2
hello   3
in  2
presto  1
spark   1
world   1
Time taken: 0.036 seconds, Fetched: 7 row(s)

用mapreduce读parquet文件

package com.fan.hadoop.parquet;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.parquet.example.data.Group;
import org.apache.parquet.hadoop.ParquetInputFormat;
import org.apache.parquet.hadoop.api.DelegatingReadSupport;
import org.apache.parquet.hadoop.api.InitContext;
import org.apache.parquet.hadoop.api.ReadSupport;
import org.apache.parquet.hadoop.example.GroupReadSupport;
 
import java.io.IOException;
import java.util.*;
 
/**
 * Created by fanglegefan.com on 17-7-18.
 */
public class ParquetNewMRReader {
 
    public static class WordCountMap1 extends
            Mapper<Void, Group, LongWritable, Text> {
 
        protected void map(Void key, Group value,
                           Mapper<Void, Group, LongWritable, Text>.Context context)
                throws IOException, InterruptedException {
 
            String name = value.getString("name",0);
            int  age = value.getInteger("age",0);
 
            context.write(new LongWritable(age),
                    new Text(name));
        }
    }
 
    public static class WordCountReduce1 extends
            Reducer<LongWritable, Text, LongWritable, Text> {
 
        public void reduce(LongWritable key, Iterable<Text> values,
                           Context context) throws IOException, InterruptedException {
            Iterator<Text> iterator = values.iterator();
            while(iterator.hasNext()){
                context.write(key,iterator.next());
            }
        }
 
    }
 
    public static final class MyReadSupport extends DelegatingReadSupport<Group> {
        public MyReadSupport() {
            super(new GroupReadSupport());
        }
 
        @Override
        public org.apache.parquet.hadoop.api.ReadSupport.ReadContext init(InitContext context) {
            return super.init(context);
        }
    }
 
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String readSchema = "message example {\n" +
                "required binary name;\n" +
                "required int32 age;\n" +
                "}";
        conf.set(ReadSupport.PARQUET_READ_SCHEMA, readSchema);
 
        Job job = new Job(conf);
        job.setJarByClass(ParquetNewMRReader.class);
        job.setJobName("parquet");
 
        String in = "hdfs://localhost:9000/test/wd1";
        String  out = "hdfs://localhost:9000/test/wd2";
 
 
        job.setMapperClass(WordCountMap1.class);
        job.setReducerClass(WordCountReduce1.class);
 
        job.setInputFormatClass(ParquetInputFormat.class);
        ParquetInputFormat.setReadSupportClass(job, MyReadSupport.class);
        ParquetInputFormat.addInputPath(job, new Path(in));
 
        job.setOutputFormatClass(TextOutputFormat.class);
        FileOutputFormat.setOutputPath(job, new Path(out));
 
        job.waitForCompletion(true);
    }
}

查看生成的文件

hadoop dfs -cat /test/wd2/part-r-00000
1       world
1       spark
1       presto
2       in
2       hadoop
2       action
3       hello
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343

推荐阅读更多精彩内容