使用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)+ "	" +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] > 
原文地址:https://www.cnblogs.com/shaosks/p/9829622.html