SPARK 集群运行是读取配置文件的常见问题

SPARK 集群运行是读取配置文件的问题 

在实时计算时,遇到一个异常:

Caused by: java.lang.NullPointerException at com.szkingdom.kdap.api.compute.dao.DBDaoFactory.batchUpdate(DBDaoFactory.java:119) at com.szkingdom.kdap.dao.statisticresultstimecomsuming.saveresultstimecomsumingsrealtime(statisticresultstimecomsuming.java:260) at com.szkingdom.kdap.spark.computation.StreamingComputationToDB$$anonfun$timecomsumingnew$1$$anonfun$apply$11.apply(StreamingComputationToDB.scala:460) 

DBDaoFactory.java:119行之前的代码为: conn = JdbcUtil.getConn(); 

此函数通过配置文件获取对数据库的连接HANDLE,在实时计算时动态获取:

public static Connection getConn(){
    Connection con = tl.get();
    try {

        if(con==null){

            Class.forName(jdbc_driver);
            con = DriverManager.getConnection( jdbc_url,jdbc_username,jdbc_password);
            tl.set(con);
        }
        return tl.get();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
} 

其中的jdbcurl,jdbcusername,jdbcpassword 在程序 启动时从config.properties配置文件中获取 经分析引起数据库连接HANDLE(con)为空的原因是:在SPARK的WORK节点上通过程序启动传过来的参数在计算时实时获取不到,导致jdbcdriver取到的是空

解决方法(利用SPARK-SUBMIT 中的--FILES 参数 ): 1、在kdapsubmit.sh中增加 选项 --files /home/hadoop/spark/kdapcompute/config.properties 2、修改程序代码,当con 为空时,重新从config.properties 获取

public static Connection getConn(){
    //��֤ͬһ���߳�����ͬһ��connection
    Connection con = tl.get();
    try {
        if(con==null){
             prop.load(new FileInputStream("config.properties"));
            Class.forName(prop.getProperty("jdbc_driver"));
            con = DriverManager.getConnection(
                    prop.getProperty("jdbc_url"),prop.getProperty("jdbc_username"),prop.getProperty("jdbc_password"));
            tl.set(con);
        }
        return tl.get();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

这个问题折腾了几天,需要对SPARK的内部任务分配机制有所理解,先是拷贝配置文件到各台机,固定目录的办法,也能通,但没有--files 这个方法灵活



参考URL: http://blog.csdn.net/u012307002/article/details/53308937

你可能感兴趣的:(大数据)