hive mysql编码问题_解决 hive 建表注释中文乱码问题

有时在 hive 中创建表之后,在查看表结构(describe TABLE_NAME)或者建表语句(show create table TABLE_NAME)时会遇到注释中的中文乱码的问题,比如:

# 建表

hive> create external table if not exists user_visit_date (

user_id bigint comment '用户ID'

) comment '每日访问用户'

partitioned by (p_day date comment '分区日期')

stored as parquet;

# 查看表结构

hive> describe user_visit_date;

OK

user_id bigint ??ID

p_day date ????

# Partition Information

# col_name data_type comment

p_day date ????

一、原因分析

很显然这是因为 hive 的元数据的编码问题,hive 的 metastore 通常存储在 mysql 中(在配置hive metastore时会在 mysql 中建库,比如笔者创建的是名为 hive 的库),我们先来看看该库的 coding 格式:

mysql> show create database hive;

+----------+-----------------------------------------------------------------+

| Database | Create Database |

+----------+-----------------------------------------------------------------+

| hive | CREATE DATABASE `hive` /*!40100 DEFAULT CHARACTER SET latin1 */ |

+----------+-----------------------------------------------------------------+

1 row in set (0.00 sec)

可以看出 hive 库的 coding 格式为 latin1,而非 utf-8。为了让中文注释能够正常显示,我们需要对部分 coding 格式设置为 utf-8。

二、解决方案

# 首先进入 hive 库

mysql > use hive;

# 然后修改部分配置

mysql > alter table COLUMNS_V2 modify column COMMENT varchar(256) character set utf8;

Query OK, 91 rows affected (0.05 sec)

Records: 91 Duplicates: 0 Warnings: 0

mysql> alter table TABLE_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;

Query OK, 35 rows affected (0.05 sec)

Records: 35 Duplicates: 0 Warnings: 0

mysql> alter table PARTITION_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;

Query OK, 0 rows affected (0.04 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table PARTITION_KEYS modify column PKEY_COMMENT varchar(4000) character set utf8;

Query OK, 1 row affected (0.04 sec)

Records: 1 Duplicates: 0 Warnings: 0

mysql> alter table INDEX_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;

Query OK, 0 rows affected (0.04 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql > commit;

三、查验结果

在 hive shell 中重新建表,并查看表结构来验证设置是否生效。

# drop 旧表

hive> drop table user_visit_date;

OK

Time taken: 0.658 seconds

# 建新表

hive> create external table if not exists user_visit_date (

> user_id bigint comment '用户ID'

> ) comment '每日访问用户'

> partitioned by (p_day date comment '分区日期')

> stored as parquet;

OK

Time taken: 0.083 seconds

# 查看表结构

hive> describe user_visit_date;

OK

user_id bigint 用户ID

p_day date 分区日期

# Partition Information

# col_name data_type comment

p_day date 分区日期

Time taken: 0.133 seconds, Fetched: 7 row(s)

可以看出修改配置后,新创建的表的表结构中的中文注释能够正常显示。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值