0021-insert Chinese characters into the Kudu table using JDBC-Secrets of cast
Warm Tip: to see the high-definition no-code picture, please open it with your mobile phone and click the picture to enlarge.
1. Problem description
Use Impala JDBC to insert Chinese characters into the Kudu table, the inserted Chinese string is garbled, and the Chinese string is truncated.
After the previous document used sql splicing method to insert Chinese string garbled solution, this document describes the problem of inserting Chinese string garbled using jdbc's PreparedStatement method.
two。 Problem recurrence
Test environment:
CDH5.12.0Kudu1.4.0ImpalaJDBC41_2.5.35
1. Test with ImpalaJDBC code, test code
StaticString JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver"; staticString 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 (1JI 81); ps.setString (2, "testing Chinese characters"); 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 {/ / close rs, ps and con rs.close (); ps.close (); con.close ();} catch (SQLException e) {/ / TODOAuto-generated catch block e.printStackTrace ();}
two。 Insert test data into the Kudu table, such as "test", "test Chinese", "test Chinese characters"
String sql2 = "insert into my_first_table values"; ps = con.prepareStatement (sql2); ps.setInt (1,73); ps.setString (2, "Test"); ps.execute (); ps.close (); ps = con.prepareStatement (sql2); ps.setInt (1,74); ps.setString (2, "Test Chinese"); ps.execute (); ps.close (); ps = con.prepareStatement (sql2); ps.setInt (1,75) Ps.setString (2, "Test Chinese characters"); ps.execute (); ps.close ()
The query result via Hue is as follows:
All Chinese characters are garbled, some of them are garbled, and the string is truncated.
3. Solution method
Modify the insert statement in the program and convert the insert string column to the String type using the cast function
String sql2 = "insert into my_first_table values (?, cast (? as string)"; ps = con.prepareStatement (sql2); ps.setInt (1,60); ps.setString (2, "testing Chinese characters"); ps.execute (); ps.close (); ps = con.prepareStatement (sql2); ps.setInt (1,61); ps.setString (2, "testing Chinese"); ps.execute (); ps.close (); ps = con.prepareStatement (sql2) Ps.setInt (1,62); ps.setString (2, "Test"); ps.execute (); ps.close ()
Re-insert test data into Kudu after modification: "Test Chinese characters", "Test Chinese", "Test"
The query using Hue is displayed as follows:
The Kudu display of Chinese string insertion is normal.
Drunken whips are famous horses, and teenagers are so pompous! Lingnan Huan Xisha, under the vomiting liquor store! The best friend refuses to let go, the flower of data play!
Warm Tip: to see the high-definition no-code picture, please open it with your mobile phone and click the picture to enlarge.
Welcome to follow Hadoop practice, the first time, share more Hadoop practical information, like please follow and share.
Original article, welcome to reprint, reprint please indicate: reproduced from the official account of Wechat Hadoop