Dear teachers,
  I encountered a trouble when I use hive jdbc to write my data into hive when 
there is character '\' in my data. My source code is in the attachment.
  When I run the code, it exits with exceptions:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String 
index out of range: -1
at java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:824)
at java.lang.StringBuffer.deleteCharAt(StringBuffer.java:441)
at 
org.apache.hive.jdbc.HivePreparedStatement.updateSql(HivePreparedStatement.java:142)
at 
org.apache.hive.jdbc.HivePreparedStatement.execute(HivePreparedStatement.java:98)
at HiveJdbcClient.main(HiveJdbcClient.java:15)


  I'm confused why the method 
org.apache.hive.jdbc.HivePreparedStatement#getCharIndexFromSqlByParamLocation 
treats the character '\' specially. After all, I can execute the sql " insert 
into name_age (name,age) values ('Pe\ter', 30) " in the beeline without any 
problem. 


  In addition, my hive-jdbc version is 1.1.0.
  Could you help me?









 
import java.sql.*;

public class HiveJdbcClient {
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";

    public static void main(String[] args) throws SQLException {
        PreparedStatement preparedStatement = null;
        try {
            Class.forName(driverName);
            Connection con = 
DriverManager.getConnection("jdbc:hive2://127.0.0.1:10000/test_fb", "hdfs", "");
            String sqlTemplate = "insert into name_age (name,age) values (?,?)";
            preparedStatement = con.prepareStatement(sqlTemplate);
            preparedStatement.setString(1, "Pe\\ter");
            preparedStatement.setInt(2, 30);
            preparedStatement.execute();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        } finally {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        }
    }
}

Reply via email to