关于Spark SQL外部表在实战中遇到的问题

笔者之前写过一篇关于Spark SQL外部表的比较全面的学习实验博客,参https://blog.csdn.net/u011817217/article/details/92403843 

本篇旨在描述和总结在实际工作中使用Spark SQL外部表遇到的一些问题以及相应的解决方案。

一、需求描述

外围系统通过文件接口的方式定期给一份数据文件,关于接口描述和数据文件示例如下:

1. 文件接口内容

2. 数据文件示例

1)Linux上查看

2)Windows上Notepad++查看

 

二、创建外部表并导入数据文件

1. 建表语句

create external table ci_interface_label_catelog(
  day_id              string,
  prvnce_id           string,
  category_id         bigint,
  category_parent_id  bigint,
  category_name       string,
  category_level      int,
  category_ord        int,
  category_path       string,
  category_path_desc  string,
  label_object        string,
  class_status        string
)
row format delimited fields terminated by '\u0005' 
lines terminated by '\n' 
stored as textfile location 'hdfs://streamcluster/hupeng/data/ci_interface';

2. 导入数据文件

通过hdfs dfs -put -f 命令将本地的数据文件上传到HDFS的/hupeng/data/ci_interface目录下。

hdfs dfs -put data_file.DAT /hupeng/data/ci_interface

三、问题及解决方法

1. 中文显示乱码

尝试指定表的编码格式:

ALTER TABLE ci_interface_label_catelog SET SERDEPROPERTIES ('serialization.encoding'='GBK');

效果如上图,虽然中文显示正常了,但是通过查看数据发现class_stauts字段值出现混乱的情况,和实际数据文件给定的内容不一致。

2. 部分字段值和数据文件不一致

出现这个原因是因为在进行了上面的操作,初步怀疑是不是数据文件编码的问题。

1)将数据文件编码转换成UTF-8格式:

iconv -c -f GBK -t UTF-8 LABEL_CATELOG.20190705.20190705.01.001.001.822.DAT -o export.txt

2)修改表的编码格式

ALTER TABLE ci_interface_label_catelog SET SERDEPROPERTIES ('serialization.encoding'='UTF-8');

3)hdfs dfs -put -f 

hdfs dfs -put -f export.txt /hupeng/data/ci_interface

一切都正常。

3. 验证创建表的默认编码是不是UTF-8

1)drop table ci_interface_label_catelog;

2)重新建表

3)将数据文件编码转换成UTF-8格式

4)hdfs dfs -put -f 

结果和2中一致,表明创建表的默认编码是UTF-8。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值