【hive】How to use Elephant Bird with Hive

转载 :

https://github.com/kevinweil/elephant-bird/wiki/How-to-use-Elephant-Bird-with-Hive

Overview

Let's quickly remind ourselves how Hive reads records so we understand how Elephant-Bird fits into the picture.

Hive data is organized as follows:

  • Databases contain Tables
  • Tables contain Partitions
  • Partitions have a StorageDescriptor

When reading data from a partition, Hive uses the storage descriptor to determine which InputFormat and SerDe to use. Each partition has its own storage descriptor, so you can change file and/or serialization formats at any time, and your existing partitions are still usable.

Hive creates an instance of the InputFormat for the partition, and uses it to read bytes from disk, splitting the input stream of bytes into individual records-worth of bytes.

Hive creates an instance of the SerDe, and initializes it with properties you provided when creating the table (serialization.class & serialization.format). Each record's worth of bytes from the input format is given to the SerDe, which converts it to the correct object type.

Elephant-Bird typically provides functionality of both input format and serde, reading bytes from disk, turning into a record's worth of bytes, and deserializing the bytes into an object of the correct type. To integrate with Hive in the least-intrusive way, Elephant-Bird was updated to include an input format that returns the raw record bytes instead of internally deserializing the record.

An alternative implementation of Elephant-Bird support for Hive could be done at the SerDe layer, providing a SerDe that expects an already deserialized record. An issue with this approach is the HiveMetaStore does not store properties for input formats – only serde's. Adding metastore support for input format properties is certainly possible, but is a larger change for little benefit. For this reason we chose to add raw bytes support to Elephant-Bird so it "just works" with Hive.

Example create table statement

You can create Hive tables as follows for your data stored by Elephant-Bird, and partitions you add to the tables will be readable.

create external table elephantbird_test
  -- no need to specify a schema - it will be discovered at runtime
  -- (myint int, myString string, underscore_int int)
  partitioned by (dt string)
    -- hive provides a thrift deserializer
    row format serde "org.apache.hadoop.hive.serde2.thrift.ThriftDeserializer"
    with serdeproperties (
      -- full name of our thrift struct class
      "serialization.class"="org.apache.hadoop.hive.serde2.thrift.test.IntString",
      -- use the binary protocol
      "serialization.format"="org.apache.thrift.protocol.TBinaryProtocol")
  stored as
    -- elephant-bird provides an input format for use with hive
    inputformat "com.twitter.elephantbird.mapred.input.DeprecatedRawMultiInputFormat"
    -- placeholder as we will not be writing to this table
    outputformat "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat";

Reading Protocol Buffers

Elephant-Bird provides ProtobufDeserializer for reading records stored as serialized protocol buffers. This deserializer expectsBytesWritable records, making it suitable for use with a variety of input formats, such as DeprecatedRawMultiInputFormat, SequenceFile, and other file formats that store binary values.

create table users
  row format serde "com.twitter.elephantbird.hive.serde.ProtobufDeserializer"
  with serdeproperties (
    "serialization.class"="com.example.proto.gen.Storage$User")
  stored as
    inputformat "com.twitter.elephantbird.mapred.input.DeprecatedRawMultiInputFormat";

Dynamic Schemas

Don't we need to store the write-time schema? Won't things break horribly if we change the schema?

A benefit to storing thrift structs is there are well-known rules for evolving structs - never remove fields, never reuse field IDs, and add new fields as optional. If playing by those rules, bytes stored to disk can be restored to thrift structs based on the current schema, marking optional fields as null, or using default values as the case may be.

Hive Configuration

The following hive-site.xml property must be set for Elephant-Bird to work correctly.

<property>
  <name>hive.input.format</name>
  <value>org.apache.hadoop.hive.ql.io.HiveInputFormat</value>
  <description>Hive uses a wrapping input format so each partition can be handled differently.
    The default CombineHiveInputFormat does not actually ask input formats for their splits,
    instead looks at the files themselves and attempts to optimize based on file size and
    location. We use HiveInputFormat to disable this optimization as we want our loaders
    to report their splits.
  </description>
</property>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值