1.問題描述javascript
使用Impala JDBC向Kudu表中插入中文字符,插入的中文字符串亂碼,中文字符串被截斷。java
2.問題復現sql
測試環境:shell
1.使用ImpalaJDBC代碼進行測試,測試代碼測試
static String 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; ResultSet rs = null; PreparedStatement ps = null; try { Class.forName(JDBC_DRIVER); con = DriverManager.getConnection(CONNECTION_URL); String insertsql = "insertinto my_first_table values(46, '測試中文字符')"; ps = con.prepareStatement(insertsql); ps.execute(); ps.close(); ps = con.prepareStatement("select* from my_first_table order by id asc"); rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getLong(1) + "\t" +rs.getString(2)); } } catch (Exception e) { e.printStackTrace(); } finally { try { // 關閉rs、ps和con rs.close(); ps.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2.向Kudu表中分別插入測試數據,如「測試」,「測試中文」,「測試中文字符」3d
String insertsql = "insert into my_first_table values(44, '測試')"; String insertsql = "insert into my_first_table values(45, '測試中文')"; String insertsql = "insert into my_first_table values(46, '測試中文字符')";
以下是按測試順序插入的數據code
經過以上操做重現問題。blog
3.解決方法ip
修改程序中插入語句,將插入字符串的單引號修改成雙引號字符串
String insertsql = "insert into my_first_table values(51, \"測試中文字符\")";
String insertsql = "insert into my_first_table values(52, \"測試中文\")";
String insertsql = "insert into my_first_table values(53, \"測試\")";
修改後從新向Kudu中插入測試數據:「測試中文字符」,「測試中文」,「測試」
使用Hue查詢顯示以下:
中文字符串插入Kudu顯示正常。
4.備註
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] >
醉酒鞭名馬,少年多浮誇! 嶺南浣溪沙,嘔吐酒肆下!摯友不願放,數據玩的花!