-1

I need to write data into snowflake database, I have the below script, but I want to write data without giving username and password, is there any other way? #NOTE - I have to run this script in databricks.

import json
import snowflake.connector

# Set up the Snowflake connection parameters
conn = snowflake.connector.connect(
    user='your_username',
    password='your_password',
    account='your_account_name',
    warehouse='your_warehouse'
)

# Create a cursor object to execute SQL statements
cursor = conn.cursor()


# Assuming `json_data` contains the fetched JSON data
parsed_data = json.loads(json_data)

# Iterate through the parsed data and insert it into Snowflake
for record in parsed_data:
    # Assuming your table has columns like `column1`, `column2`, etc.
    query = f"INSERT INTO your_table (column1, column2) VALUES ('{record['key1']}', '{record['key2']}')"
    cursor.execute(query)

# Commit the changes
conn.commit()

cursor.close()
conn.close()


gourav
  • 33
  • 7

2 Answers2

0

how about creating environmental variables and import them

mac/linux

EXPORT S_USR="your_snowflake_usr"
EXPORT S_PWD="your_snowflake_pwd"

windows/cmd

set S_USR="your_snowflake_usr"
set S_PWD="your_snowflake_pwd"

use them in python:

user = os.environ.get("S_USR")
pwd = os.environ.get("S_PWD")
nisakova
  • 89
  • 6
  • I have to run this script in databricks, this will be a automated script, which will run on a particular time everyday – gourav Aug 01 '23 at 14:14
0

Use Databricks secret manager:

user = dbutils.secrets.get("data-warehouse", "<snowflake-user>")
password = dbutils.secrets.get("data-warehouse", "<snowflake-password>")
 
options = {
  "sfUrl": "<snowflake-url>",
  "sfUser": user,
  "sfPassword": password,
  "sfDatabase": "<snowflake-database>",
  "sfSchema": "<snowflake-schema>",
  "sfWarehouse": "<snowflake-cluster>"
}
NickW
  • 8,430
  • 2
  • 6
  • 19