使用JDBC向Kudu表插入中文数据乱码(转载)
参考:https://cloud.tencent.com/developer/article/1077763
问题描述
使用Impala JDBC向Kudu表中插入中文字符,插入的中文字符串乱码,中文字符串被截断。
此文档描述使用jdbc的PreparedStatement方式插入中文字符串乱码问题。
1、使用ImpalaJDBC代码进行测试,测试代码
staticString JDBC_DRIVER ="com.cloudera.impala.jdbc41.Driver";
static String CONNECTION_URL ="jdbc:impala://ip-172-31-10-118:21050/default"; public static void main(String[] args) {
Connection con = null;
ResultSetrs = null;
PreparedStatementps = null; try {
Class.forName(JDBC_DRIVER);
con =DriverManager.getConnection(CONNECTION_URL); Stringsql2 = "insert into my_first_table values(?, ?)";
ps =con.prepareStatement(sql2);
ps.setInt(1,81);
ps.setString(2,"测试中文字符");
ps.execute();
ps.close(); ps =con.prepareStatement("select * from my_first_table order byid asc");
rs = ps.executeQuery();
while (rs.next()){
System.out.println(rs.getLong(1)+ "\t" +rs.getString(2));
} } catch (Exceptione) {
e.printStackTrace();
} finally{
try {// 关闭rs、ps和con
rs.close();
ps.close();
con.close();
} catch(SQLException e) {
// TODOAuto-generated catch block
e.printStackTrace();
} }
}
2、向Kudu表中分别插入测试数据,如“测试”,“测试中文”,“测试中文字符”
String sql2 = "insert into my_first_table values(?, ?)";
ps = con.prepareStatement(sql2);
ps.setInt(1, 73);
ps.setString(2, "测试");
ps.execute();
ps.close(); ps = con.prepareStatement(sql2);
ps.setInt(1, 74);
ps.setString(2, "测试中文");
ps.execute();
ps.close(); ps = con.prepareStatement(sql2);
ps.setInt(1, 75);
ps.setString(2, "测试中文字符");
ps.execute();
ps.close();
通过查询kudu数据库如下:

中文字符全部乱码,部分乱码,字符串被截断问题重现。
3、解决方法
修改程序中插入语句,将插入字符串列使用cast函数转成String类型
String sql2 = "insert into my_first_table values(?, cast(? as string))";
ps = con.prepareStatement(sql2);
ps.setInt(1, 60);
ps.setString(2, "测试中文字符");
ps.execute();
ps.close(); ps = con.prepareStatement(sql2);
ps.setInt(1, 61);
ps.setString(2, "测试中文");
ps.execute();
ps.close(); ps = con.prepareStatement(sql2);
ps.setInt(1, 62);
ps.setString(2, "测试");
ps.execute();
ps.close();
修改后重新向Kudu中插入测试数据:“测试中文字符”,“测试中文”,“测试”
使用Hue查询显示如下:

中文字符串插入Kudu显示正常。
另一种情况
1、向Kudu表中分别插入测试数据,如“测试”,“测试中文”,“测试中文字符”


2、解决办法
修改程序中插入语句,将插入字符串的单引号修改为双引号

修改后重新向Kudu中插入测试数据:“测试中文字符”,“测试中文”,“测试”
使用Hue查询显示如下:

备注
1.使用Cloudera官网最新的JDBC驱动,插入中文字符时也有上述问题
下载地址:https://downloads.cloudera.com/connectors/impala_jdbc_2.5.38.1058.zip
2.通过Impala-shell插入中文字符串正常
[172.31.10.118:21000] > insert into my_first_table values(66,'插入中文字符');
Modified 1 row(s), 0 row error(s) in 0.11s
[172.31.10.118:21000] > select * from my_first_table where id=66;
+----+--------------+
| id | name |
+----+--------------+
| 66 | 插入中文字符 |
+----+--------------+
Fetched 1 row(s) in 0.21s
[172.31.10.118:21000] > [172.31.10.118:21000] > insert into my_first_table values(77, "测试中文字符");
Modified 1 row(s), 0 row error(s) in 0.11s
[172.31.10.118:21000] > select * from my_first_table where id=77;
+----+--------------+
| id | name |
+----+--------------+
| 77 | 测试中文字符 |
+----+--------------+
Fetched 1 row(s) in 0.18s
[172.31.10.118:21000] >
使用JDBC向Kudu表插入中文数据乱码(转载)的更多相关文章
- mariadb插入中文数据乱码解决过程
基本情况: 系统:centos 7 mariadb安装方式:yum 乱码解决过程: 查看当前数据库编码(登录数据库后) # show variables like 'character%'; (上图为 ...
- MySQL 插入 中文数据乱码解决
问题描述: 1.在命令行中进行插入,没有问题.但是显示存在部分乱码 2.在JDBC中插入成功.中文是直接以“??”形式显示. 通过Navicat客户端查看 与在网页中看到的一一致,说明读取没有问题,问 ...
- java web 向数据库插入中文数据乱码问题
一.先检查下是 页面返回数据时已经乱码了,还是在插入数据库的时候乱的码. 二.页面返回乱码: 1. Web.XML 文件配置 <!-- 配置编码过滤器 --> <filter&g ...
- Hibernate向MySQL插入中文数据--乱码解决
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/exam?useUnicod ...
- Oracle插入中文数据乱码 设置服务器编码和客户端编码一致
- mysql不能插入中文数据
上次遇到的是向mysql插入中文数据,中文数据乱码了.这次直接就不能插入中文数据了!!!! 参考博文:http://blog.csdn.net/generalyy0/article/details/7 ...
- MySQL插入中文数据出现?号
原文转载自:https://blog.csdn.net/LynneZoe/article/details/79174119 运行环境:win10 mysql版本:Mysql5.6 做一个项目的时候,向 ...
- 解决Python向MySQL数据库插入中文数据时出现乱码
解决Python向MySQL数据库插入中文数据时出现乱码 先在MySQL命令行中输入如下语句查看结果: 只要character_set_client character_set_database ch ...
- mysql插入中文数据变成问号怎么处理
插入中文数据变成问号,一般都是因为字符集没有设置成utf8的原因 1.修改字符集: ALTER TABLE 表名 MODIFY 列名 类型(50) CHARACTER SET "utf8&q ...
随机推荐
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...
- 使用 IntraWeb (27) - 基本控件之 TIWAudio、TIWMPEG、TIWFlash、TIWSilverlight、TIWSilverlightVideo、TIWApplet、TIWQuickTime、TIWActiveX
TIWAudio 所在单元及继承链: IWCompAudio.TIWAudio 主要成员: property AudioFile: TIWFileReference // property Focus ...
- SystemParametersinfo的用法(一)
SystemParametersinfo的用法(一) 函数功能:该函数查询或设置系统级参数.该函数也可以在设置参数中更新用户配置文件. 函数原型:B00L SystemParametersinfo(U ...
- ListBox使用
一.什么是ListBox? ListBox 是一个显示项集合的控件.一次可以显示 ListBox 中的多个项. ListBox继承自ItemsControl,可以使用Items或者ItemsSourc ...
- Android 中的概念大集合
Intent: An Intent is an object that provides runtime binding between separate components (such as tw ...
- 【k8s】基础概念 + 工作原理
工作原理: 原理图 工作原理描述: 1>用户通过kubectl或者API server的REST API接口,提交需要运行的docker容器(创建pod请求): 2>api server将 ...
- WordPress基础:常用分类列表wp_list_categories
函数:wp_list_categories($args) 作用:列出某个分类下的分类项目 用法: <ul> <?php $args= array( 'depth'=>1, 'o ...
- Oracle 备份、恢复单表或多表数据步骤
Oracle 备份.恢复单表或多表数据步骤,适用于 Oracle 8.9.10. *备份单表或多表数据: exp user/password@server file=filefullpa ...
- Quartz:不要重复造轮子,一款企业级任务调度框架。
背景 第一次遇到定时执行某些任务的需求时,很多朋友可能设计了一个小类库,这个类图提高了一个接口,然后由调度器调度所有注册的接口类型,我就是其中之一,随着接触的开源项目越来越多,我的某些开发习惯受到了影 ...
- git push.default设置
转自:http://blog.csdn.net/daijingxin/article/details/51326715 在进行一次空仓库的提交时,我遇到了这个警告 警告如下: warning: pus ...