<?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 Re: 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/175353#M56992</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&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;DIV&gt;the page does not explicitly say environment variables can be injected into or accessed from inside foreachPartition using standard compute.&lt;/DIV&gt;</description>
    <pubDate>Mon, 02 Nov 2026 15:44:19 GMT</pubDate>
    <dc:creator>saurabh18cs</dc:creator>
    <dc:date>2026-11-02T15:44:19Z</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>
    <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/174717#M56897</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks for the response. Yes, that matches what I’m seeing and explains the sandbox&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;process.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I was wondering if there’s a way to configure the shared cluster so that environment variables can be passed through to these sandbox workers.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Capturing the value on the driver is something I wanted to avoid, since on a shared cluster it would mean the secret/password could end up in the serialized task. The functionality already works on dedicated clusters; I was looking to extend it to shared clusters as well.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2026 06:39:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cluster-environment-variables-not-visible-to-java-dataset/m-p/174717#M56897</guid>
      <dc:creator>capoorhimanshu</dc:creator>
      <dc:date>2026-10-27T06:39:57Z</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/175264#M56977</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/268183"&gt;@capoorhimanshu&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Your worry about the closure is fair. A secret that rides inside the serialized task is harder to control than one that never leaves a secret store.&lt;/P&gt;&lt;P&gt;As far as I can tell, there's no supported setting on standard access mode that pushes cluster environment variables into the sandbox where your Java closures run. The isolation is deliberate. On DBR 19, the docs also list spark.executorEnv.* among the Spark config properties you can't set on standard access mode, so that route is closed on purpose too. You're right that the "remain available to your user code, including UDFs" line on the limitations page doesn't match what you're seeing for JVM closures. It's worth sending docs feedback on that page so it gets clarified.&lt;/P&gt;&lt;P&gt;So the options are really about where the credential lives:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1. If it's a database password: use a JDBC Unity Catalog connection&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The connection stores the URL and credentials in Unity Catalog, and users only need USE CONNECTION. Your code never sees the password, so nothing ends up in a closure. It works on standard access mode on DBR 17.3 LTS and above (Public Preview through 18, GA from DBR 19).&lt;/P&gt;&lt;PRE&gt;CREATE CONNECTION my_pg_conn TYPE JDBC
ENVIRONMENT (java_dependencies '["/Volumes/main/jars/drivers/postgresql-42.7.4.jar"]')
OPTIONS (
  url 'jdbc:postgresql://db-host:5432/appdb',
  user '&amp;lt;db_user&amp;gt;',
  password '&amp;lt;db_password&amp;gt;',
  externalOptionsAllowList 'dbtable,query'
);

GRANT USE CONNECTION ON CONNECTION my_pg_conn TO `data_engineers`;
-- users of the connection also need to read the driver JAR
GRANT READ VOLUME ON VOLUME main.jars.drivers TO `data_engineers`;&lt;/PRE&gt;&lt;PRE&gt;ds.write()
  .format("jdbc")
  .option("databricks.connection", "my_pg_conn")
  .option("dbtable", "public.target_table")
  .mode("append")
  .save();&lt;/PRE&gt;&lt;P&gt;The catch is that it's a DataFrame read/write (append or overwrite), not custom per-row logic. Row-level UPDATE/DELETE through the connection isn't supported. If your foreachPartition is doing something like upserts, a common pattern is to append into a staging table on the database side and run the upsert there. The connection owner sets the password once. After that, users can't read it back or override the connection's options at query time.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2. If you need custom code inside the partitions: dedicated compute assigned to a group&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;You already found that dedicated works. Dedicated access mode can also be assigned to a group (Public Preview, DBR 15.4+), so several people or service principals can share one cluster and the executors still get the cluster's environment variables. Everything on that cluster runs with the group's permissions, not each person's, so the group needs READ on the secret scope and the table grants. That's usually fine for a team-owned job cluster, but it's worth knowing before you move interactive users onto it.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;3. Keep a narrow fallback&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;If neither fits, passing the value from the driver is still the only way into the sandbox. I'd at least keep the credential short-lived (for example, a token you fetch on the driver at job start, not a long-lived password) and limit who can read the job's logs and event logs.&lt;/P&gt;&lt;P&gt;Docs:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Standard compute limitations: &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;&lt;/LI&gt;&lt;LI&gt;JDBC connection: &lt;A href="https://docs.databricks.com/aws/en/connect/jdbc-connection" target="_blank"&gt;https://docs.databricks.com/aws/en/connect/jdbc-connection&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Dedicated compute group access: &lt;A href="https://docs.databricks.com/aws/en/compute/group-access" target="_blank"&gt;https://docs.databricks.com/aws/en/compute/group-access&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Sun, 01 Nov 2026 11:49:02 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cluster-environment-variables-not-visible-to-java-dataset/m-p/175264#M56977</guid>
      <dc:creator>Isaac_18</dc:creator>
      <dc:date>2026-11-01T11:49:02Z</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/175353#M56992</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&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;DIV&gt;the page does not explicitly say environment variables can be injected into or accessed from inside foreachPartition using standard compute.&lt;/DIV&gt;</description>
      <pubDate>Mon, 02 Nov 2026 15:44:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cluster-environment-variables-not-visible-to-java-dataset/m-p/175353#M56992</guid>
      <dc:creator>saurabh18cs</dc:creator>
      <dc:date>2026-11-02T15:44:19Z</dc:date>
    </item>
  </channel>
</rss>

