Hive 学习笔记 (二)

DESCRIBE EXTENDED view_logs;## 存储格式

Hive从两个维度对表的存储进行管理: row format 和 file format。 row format 指 行和一行中的字段如何存储。对于Hive来说,row format的定义由SerDe定义。

查询表时,SerDe 把文件中字节形式的数据行反序列化为Hive内部操作数据行时所使用的对象形式。执行Insert 或者CTAS 时,表的SerDe会把Hive的数据行内部表示形式序列化成字节形式并写到输出文件中。

file format 指一行中字段容器的格式。最简单的格式是纯文本文件。

文本存储

如果在创建表时没有用ROW FORMAT 或者 STORED AS,那么Hive 默认使用 文本格式存储。每行存储一个数据行,默认的行内分隔符不是制表符,是Ctrl-A(ASCII 码为1)。集合元素默认的分隔符未字符Ctrl-B,用于分割 ARRAY或STRUCT或MAP的键值对中的元素。
Hive默认使用8级分隔符。

CREATE TABLE ...
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\001'
COLLECTION ITEMS TERMINATED BY '\002'
MAP KEYS TERMINATED BY '\003'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;

Hive内部使用一个名未LazySimpleSerDe的SerDe来处理这种分割符。

代码片段:

 public static SerDeParameters initSerdeParams(Configuration job,
      Properties tbl, String serdeName) throws SerDeException {
    SerDeParameters serdeParams = new SerDeParameters();
    // Read the separators: We use 8 levels of separators by default,
    // and 24 if SERIALIZATION_EXTEND_NESTING_LEVELS is set to true
    // The levels possible are the set of control chars that we can use as
    // special delimiters, ie they should absent in the data or escaped.
    // To increase this level further, we need to stop relying
    // on single control chars delimiters

    serdeParams.separators = new byte[8];
    serdeParams.separators[0] = getByte(tbl.getProperty(serdeConstants.FIELD_DELIM,
        tbl.getProperty(serdeConstants.SERIALIZATION_FORMAT)), DefaultSeparators[0]);
    serdeParams.separators[1] = getByte(tbl
        .getProperty(serdeConstants.COLLECTION_DELIM), DefaultSeparators[1]);
    serdeParams.separators[2] = getByte(
        tbl.getProperty(serdeConstants.MAPKEY_DELIM), DefaultSeparators[2]);
    String extendedNesting =
        tbl.getProperty(SERIALIZATION_EXTEND_NESTING_LEVELS);
    if(extendedNesting == null || !extendedNesting.equalsIgnoreCase("true")){
      //use the default smaller set of separators for backward compatibility
      for (int i = 3; i < serdeParams.separators.length; i++) {
   
        serdeParams
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值