java使用impala存放多条sql_impala jdbc驱动执行impala sql的一个坑(不支持多行sql)...

架构使用spark streaming 消费kafka的数据,并通过impala来插入到kudu中,但是通过对比发现落地到kudu表中的数据比kafka消息数要少,通过后台日志发现,偶发性的出现java.sql.SQLException: [Simba][ImpalaJDBCDriver](500051) ERROR processing query/statement. Error Code: 0, SQL state: TStatus(statusCode:ERROR_STATUS, sqlState:HY000, errorMessage:AnalysisException: Syntax error in line 1

原因是调用过程中使用了数据库连接池,会合并多行sql执行,实现如下:

importcom.alibaba.druid.pool.DruidDataSource;importcom.alibaba.druid.pool.DruidDataSourceFactory;importcom.alibaba.druid.pool.DruidPooledConnection;importmons.logging.Log;importmons.logging.LogFactory;importjava.sql.SQLException;importjava.util.Properties;

public classImapalConnPool {private static Log logger = LogFactory.getLog(ImapalConnPool.class);private static ImapalConnPool imapalConnPool = null;private static DruidDataSource druidDataSource = null;static{

Properties properties= newProperties();

properties.setProperty("driverClassName","com.cloudera.impala.jdbc41.Driver");

properties.setProperty("url","jdbc:impala://127.0.0.1:21050");

properties.setProperty("username","");

properties.setProperty("password","");

properties.setProperty("initialSize","50");

properties.setProperty("maxActive","100");

properties.setProperty("maxWait","60000");

properties.setProperty("timeBetweenEvictionRunsMillis","60000");

properties.setProperty("minEvictableIdleTimeMillis","300000");

properties.setProperty("validationQuery","SELECT 1");

properties.setProperty("testWhileIdle","true");

properties.setProperty("testOnBorrow","false");

properties.setProperty("testOnReturn","false");

properties.setProperty("poolPreparedStatements","false");//当该值大于0时,启用pool,poolPreparedStatements为true

properties.setProperty("maxPoolPreparedStatementPerConnectionSize","-1");try{

druidDataSource= (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); //DruidDataSrouce工厂模式

} catch(Exception e) {

logger.error(e);

}

}public staticImapalConnPool getInstance(){if (null ==imapalConnPool){synchronized(ImapalConnPool.class) {if(null ==imapalConnPool) {

imapalConnPool= newImapalConnPool();

}

}

}returnimapalConnPool;

}public DruidPooledConnection getConnection() throwsSQLException {returndruidDataSource.getConnection();

}

}

调整上述参数poolPreparedStatements和maxPoolPreparedStatementPerConnectionSize任然不能解决同一个connection合并多行sql的问题

后去掉连接池,改为jdbc直连问题解决。有解决过上面数据库连接池问题的麻烦告知我一下

Connection connectionn = null;

Statement statement= null;try{

Class.forName("com.cloudera.impala.jdbc41.Driver");

connectionn= DriverManager.getConnection("jdbc:impala://127.0.0.1:21050");

statement=connectionn.createStatement();for(String item : execSql) {

statement.execute(item);

}

processResult= true;

}catch(Exception ex){

logger.error(this,ex);

}finally{if(null !=statement){try{

statement.close();

}catch(Exception ex){

logger.error(this,ex);

}

}if(null !=connectionn) {try{

connectionn.close();

}catch(Exception ex){

logger.error(this,ex);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值