You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Kamalanathan Venkatesan <Ka...@in.ey.com> on 2018/03/22 09:18:29 UTC

Need config params while doing rdd.foreach or map

Hello All,

I have custom parameter say for example file name added to the conf of spark context example  SparkConf.set(INPUT_FILE_NAME, fileName).
I need this value inside foreach performed on an  RDD, but the when access spark context inside foreach, I receive spark context is null exception!

Code sample:

val conf = new SparkConf().setMaster(appConfig.envOrElseConfig("app.sparkconf.master"))
      .setAppName(appConfig.envOrElseConfig("app.appName"))
      .set("INPUT_FILE_NAME", fileName)

var sparkContext = new SparkContext(conf)

sparkContext.addJar(sparkContextParams.jarPath)

var sqlContext = new SQLContext(sparkContext)

var df = sqlContext.read.format("com.databricks.spark.csv")
     .option("header", "true")
      .load(<filePath>)

df.foreach( f=> {
       f.split(",")
       println(sparkContext.getConf.get("INPUT_FILE_NAME"))
});

The above sparkContext.getConf.get("INPUT_FILE_NAME") throws null pointer exception!

Thanks,
Kamal.

The information contained in this communication is intended solely for the use of the individual or entity to whom it is addressed and others authorized to receive it. It may contain confidential or legally privileged information. If you are not the intended recipient you are hereby notified that any disclosure, copying, distribution or taking any action in reliance on the contents of this information is strictly prohibited and may be unlawful. If you have received this communication in error, please notify us immediately by responding to this email and then delete it from your system. The firm is neither liable for the proper and complete transmission of the information contained in this communication nor for any delay in its receipt.

Re: Need config params while doing rdd.foreach or map

Posted by ayan guha <gu...@gmail.com>.
Spark context runs in driver whereas the func inside foreach runs in
executor. You can pass on the param in the func so it is available in
executor

On Thu, 22 Mar 2018 at 8:18 pm, Kamalanathan Venkatesan <
Kamalanathan.V@in.ey.com> wrote:

> Hello All,
>
>
>
> I have custom parameter say for example file name added to the conf of
> spark context example  SparkConf.set(INPUT_FILE_NAME, fileName).
>
> I need this value inside foreach performed on an  RDD, but the when access
> spark context inside foreach, I receive spark context is null exception!
>
>
>
> Code sample:
>
>
>
> *val* conf = *new* SparkConf().setMaster(appConfig.envOrElseConfig(
> "app.sparkconf.master"))
>
>       .setAppName(appConfig.envOrElseConfig("app.appName"))
>
>       .set(“INPUT_FILE_NAME”, fileName)
>
>
>
> var sparkContext = *new* SparkContext(conf)
>
>
>
> sparkContext.addJar(sparkContextParams.jarPath)
>
>
>
> var sqlContext = *new* SQLContext(sparkContext)
>
>
>
> *var* df = sqlContext.read.format("com.databricks.spark.csv")
>
>      .option("header", "true")
>
>       .load(<filePath>)
>
>
>
> df.foreach( f=> {
>
>        f.split(“,”)
>
> *       println(sparkContext.getConf.get(“INPUT_FILE_NAME”))*
>
> });
>
>
>
> The above *sparkContext.getConf.get(“INPUT_FILE_NAME”) throws null
> pointer exception!*
>
>
>
> Thanks,
>
> Kamal.
>
> The information contained in this communication is intended solely for the
> use of the individual or entity to whom it is addressed and others
> authorized to receive it. It may contain confidential or legally privileged
> information. If you are not the intended recipient you are hereby notified
> that any disclosure, copying, distribution or taking any action in reliance
> on the contents of this information is strictly prohibited and may be
> unlawful. If you have received this communication in error, please notify
> us immediately by responding to this email and then delete it from your
> system. The firm is neither liable for the proper and complete transmission
> of the information contained in this communication nor for any delay in its
> receipt.
>
-- 
Best Regards,
Ayan Guha