简体   繁体   中英

spark-submit: Passing java properties file gives FileNotFoundException

I have a spark job that I am running in yarn cluster mode. I need to pass my application specific java properties file. I am doing this java FileInputStream .
But I am facing FileNotFoundException for /home/aiman/config/my-file.properties

My java properties file is: /home/aiman/config/my-file.properties

mongo_server=my.mongo.server
mongo_port=1530
mongo_user=mongoadmin
mongo_password=readONLYpass
mongo_db=testdb

My spark-submit looks like:

spark-submit --master yarn --deploy-mode cluster --class mongo.MongoRead --jars /home/aiman/ojdbc-7.jar /home/aiman/app/jars/MongoRead-0.1-jar-with-dependencies.jar /home/aiman/config/my-file.properties

Code snippet:

public static void main(final String[] args) throws Exception
    {       
        if(args.length<1){
            System.out.println("Please provide properties file path");
            System.exit(1);
        }

        System.out.println("Mongo Import Begins...");

        Properties prop = new Properties();
        InputStream in = null;
        try{
            in = new FileInputStream(args[0]);
            prop.load(in);
        }
        catch(Exception e){
            e.printStackTrace();
            System.exit(1);
        }

        /*Reading the properties*/
        String mongoServer = prop.getProperty("mongo_server");
        String mongoPort = prop.getProperty("mongo_port");
        String mongoUser = prop.getProperty("mongo_user");
        ....
        ...

        String mongoAddress = "mongodb://"+mongoUser+":"+mongoPassword+"@"+mongoServer+":"+mongoPort+"/"+mongoDb+"."+tableNm;

        SparkSession spark = SparkSession.builder()
            .appName("MongoSparkRecordReader")
            .config("spark.mongodb.input.uri", mongoAddress)
            .config("spark.mongodb.output.uri", mongoAddress)
            .getOrCreate();

        JavaSparkContext jsc = new JavaSparkContext(spark.sparkContext());
        ...
        ..

}

Please suggest what is going wrong. Am I reading the properties file in a wrong manner, that it isn't finding the file?

Running this in --deploy-mode client resolves the issue. The Driver is getting launched where the properties file is present. So file is getting read and not getting any Exception.

You should pass the local file via the "--file /home/aiman/config/my-file.properties" spark-submit's argument, which will distribute the file to the containers launched by YARN that will find the "my-file.properties" in their JVM's classpath. You should then be able to read it as a JVM's local resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM