使用 CSV Serde 和 Hive create table 将所有字段类型转换为字符串

Using CSV Serde with Hive create table converts all field types to string

如果我创建一个 table 并指定一个 CSVSerde,那么所有字段都将转换为字符串类型。

hive> create table foo(a int, b double, c string) row format serde 'com.bizo.hive.serde.csv.CSVSerde' stored as textfile; OK Time taken: 0.22 seconds hive> describe foo; OK a string from deserializer b string from deserializer c string from deserializer Time taken: 0.063 seconds, Fetched: 3 row(s) Serde 来自 https://github.com/ogrodnek/csv-serde

如果我从这个页面 https://cwiki.apache.org/confluence/display/Hive/CSV+Serde 尝试 serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde' 我看到了同样的事情。所有字段都更改为字符串类型。

Hive 版本 1.2.1 Hadoop 版本 2.7.0 java 版本“1.7.0_80”

是的,com.bizo.hive.serde.csv.CSVSerde 只创建字符串。这就是它的构建方式以及它将始终有效的方式。没有更改它的选项。我认为这很可能适用于您的大多数变量。话虽这么说我会。

Use A SELECT statement using a regex-based column specification, 可以在 0.13.0 之前的 Hive 版本中使用,或者在 0.13.0 及更高版本中使用,如果配置 属性 hive.support.quoted.identifiers 设置为 none。这意味着您可以快速构建一个新的 table,将您需要的少数变量的类型更改为双精度或整数。

set hive.support.quoted.identifiers=none;

drop table if       exists database.table_name;
create table if not exists database.table_name as
select `(a|b|c)?+.+`
    , cast(a as double) as a
    , cast(b as double) as b
    , cast(c as double) as c
    from database.some_table

;

您可以使用此方法仅触摸需要更改的变量并最小化查询长度。您可以在 table 之上创建视图以这种方式进行查询。或者你可以创建一个外部 table 并删除旧的 table;