Hive之——Avro Hive SerDe

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

转载请注明出处:https://blog.csdn.net/l1028386804/article/details/88623683

使用表属性信息定义Avro Schema

如下语句通过制定AvroSerDe、AvroContainerInputFormat和AvroContaionerOutputFormat创建一个Avro表。Avro具有自身的模式定义语言。这个模式定义语言可以使用属性avro.schema.literal以字符串的形式存储在表的属性信息中。模式中指定了3个字段:
int类型的number、string类型的firstname和string类型的lastname

create table doctors
row format 
serde 'org.apache.hadoop.hive.serde2.avro.AvroSerDe'
stored as
inputformat 'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat'
outputformat 'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat'
tblproperties('avro.schema.literal'='{
"namespace":"testing.hive.avro.serde",
"name":"doctors",
"type":"record",
"fields":[
{
"name":"number",
"type":"int",
"doc":"Order of playing the role"
},
{
"name":"first_name",
"type":"string",
"doc":"first name of actor playing role"
},
{
"name":"last_name",
"type":"string",
"doc":"last name of actor playing role"
}
]
}');

当使用describe命令执行查看时,Hive就会显示这3个字段的名称和类型

hive> describe doctors;
OK
number                  int                     Order of playing the role
first_name              string                  first name of actor playing role
last_name               string                  last name of actor playing role
Time taken: 0.085 seconds, Fetched: 3 row(s)

从指定的URL中定义Schema

可以以URL的方式提供模式。可以是HDFS中某个文件的路径或者指向HTTP服务器的一个链接。通常可以在表属性中设置avro.schema.url的值,同时不设置avro.schema.literal的值。
模式可以是HDFS中的一个文件:

tblproperties('avro.schema.url'='hdfs://binghe:9000/path/to.schema')

模式同样可以存储在HTTP服务器上

tblproperties('avro.schema.url'='http://site.com/path/to.schema')

进化的模式

进化的模式是值随着时间而发生改变的模式。Avro允许字段是null。也允许如果数据文件中没有定义字段的话就返回指定的缺省值。
例如:如果Avro模式发生了改变,并增加了一个字段,那么在default字段中会提供一个值,如果没有找到这个新增列就返回这个值:

{
"name":"extra_fields",
"type":"string",
"doc":"an extra field not in the original file",
"default":"fishfingers and custard"
}

猜你喜欢

转载自blog.csdn.net/l1028386804/article/details/88623683