hive注释中文乱码问题

在hive中创建表 , 表中的字段注释是中文注释

create external table movie(
userID int comment '用户ID',
movieID int comment '电影ID',
rating int comment '电影评分',
timestamped bigint comment '评分时间戳',
movieName string comment '电影名字', 
movieType string comment '电影类型', 
sex string comment '性别', 
age int comment '年龄', 
occupation string comment '职业', 
zipcode string comment '邮政编码'
) comment '影评三表合一' 
row format delimited fields terminated by ","
location '/hive/movie';

建表查看表基本信息

hive> desc movie;
OK
userid                  int                     ??ID
movieid                 int                     ??ID
rating                  int                     ????
timestamped             bigint                  ?????
moviename               string                  ????
movietype               string                  ????
sex                     string                  ??
age                     int                     ??
occupation              string                  ??
zipcode                 string                  ????
Time taken: 0.057 seconds, Fetched: 10 row(s)

这是因为在MySQL中的元数据出现乱码

因为我们知道 metastore 支持数据库级别,表级别的字符集是 latin1

那么我们只需要把相应注释的地方的字符集由 latin1 改成 utf-8,就可以了。用到注释的就三个地方,表、分区、视图。如下修改分为两个步骤:

进入数据库 Metastore 中执行以下 5 条 SQL 语句

(1)修改表字段注解和表注解

alter table COLUMNS_V2 modify column COMMENT varchar(256) character set utf8;
alter table TABLE_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;

(2)修改分区字段注解

alter table PARTITION_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
alter table PARTITION_KEYS modify column PKEY_COMMENT varchar(4000) character set utf8;

(3)修改索引注解

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

修改 metastore 的连接 URL vi hive-site.xml文件

<property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://linux01:3306/hive?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</value>
    <description>JDBC connect string for a JDBC metastore</description>
</property>

重新创建表验证

扫描二维码关注公众号,回复: 13588541 查看本文章
hive> desc formatted movie;
OK
# col_name              data_type               comment
 
userid                  int                     用户ID
movieid                 int                     电影ID
rating                  int                     电影评分
timestamped             bigint                  评分时间戳
moviename               string                  电影名字
movietype               string                  电影类型
sex                     string                  性别
age                     int                     年龄
occupation              string                  职业
zipcode                 string                  邮政编码

猜你喜欢

转载自blog.csdn.net/JAVA_LuZiMaKei/article/details/117919983