如何从 python 调用 snowsql 客户端
How to call snowsql client from python
我正在从 shell 脚本调用 snowsql 客户端。我正在通过执行源代码导入属性文件。并调用 snowsql 客户端。我怎样才能在 Python 中做同样的事情?任何帮助将不胜感激。
Shell 调用 snowsql 客户端的脚本:
source /opt/data/airflow/config/cyrus_de/snowflake_config.txt
sudo /home/user/snowsql -c $connection --config=/home/user/.snowsql/config -w $warehouse --variable database_name=$dbname --variable stage=$stagename --variable env=$env -o exit_on_error=true -o variable_substitution=True -q /data/snowsql/queries.sql
假设您转而使用 Python 纯粹是为了改进控制流,并且仍想继续使用 shell 功能,直接翻译需要编写一个函数 acts as the source command to import environment variables, then using them in a subprocess call that executes with a shell to allow environment variable substitution:
import os, shlex, subprocess
def source_file_into_env():
command = shlex.split("env -i bash -c 'source /opt/data/airflow/config/cyrus_de/snowflake_config.txt && env'")
proc = subprocess.Popen(command, stdout = subprocess.PIPE)
for line in proc.stdout:
(key, _, value) = line.partition("=")
os.environ[key] = value
proc.communicate()
def run():
source_file_into_env()
subprocess.run("""sudo /home/user/snowsql \
-c $connection \
--config=/home/user/.snowsql/config \
-w $warehouse \
--variable database_name=$dbname \
--variable stage=$stagename \
--variable env=$env \
-o exit_on_error=true \
-o variable_substitution=True \
-q /data/snowsql/queries.sql""", \
shell=True, \
env=os.environ)
if __name__ == '__main__':
run()
如果您希望完全 Python 而不需要任何 shell 调用,那么更多 native connector offered by Snowflake can be used instead of snowsql
. This would be a far more invasive change but the connection examples 将帮助您入门。
我正在从 shell 脚本调用 snowsql 客户端。我正在通过执行源代码导入属性文件。并调用 snowsql 客户端。我怎样才能在 Python 中做同样的事情?任何帮助将不胜感激。
Shell 调用 snowsql 客户端的脚本:
source /opt/data/airflow/config/cyrus_de/snowflake_config.txt
sudo /home/user/snowsql -c $connection --config=/home/user/.snowsql/config -w $warehouse --variable database_name=$dbname --variable stage=$stagename --variable env=$env -o exit_on_error=true -o variable_substitution=True -q /data/snowsql/queries.sql
假设您转而使用 Python 纯粹是为了改进控制流,并且仍想继续使用 shell 功能,直接翻译需要编写一个函数 acts as the source command to import environment variables, then using them in a subprocess call that executes with a shell to allow environment variable substitution:
import os, shlex, subprocess
def source_file_into_env():
command = shlex.split("env -i bash -c 'source /opt/data/airflow/config/cyrus_de/snowflake_config.txt && env'")
proc = subprocess.Popen(command, stdout = subprocess.PIPE)
for line in proc.stdout:
(key, _, value) = line.partition("=")
os.environ[key] = value
proc.communicate()
def run():
source_file_into_env()
subprocess.run("""sudo /home/user/snowsql \
-c $connection \
--config=/home/user/.snowsql/config \
-w $warehouse \
--variable database_name=$dbname \
--variable stage=$stagename \
--variable env=$env \
-o exit_on_error=true \
-o variable_substitution=True \
-q /data/snowsql/queries.sql""", \
shell=True, \
env=os.environ)
if __name__ == '__main__':
run()
如果您希望完全 Python 而不需要任何 shell 调用,那么更多 native connector offered by Snowflake can be used instead of snowsql
. This would be a far more invasive change but the connection examples 将帮助您入门。