<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Cluster environment variables not visible to Java Dataset.foreachPartition / mapPartitions in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/cluster-environment-variables-not-visible-to-java-dataset/m-p/174659#M56888</link>
    <description>&lt;P&gt;We run a Spark JAR task (Java) on a standard access mode cluster. Credentials come from cluster environment&lt;BR /&gt;variables set as secret references in the cluster's Environment variables field, e.g.&lt;BR /&gt;MY_PASSWORD={{secrets/&amp;lt;scope&amp;gt;/&amp;lt;key&amp;gt;}}.&lt;BR /&gt;&lt;BR /&gt;The variables are visible to the JAR task's driver code, but not to code running inside Dataset.foreachPartition /&lt;BR /&gt;mapPartitions on the workers (we only check presence, never values):&lt;BR /&gt;&lt;BR /&gt;- DBR 16.4 LTS (Scala 2.12), standard access mode: inside foreachPartition the code runs as process "3@sandbox" and sees only 27 variables (DBR_VERSION, DB_*, HOME, LANG, PATH, PWD, SHLVL, payload_format, stageId, taskAttemptId). None of the cluster-configured variables are present, including a plain non-secret one. On the driver all are present.&lt;BR /&gt;&lt;BR /&gt;- DBR 19 (Scala 2.13, Spark 4.2), standard access mode: driver sees MY_PASSWORD, mapPartitions code does not. The cluster edit page says "Databricks Runtime 19 and above restricts environment variables. The following&lt;BR /&gt;variables will be available in user code, but not loaded into the Spark session", listing our variables.&lt;BR /&gt;&lt;BR /&gt;The standard compute limitations page says variables outside the predefined set "remain available to your user code,including UDFs", which does not match what we see.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Minimal reproduction (Java JAR task, MY_PASSWORD set as a cluster environment variable):&lt;BR /&gt;&lt;BR /&gt;SparkSession spark = SparkSession.builder().getOrCreate();&lt;BR /&gt;boolean onDriver = System.getenv("MY_PASSWORD") != null; // true&lt;BR /&gt;List&amp;lt;String&amp;gt; onWorkers = spark.range(0, 2, 1, 2)&lt;BR /&gt;.mapPartitions((MapPartitionsFunction&amp;lt;Long, String&amp;gt;) rows -&amp;gt; Collections.singletonList(&lt;BR /&gt;String.valueOf(System.getenv("MY_PASSWORD") != null)).iterator(), Encoders.STRING())&lt;BR /&gt;.collectAsList();&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Questions:&lt;BR /&gt;1. Is this expected for Scala/Java Dataset.foreachPartition / mapPartitions code on standard access mode?&lt;BR /&gt;2. Is there a supported way to make specific cluster environment variables visible to that code?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Oct 2026 16:37:54 GMT</pubDate>
    <dc:creator>capoorhimanshu</dc:creator>
    <dc:date>2026-10-26T16:37:54Z</dc:date>
    <item>
      <title>Cluster environment variables not visible to Java Dataset.foreachPartition / mapPartitions</title>
      <link>https://community.databricks.com/t5/data-engineering/cluster-environment-variables-not-visible-to-java-dataset/m-p/174659#M56888</link>
      <description>&lt;P&gt;We run a Spark JAR task (Java) on a standard access mode cluster. Credentials come from cluster environment&lt;BR /&gt;variables set as secret references in the cluster's Environment variables field, e.g.&lt;BR /&gt;MY_PASSWORD={{secrets/&amp;lt;scope&amp;gt;/&amp;lt;key&amp;gt;}}.&lt;BR /&gt;&lt;BR /&gt;The variables are visible to the JAR task's driver code, but not to code running inside Dataset.foreachPartition /&lt;BR /&gt;mapPartitions on the workers (we only check presence, never values):&lt;BR /&gt;&lt;BR /&gt;- DBR 16.4 LTS (Scala 2.12), standard access mode: inside foreachPartition the code runs as process "3@sandbox" and sees only 27 variables (DBR_VERSION, DB_*, HOME, LANG, PATH, PWD, SHLVL, payload_format, stageId, taskAttemptId). None of the cluster-configured variables are present, including a plain non-secret one. On the driver all are present.&lt;BR /&gt;&lt;BR /&gt;- DBR 19 (Scala 2.13, Spark 4.2), standard access mode: driver sees MY_PASSWORD, mapPartitions code does not. The cluster edit page says "Databricks Runtime 19 and above restricts environment variables. The following&lt;BR /&gt;variables will be available in user code, but not loaded into the Spark session", listing our variables.&lt;BR /&gt;&lt;BR /&gt;The standard compute limitations page says variables outside the predefined set "remain available to your user code,including UDFs", which does not match what we see.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Minimal reproduction (Java JAR task, MY_PASSWORD set as a cluster environment variable):&lt;BR /&gt;&lt;BR /&gt;SparkSession spark = SparkSession.builder().getOrCreate();&lt;BR /&gt;boolean onDriver = System.getenv("MY_PASSWORD") != null; // true&lt;BR /&gt;List&amp;lt;String&amp;gt; onWorkers = spark.range(0, 2, 1, 2)&lt;BR /&gt;.mapPartitions((MapPartitionsFunction&amp;lt;Long, String&amp;gt;) rows -&amp;gt; Collections.singletonList(&lt;BR /&gt;String.valueOf(System.getenv("MY_PASSWORD") != null)).iterator(), Encoders.STRING())&lt;BR /&gt;.collectAsList();&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Questions:&lt;BR /&gt;1. Is this expected for Scala/Java Dataset.foreachPartition / mapPartitions code on standard access mode?&lt;BR /&gt;2. Is there a supported way to make specific cluster environment variables visible to that code?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2026 16:37:54 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cluster-environment-variables-not-visible-to-java-dataset/m-p/174659#M56888</guid>
      <dc:creator>capoorhimanshu</dc:creator>
      <dc:date>2026-10-26T16:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Cluster environment variables not visible to Java Dataset.foreachPartition / mapPartitions</title>
      <link>https://community.databricks.com/t5/data-engineering/cluster-environment-variables-not-visible-to-java-dataset/m-p/174673#M56889</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/268183"&gt;@capoorhimanshu&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On standard access mode this is expected. Scala/Java closures (foreachPartition, mapPartitions, UDFs) don't run in the executor JVM where the cluster env vars are set. They run in an isolated per-user sandbox process with a fixed, minimal environment. That's the &lt;A href="mailto:3@sandbox" target="_blank"&gt;3@sandbox&lt;/A&gt;&amp;nbsp;process and the 27 vars you're seeing. On DBR 19 spark.executorEnv.* is also restricted on standard, so you can't push the vars that way. The supported fix is to read the value on the driver and pass it into the closure, or to run the job on dedicated access mode.&lt;/P&gt;&lt;P&gt;Option 1: capture on the driver. This works on standard mode, DBR 16.4 and 19:&lt;/P&gt;&lt;P&gt;final String password = System.getenv("MY_PASSWORD");&amp;nbsp; &amp;nbsp;// driver: present&lt;/P&gt;&lt;P&gt;ds.foreachPartition((ForeachPartitionFunction&amp;lt;Row&amp;gt;) rows -&amp;gt; {&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; // 'password' is serialized with the task closure&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; try (Connection c = DriverManager.getConnection(url, user, password)) {&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while (rows.hasNext()) { /* ... */ }&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; }&lt;/P&gt;&lt;P&gt;});&lt;/P&gt;&lt;P&gt;Keep it in a local final variable (not a static field), or the sandbox will just see null. It travels inside the serialized task, so don't log or toString() the closure. Spark's redaction won't touch it there.&lt;/P&gt;&lt;P&gt;Option 2: dedicated (single user) access mode. Executors there run your code in their own JVM, which gets the cluster's environment variables, so System.getenv("MY_PASSWORD") works inside mapPartitions unchanged. This is the cleaner choice if the job runs as a service principal anyway.&lt;/P&gt;&lt;P&gt;About the docs line saying non-predefined variables "remain available to your user code, including UDFs": your repro shows that doesn't hold for JVM closures on standard. It's worth sending docs feedback on &lt;A href="https://docs.databricks.com/aws/en/compute/standard-limitations" target="_blank"&gt;https://docs.databricks.com/aws/en/compute/standard-limitations&lt;/A&gt;&amp;nbsp;so it gets clarified.&lt;/P&gt;&lt;P&gt;If this helps you and solves your problem , kindly accept them as answer so that community can be benefited&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2026 18:59:26 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cluster-environment-variables-not-visible-to-java-dataset/m-p/174673#M56889</guid>
      <dc:creator>SumeshKashyap</dc:creator>
      <dc:date>2026-10-26T18:59:26Z</dc:date>
    </item>
  </channel>
</rss>

