<?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: Cannot write data to BigQuery when using Databricks secret in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101477#M40683</link>
    <description>&lt;P&gt;The json and command looks good to me.&lt;/P&gt;
&lt;P&gt;Is it feasible for you to attach the full stacktrace, and/or check if this is applicable to your cluster setup as well?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.databricks.com/t5/community-platform-discussions/bigquery-in-notebook-failing-with-unity-catalog-enabled-cluster/td-p/59784" target="_blank"&gt;https://community.databricks.com/t5/community-platform-discussions/bigquery-in-notebook-failing-with-unity-catalog-enabled-cluster/td-p/59784&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 09 Dec 2024 14:34:21 GMT</pubDate>
    <dc:creator>VZLA</dc:creator>
    <dc:date>2024-12-09T14:34:21Z</dc:date>
    <item>
      <title>Cannot write data to BigQuery when using Databricks secret</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/100528#M40326</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I am following this&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://docs.databricks.com/en/connect/external-systems/bigquery.html#read-and-write-to-a-bigquery-table" target="_blank" rel="nofollow noopener noreferrer"&gt;guide&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;on writing data to the BigQuery table.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Right now, I have an error when I try to write data using&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://docs.databricks.com/en/security/secrets/example-secret-workflow.html#step-3-use-the-secrets-in-a-notebook" target="_blank" rel="nofollow noopener noreferrer"&gt;Databricks Secret&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;instead of the JSON credential file and setting the&amp;nbsp;&lt;/SPAN&gt;GOOGLE_APPLICATION_CREDENTIALS&lt;SPAN&gt;&amp;nbsp;environment variable.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;java.io.IOException: Error getting access token from metadata server at: http://169.x.x.x/computeMetadata/v1/instance/service-accounts/default/token&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is strange here is I can write/read the data using the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;GOOGLE_APPLICATION_CREDENTIALS&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;environment variable. I can also read the data using Databricks Secret. So I don't know why it is not work when I write the data with Databricks Secret.&lt;/P&gt;&lt;P&gt;Here is my code to read the Databricks Secret:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;import&lt;/SPAN&gt; base64

cred = dbutils.secrets.get(scope=&lt;SPAN class=""&gt;"bigquery-scope"&lt;/SPAN&gt;, key=&lt;SPAN class=""&gt;"secret-name"&lt;/SPAN&gt;).encode(&lt;SPAN class=""&gt;'ascii'&lt;/SPAN&gt;)
cred = base64.b64encode(cred)
cred = cred.decode(&lt;SPAN class=""&gt;'ascii'&lt;/SPAN&gt;)
spark.conf.&lt;SPAN class=""&gt;set&lt;/SPAN&gt;(&lt;SPAN class=""&gt;"credentials"&lt;/SPAN&gt;, cred)&lt;/PRE&gt;&lt;P&gt;Below is my code to read/write the data:&lt;/P&gt;&lt;DIV class=""&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;# Read data&lt;/SPAN&gt;
df = spark.read.&lt;SPAN class=""&gt;format&lt;/SPAN&gt;(&lt;SPAN class=""&gt;"bigquery"&lt;/SPAN&gt;)
    .option(&lt;SPAN class=""&gt;"parentProject"&lt;/SPAN&gt;, &amp;lt;parent-project-&lt;SPAN class=""&gt;id&lt;/SPAN&gt;&amp;gt;)
    .option(&lt;SPAN class=""&gt;"viewsEnabled"&lt;/SPAN&gt;,&lt;SPAN class=""&gt;"true"&lt;/SPAN&gt;)
    .option(&lt;SPAN class=""&gt;"table"&lt;/SPAN&gt;, &amp;lt;table-name&amp;gt;)
    .load()

&lt;SPAN class=""&gt;# Write data&lt;/SPAN&gt;
df.write.&lt;SPAN class=""&gt;format&lt;/SPAN&gt;(&lt;SPAN class=""&gt;"bigquery"&lt;/SPAN&gt;) \
  .mode(&lt;SPAN class=""&gt;"overwrite"&lt;/SPAN&gt;) \
  .option(&lt;SPAN class=""&gt;"temporaryGcsBucket"&lt;/SPAN&gt;, &amp;lt;bucket-name&amp;gt;) \
  .option(&lt;SPAN class=""&gt;"table"&lt;/SPAN&gt;, &amp;lt;table-name&amp;gt;) \
  .option(&lt;SPAN class=""&gt;"parentProject"&lt;/SPAN&gt;, &amp;lt;parent-project-&lt;SPAN class=""&gt;id&lt;/SPAN&gt;&amp;gt;) \
  .save()&lt;/PRE&gt;&lt;P&gt;Am I missing any configuration for writing the data to BigQuery with Databricks Secret?&lt;/P&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sun, 01 Dec 2024 13:41:32 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/100528#M40326</guid>
      <dc:creator>huytran</dc:creator>
      <dc:date>2024-12-01T13:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot write data to BigQuery when using Databricks secret</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101267#M40607</link>
      <description>&lt;P&gt;Hello, thank you for your question.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To address your issue, the problem likely arises because the BigQuery connector expects a JSON credentials file or an environment variable pointing to it. Using a Databricks Secret directly requires extra steps. Here's how you can resolve it:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Retrieve the JSON credentials from the Databricks Secret as a raw string, avoiding Base64 encoding. Ensure the JSON is passed exactly as it was stored in the secret.&lt;/LI&gt;
&lt;LI&gt;Dynamically create a temporary file in your environment to store the credentials. Use a method to write the JSON credentials to a file and set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of this file.&lt;/LI&gt;
&lt;LI&gt;Confirm that your Spark session has the required configurations set for BigQuery, including the temporary GCS bucket and parent project. Ensure the service account in your credentials has permissions for both the BigQuery table and the GCS bucket.&lt;/LI&gt;
&lt;LI&gt;Double-check that your temporary GCS bucket is properly configured and accessible by the service account, as write operations often rely on this intermediate storage.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;If these steps don't resolve the issue, feel free to share additional details for further assistance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2024 16:39:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101267#M40607</guid>
      <dc:creator>VZLA</dc:creator>
      <dc:date>2024-12-06T16:39:00Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot write data to BigQuery when using Databricks secret</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101466#M40680</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/34618"&gt;@VZLA&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to get the Databricks Secret, write to a temporary file, and set the temporary file path to&amp;nbsp;&lt;SPAN&gt;GOOGLE_APPLICATION_CREDENTIALS&lt;/SPAN&gt; with the code below. And yes, I have to encode the Secret value so I can write it to a temporary file.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;cred = dbutils.secrets.get("bigquery-cred", "project-id-name").encode('utf-8')
fd = os.open("/tmp/cred.json", os.O_RDWR|os.O_CREAT)
ret = os.write(fd, cred) 
os.close(fd)

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/tmp/cred.json'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Then I got this error:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Py4JJavaError: An error occurred while calling o677.save.
: com.google.cloud.spark.bigquery.repackaged.com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) Error in custom provider, java.io.UncheckedIOException: Failed to create default Credentials
  at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryCredentialsSupplier(BigQueryClientModule.java:46)
  while locating com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryCredentialsSupplier
    for the 3rd parameter of com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryClient(BigQueryClientModule.java:63)
  at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryClient(BigQueryClientModule.java:63)
  while locating com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClient&lt;/LI-CODE&gt;&lt;P&gt;I guess it doesn't get the value from the temporary file. Could you check if I use the correct code to write the file?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2024 12:41:04 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101466#M40680</guid>
      <dc:creator>huytran</dc:creator>
      <dc:date>2024-12-09T12:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot write data to BigQuery when using Databricks secret</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101472#M40681</link>
      <description>&lt;P&gt;Interesting, maybe there's a problem with the encoding, can you please give this a try?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;import os

# Retrieve the JSON credentials from Databricks Secrets
cred = dbutils.secrets.get("bigquery-cred", "project-id-name")

# Write the JSON to a temporary file
temp_cred_path = "/tmp/cred.json"
with open(temp_cred_path, "w") as temp_cred_file:
    temp_cred_file.write(cred)

# Set the environment variable to point to the JSON file
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = temp_cred_path

# Verify the environment variable and file content (for debugging)
print("GOOGLE_APPLICATION_CREDENTIALS:", os.environ['GOOGLE_APPLICATION_CREDENTIALS'])
with open(temp_cred_path, "r") as temp_cred_file:
    print(temp_cred_file.read())
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2024 13:18:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101472#M40681</guid>
      <dc:creator>VZLA</dc:creator>
      <dc:date>2024-12-09T13:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot write data to BigQuery when using Databricks secret</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101473#M40682</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/34618"&gt;@VZLA&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code returns below. The 2nd print command returns "[REDACTED]" and I think it is because we don't encode it.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;GOOGLE_APPLICATION_CREDENTIALS: /tmp/cred.json
[REDACTED]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;And I got the same error above:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Py4JJavaError: An error occurred while calling o679.save.
: com.google.cloud.spark.bigquery.repackaged.com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) Error in custom provider, java.io.UncheckedIOException: Failed to create default Credentials
  at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryCredentialsSupplier(BigQueryClientModule.java:46)
  while locating com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryCredentialsSupplier
    for the 3rd parameter of com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryClient(BigQueryClientModule.java:63)
  at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryClient(BigQueryClientModule.java:63)
  while locating com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClient&lt;/LI-CODE&gt;&lt;P&gt;More information, the way I create the Databricks Secret is I have the file.json value below:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;{
        "type": "service_account",
        "project_id": "xxxx-yyy",
        "private_key_id": "xxxxxxx",
        "private_key": "-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----\n",
        "client_email": "xxx@yyyy.iam.gserviceaccount.com",
        "client_id": "xxxxx",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxxxx%40xxxx.iam.gserviceaccount.com"
}&lt;/LI-CODE&gt;&lt;P&gt;Then I used this command to create the Secret:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;databricks secrets put-secret bigquery-cred project-id-name --string-value "$(cat file.json)"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2024 13:33:33 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101473#M40682</guid>
      <dc:creator>huytran</dc:creator>
      <dc:date>2024-12-09T13:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot write data to BigQuery when using Databricks secret</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101477#M40683</link>
      <description>&lt;P&gt;The json and command looks good to me.&lt;/P&gt;
&lt;P&gt;Is it feasible for you to attach the full stacktrace, and/or check if this is applicable to your cluster setup as well?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.databricks.com/t5/community-platform-discussions/bigquery-in-notebook-failing-with-unity-catalog-enabled-cluster/td-p/59784" target="_blank"&gt;https://community.databricks.com/t5/community-platform-discussions/bigquery-in-notebook-failing-with-unity-catalog-enabled-cluster/td-p/59784&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2024 14:34:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101477#M40683</guid>
      <dc:creator>VZLA</dc:creator>
      <dc:date>2024-12-09T14:34:21Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot write data to BigQuery when using Databricks secret</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101483#M40685</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/34618"&gt;@VZLA&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I checked my cluster setup and it is a shared cluster. Many people in my company can use it for their notebook.&lt;/P&gt;&lt;P&gt;And here is the full stackstrace:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;---------------------------------------------------------------------------
Py4JJavaError                             Traceback (most recent call last)
&amp;lt;command-846265064717828&amp;gt; in &amp;lt;module&amp;gt;
      1 # this exports to Optus Big Query -- note "overwrite" in method def above
----&amp;gt; 2 write_to_bigquery(table_name, bucket, dataset, project_id)

&amp;lt;command-846265064717827&amp;gt; in write_to_bigquery(tableName, tempBucket, df, project_id)
      1 def write_to_bigquery(tableName, tempBucket, df, project_id):
----&amp;gt; 2   df.write.format("bigquery") \
      3   .option("temporaryGcsBucket", tempBucket) \
      4   .option("parentProject", project_id) \
      5   .option("table", tableName) \

/databricks/spark/python/pyspark/sql/readwriter.py in save(self, path, format, mode, partitionBy, **options)
    736             self.format(format)
    737         if path is None:
--&amp;gt; 738             self._jwrite.save()
    739         else:
    740             self._jwrite.save(path)

/databricks/spark/python/lib/py4j-0.10.9.1-src.zip/py4j/java_gateway.py in __call__(self, *args)
   1302 
   1303         answer = self.gateway_client.send_command(command)
-&amp;gt; 1304         return_value = get_return_value(
   1305             answer, self.gateway_client, self.target_id, self.name)
   1306 

/databricks/spark/python/pyspark/sql/utils.py in deco(*a, **kw)
    115     def deco(*a, **kw):
    116         try:
--&amp;gt; 117             return f(*a, **kw)
    118         except py4j.protocol.Py4JJavaError as e:
    119             converted = convert_exception(e.java_exception)

/databricks/spark/python/lib/py4j-0.10.9.1-src.zip/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
    324             value = OUTPUT_CONVERTER[type](answer[2:], gateway_client)
    325             if answer[1] == REFERENCE_TYPE:
--&amp;gt; 326                 raise Py4JJavaError(
    327                     "An error occurred while calling {0}{1}{2}.\n".
    328                     format(target_id, ".", name), value)

Py4JJavaError: An error occurred while calling o679.save.
: com.google.cloud.spark.bigquery.repackaged.com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) Error in custom provider, java.io.UncheckedIOException: Failed to create default Credentials
  at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryCredentialsSupplier(BigQueryClientModule.java:46)
  while locating com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryCredentialsSupplier
    for the 3rd parameter of com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryClient(BigQueryClientModule.java:63)
  at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryClient(BigQueryClientModule.java:63)
  while locating com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClient

1 error
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1097)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1131)
	at com.google.cloud.spark.bigquery.BigQueryRelationProvider.createRelation(BigQueryRelationProvider.scala:110)
	at org.apache.spark.sql.execution.datasources.SaveIntoDataSourceCommand.run(SaveIntoDataSourceCommand.scala:47)
	at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:80)
	at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:78)
	at org.apache.spark.sql.execution.command.ExecutedCommandExec.executeCollect(commands.scala:89)
	at org.apache.spark.sql.execution.QueryExecution$$anonfun$$nestedInanonfun$eagerlyExecuteCommands$1$1.$anonfun$applyOrElse$1(QueryExecution.scala:174)
	at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withCustomExecutionEnv$8(SQLExecution.scala:245)
	at org.apache.spark.sql.execution.SQLExecution$.withSQLConfPropagated(SQLExecution.scala:393)
	at org.apache.spark.sql.execution.SQLExecution$.$anonfun$withCustomExecutionEnv$1(SQLExecution.scala:192)
	at org.apache.spark.sql.SparkSession.withActive(SparkSession.scala:979)
	at org.apache.spark.sql.execution.SQLExecution$.withCustomExecutionEnv(SQLExecution.scala:147)
	at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:343)
	at org.apache.spark.sql.execution.QueryExecution$$anonfun$$nestedInanonfun$eagerlyExecuteCommands$1$1.applyOrElse(QueryExecution.scala:174)
	at org.apache.spark.sql.execution.QueryExecution$$anonfun$$nestedInanonfun$eagerlyExecuteCommands$1$1.applyOrElse(QueryExecution.scala:170)
	at org.apache.spark.sql.catalyst.trees.TreeNode.$anonfun$transformDownWithPruning$1(TreeNode.scala:590)
	at org.apache.spark.sql.catalyst.trees.CurrentOrigin$.withOrigin(TreeNode.scala:168)
	at org.apache.spark.sql.catalyst.trees.TreeNode.transformDownWithPruning(TreeNode.scala:590)
	at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.org$apache$spark$sql$catalyst$plans$logical$AnalysisHelper$$super$transformDownWithPruning(LogicalPlan.scala:31)
	at org.apache.spark.sql.catalyst.plans.logical.AnalysisHelper.transformDownWithPruning(AnalysisHelper.scala:268)
	at org.apache.spark.sql.catalyst.plans.logical.AnalysisHelper.transformDownWithPruning$(AnalysisHelper.scala:264)
	at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.transformDownWithPruning(LogicalPlan.scala:31)
	at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.transformDownWithPruning(LogicalPlan.scala:31)
	at org.apache.spark.sql.catalyst.trees.TreeNode.transformDown(TreeNode.scala:566)
	at org.apache.spark.sql.execution.QueryExecution.$anonfun$eagerlyExecuteCommands$1(QueryExecution.scala:170)
	at org.apache.spark.sql.catalyst.plans.logical.AnalysisHelper$.allowInvokingTransformsInAnalyzer(AnalysisHelper.scala:324)
	at org.apache.spark.sql.execution.QueryExecution.eagerlyExecuteCommands(QueryExecution.scala:170)
	at org.apache.spark.sql.execution.QueryExecution.commandExecuted$lzycompute(QueryExecution.scala:155)
	at org.apache.spark.sql.execution.QueryExecution.commandExecuted(QueryExecution.scala:146)
	at org.apache.spark.sql.execution.QueryExecution.assertCommandExecuted(QueryExecution.scala:200)
	at org.apache.spark.sql.DataFrameWriter.runCommand(DataFrameWriter.scala:959)
	at org.apache.spark.sql.DataFrameWriter.saveToV1Source(DataFrameWriter.scala:427)
	at org.apache.spark.sql.DataFrameWriter.saveInternal(DataFrameWriter.scala:396)
	at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:258)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
	at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:380)
	at py4j.Gateway.invoke(Gateway.java:295)
	at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
	at py4j.commands.CallCommand.execute(CallCommand.java:79)
	at py4j.GatewayConnection.run(GatewayConnection.java:251)
	at java.lang.Thread.run(Thread.java:750)
Caused by: java.io.UncheckedIOException: Failed to create default Credentials
	at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryCredentialsSupplier.createDefaultCredentials(BigQueryCredentialsSupplier.java:101)
	at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryCredentialsSupplier.&amp;lt;init&amp;gt;(BigQueryCredentialsSupplier.java:50)
	at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule.provideBigQueryCredentialsSupplier(BigQueryClientModule.java:53)
	at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryClientModule$$FastClassByGuice$$b1b60333.invoke(&amp;lt;generated&amp;gt;)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.ProviderMethod$FastClassProviderMethod.doProvision(ProviderMethod.java:264)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.ProviderMethod.doProvision(ProviderMethod.java:173)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InternalProviderInstanceBindingImpl$CyclicFactory.provision(InternalProviderInstanceBindingImpl.java:185)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InternalProviderInstanceBindingImpl$CyclicFactory.get(InternalProviderInstanceBindingImpl.java:162)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:168)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:39)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:42)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:65)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.ProviderMethod.doProvision(ProviderMethod.java:173)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InternalProviderInstanceBindingImpl$CyclicFactory.provision(InternalProviderInstanceBindingImpl.java:185)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InternalProviderInstanceBindingImpl$CyclicFactory.get(InternalProviderInstanceBindingImpl.java:162)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:168)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:39)
	at com.google.cloud.spark.bigquery.repackaged.com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1094)
	... 45 more
Caused by: java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
	at com.google.cloud.spark.bigquery.repackaged.com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:134)
	at com.google.cloud.spark.bigquery.repackaged.com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:124)
	at com.google.cloud.spark.bigquery.repackaged.com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:96)
	at com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.connector.common.BigQueryCredentialsSupplier.createDefaultCredentials(BigQueryCredentialsSupplier.java:99)
	... 64 more&lt;/LI-CODE&gt;&lt;P&gt;I found this error:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Caused by: java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.&lt;/LI-CODE&gt;&lt;P&gt;Does it mean the code cannot read the credential in the temporary file?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2024 14:57:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101483#M40685</guid>
      <dc:creator>huytran</dc:creator>
      <dc:date>2024-12-09T14:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot write data to BigQuery when using Databricks secret</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101493#M40690</link>
      <description>&lt;P&gt;It seems that nothing is being loaded into the GOOGLE_APPLICATION_CREDENTIALS.&lt;/P&gt;
&lt;P&gt;From&amp;nbsp;&lt;A href="https://github.com/GoogleCloudDataproc/hadoop-connectors/blob/master/gcs/INSTALL.md" target="_blank" rel="noopener"&gt;https://github.com/GoogleCloudDataproc/hadoop-connectors/blob/master/gcs/INSTALL.md&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;# The JSON keyfile of the service account used for GCS
# access when google.cloud.auth.service.account.enable is true.
spark.hadoop.google.cloud.auth.service.account.json.keyfile=/path/to/keyfile&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think its worth trying this before raising a support ticket as there are many details that could be playing a role here. I think&amp;nbsp;os.environ["GOOGLE_APPLICATION_CREDENTIALS"], should've worked to be honest. So, I would expected the following to be a good starting point -summarizing what we've done so far-:&lt;/P&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;&lt;SPAN&gt;Here is how you can modify your code to ensure it works correctly:&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;&lt;SPAN&gt;&lt;STRONG&gt;Reading the Databricks Secret and Setting the Spark Configuration:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;DIV class="gb5fhw2"&gt;
&lt;PRE&gt;&lt;CODE class="markdown-code-python _1t7bu9hb hljs language-python gb5fhw3"&gt;&lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; base64

&lt;SPAN class="hljs-comment"&gt;# Read the secret from Databricks Secrets&lt;/SPAN&gt;
cred = dbutils.secrets.get(scope=&lt;SPAN class="hljs-string"&gt;"bigquery-scope"&lt;/SPAN&gt;, key=&lt;SPAN class="hljs-string"&gt;"secret-name"&lt;/SPAN&gt;).encode(&lt;SPAN class="hljs-string"&gt;'ascii'&lt;/SPAN&gt;)
cred = base64.b64encode(cred).decode(&lt;SPAN class="hljs-string"&gt;'ascii'&lt;/SPAN&gt;)

&lt;SPAN class="hljs-comment"&gt;# Set the credentials in Spark configuration&lt;/SPAN&gt;
spark.conf.&lt;SPAN class="hljs-built_in"&gt;set&lt;/SPAN&gt;(&lt;SPAN class="hljs-string"&gt;"spark.hadoop.google.cloud.auth.service.account.json.keyfile"&lt;/SPAN&gt;, cred)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;&lt;SPAN&gt;&lt;STRONG&gt;Reading Data from BigQuery:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;DIV class="gb5fhw2"&gt;
&lt;PRE&gt;&lt;CODE class="markdown-code-python _1t7bu9hb hljs language-python gb5fhw3"&gt;&lt;SPAN class="hljs-comment"&gt;# Read data from BigQuery&lt;/SPAN&gt;
df = spark.read.&lt;SPAN class="hljs-built_in"&gt;format&lt;/SPAN&gt;(&lt;SPAN class="hljs-string"&gt;"bigquery"&lt;/SPAN&gt;) \
    .option(&lt;SPAN class="hljs-string"&gt;"parentProject"&lt;/SPAN&gt;, &lt;SPAN class="hljs-string"&gt;"&amp;lt;parent-project-id&amp;gt;"&lt;/SPAN&gt;) \
    .option(&lt;SPAN class="hljs-string"&gt;"viewsEnabled"&lt;/SPAN&gt;, &lt;SPAN class="hljs-string"&gt;"true"&lt;/SPAN&gt;) \
    .option(&lt;SPAN class="hljs-string"&gt;"table"&lt;/SPAN&gt;, &lt;SPAN class="hljs-string"&gt;"&amp;lt;table-name&amp;gt;"&lt;/SPAN&gt;) \
    .load()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;&lt;SPAN&gt;&lt;STRONG&gt;Writing Data to BigQuery:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;DIV class="gb5fhw2"&gt;
&lt;PRE&gt;&lt;CODE class="markdown-code-python _1t7bu9hb hljs language-python gb5fhw3"&gt;&lt;SPAN class="hljs-comment"&gt;# Write data to BigQuery&lt;/SPAN&gt;
df.write.&lt;SPAN class="hljs-built_in"&gt;format&lt;/SPAN&gt;(&lt;SPAN class="hljs-string"&gt;"bigquery"&lt;/SPAN&gt;) \
    .mode(&lt;SPAN class="hljs-string"&gt;"overwrite"&lt;/SPAN&gt;) \
    .option(&lt;SPAN class="hljs-string"&gt;"temporaryGcsBucket"&lt;/SPAN&gt;, &lt;SPAN class="hljs-string"&gt;"&amp;lt;bucket-name&amp;gt;"&lt;/SPAN&gt;) \
    .option(&lt;SPAN class="hljs-string"&gt;"table"&lt;/SPAN&gt;, &lt;SPAN class="hljs-string"&gt;"&amp;lt;table-name&amp;gt;"&lt;/SPAN&gt;) \
    .option(&lt;SPAN class="hljs-string"&gt;"parentProject"&lt;/SPAN&gt;, &lt;SPAN class="hljs-string"&gt;"&amp;lt;parent-project-id&amp;gt;"&lt;/SPAN&gt;) \
    .save()&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;About the stacktrace itself, it simply looks like indeed the GOOGLE_APPLICATION_CREDENTIALS isn't set or is inaccessible, and none of the other defaultt credential sources are available:&lt;/P&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class="gb5fhw2"&gt;&lt;A href="https://github.com/googleapis/google-auth-library-java/blob/v0.27.0/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java#L124" target="_blank" rel="noopener"&gt;https://github.com/googleapis/google-auth-library-java/blob/v0.27.0/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java#L124&lt;/A&gt;&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;Consider this&amp;nbsp;&lt;A href="https://github.com/googleapis/google-auth-library-java/blob/v0.27.0/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java#L107-L108" target="_blank" rel="noopener"&gt;https://github.com/googleapis/google-auth-library-java/blob/v0.27.0/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java#L107-L108&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class="gb5fhw2"&gt;&lt;A href="https://github.com/googleapis/google-auth-library-java/blob/v0.27.0/oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java#L133" target="_self"&gt;https://github.com/googleapis/google-auth-library-java/blob/v0.27.0/oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java#L133&lt;/A&gt;&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;I think it eventually goes through&amp;nbsp;&lt;A href="https://github.com/googleapis/google-auth-library-java/blob/v0.27.0/oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java#L159" target="_blank" rel="noopener"&gt;https://github.com/googleapis/google-auth-library-java/blob/v0.27.0/oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java#L159&lt;/A&gt;&amp;nbsp;and attempts the other methods for a couple of second before bailing out.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Mon, 09 Dec 2024 16:00:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-write-data-to-bigquery-when-using-databricks-secret/m-p/101493#M40690</guid>
      <dc:creator>VZLA</dc:creator>
      <dc:date>2024-12-09T16:00:09Z</dc:date>
    </item>
  </channel>
</rss>

