Hive之——Avro Hive SerDe

转载请注明出处: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"
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰 河

可以吃鸡腿么?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值