我正在尝试用基础结构作为代码来创建一个composer环境。为此,我需要能够以编程的方式存储和检索气流变量,并将它们保存在某个地方。
下面的命令允许我列出指定$COMPOSER_ENV中的所有连接
gcloud composer environments run `$COMPOSER_ENV` --location <location> connections -- --list这是输出:
╒════════════════════════════════╤═════════════════════════════╤════════════════════════════════╤════════╤════════════════╤══════════════════════╤════════════════════════════════╕
│ Conn Id │ Conn Type │ Host │ Port │ Is Encrypted │ Is Extra Encrypted │ Extra │
╞════════════════════════════════╪═════════════════════════════╪════════════════════════════════╪════════╪════════════════╪══════════════════════╪════════════════════════════════╡
│ 'airflow_db' │ 'mysql' │ 'airflow-sqlp...rvice.default' │ None │ True │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'beeline_default' │ 'beeline' │ 'localhost' │ 10000 │ False │ True │ 'gAAAAABfdZs0...yjt7nj1C2Dzgm' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'bigquery_default' │ 'google_cloud_platform' │ None │ None │ False │ True │ 'gAAAAABfdZs2...AOdwY-EnZLg==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'local_mysql' │ 'mysql' │ 'localhost' │ None │ True │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'presto_default' │ 'presto' │ 'localhost' │ 3400 │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'google_cloud_default' │ 'google_cloud_platform' │ None │ None │ False │ True │ 'gAAAAABfdZs2...oMm2saUwAxQ==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'hive_cli_default' │ 'hive_cli' │ None │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'pig_cli_default' │ 'pig_cli' │ None │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'hiveserver2_default' │ 'hiveserver2' │ 'localhost' │ 10000 │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'metastore_default' │ 'hive_metastore' │ 'localhost' │ 9083 │ False │ True │ 'gAAAAABfdZs0...vNSgFh1mE1HY=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'mongo_default' │ 'mongo' │ 'mongo' │ 27017 │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'mysql_default' │ 'mysql' │ 'mysql' │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'postgres_default' │ 'postgres' │ 'postgres' │ None │ True │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'sqlite_default' │ 'sqlite' │ '/tmp/sqlite_default.db' │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'http_default' │ 'http' │ 'https://www.httpbin.org/' │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'mssql_default' │ 'mssql' │ 'localhost' │ 1433 │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'vertica_default' │ 'vertica' │ 'localhost' │ 5433 │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'wasb_default' │ 'wasb' │ None │ None │ False │ True │ 'gAAAAABfdZs0...ST7E2347-uG4=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'webhdfs_default' │ 'hdfs' │ 'localhost' │ 50070 │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'ssh_default' │ 'ssh' │ 'localhost' │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'sftp_default' │ 'sftp' │ 'localhost' │ 22 │ False │ True │ 'gAAAAABfdZs0...guLrr1ky5XpN2' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'fs_default' │ 'fs' │ None │ None │ False │ True │ 'gAAAAABfdZs0...WqhP9ZLa8gQ==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'aws_default' │ 'aws' │ None │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'spark_default' │ 'spark' │ 'yarn' │ None │ False │ True │ 'gAAAAABfdZs0...18ws2BelkcL8=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'druid_broker_default' │ 'druid' │ 'druid-broker' │ 8082 │ False │ True │ 'gAAAAABfdZs0...sC6Kcd9mOKhE=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'druid_ingest_default' │ 'druid' │ 'druid-overlord' │ 8081 │ False │ True │ 'gAAAAABfdZs0...CpBdCkHuk5lqw' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'redis_default' │ 'redis' │ 'redis' │ 6379 │ False │ True │ 'gAAAAABfdZs0...E1qdjhMngIg==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'sqoop_default' │ 'sqoop' │ 'rmdbs' │ None │ False │ False │ '' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'emr_default' │ 'emr' │ None │ None │ False │ True │ 'gAAAAABfdZs0...GsJIS8IjaBuM=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'databricks_default' │ 'databricks' │ 'localhost' │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'qubole_default' │ 'qubole' │ 'localhost' │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'segment_default' │ 'segment' │ None │ None │ False │ True │ 'gAAAAABfdZs0...oawClUj4Qzj8i' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'azure_data_lake_default' │ 'azure_data_lake' │ None │ None │ False │ True │ 'gAAAAABfdZs0...DMIAMmOeZNg==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'azure_cosmos_default' │ 'azure_cosmos' │ None │ None │ False │ True │ 'gAAAAABfdZs0...tusOfGrWviAk=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'azure_contai...ances_default' │ 'azure_container_instances' │ None │ None │ False │ True │ 'gAAAAABfdZs0...q460BKvTu4Lk=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'cassandra_default' │ 'cassandra' │ 'cassandra' │ 9042 │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'dingding_default' │ 'http' │ '' │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'opsgenie_default' │ 'http' │ '' │ None │ False │ False │ None │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'google_cloud...store_default' │ 'google_cloud_platform' │ None │ None │ False │ True │ 'gAAAAABfdZs2...ltsxQHWUgxA==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'google_cloud_storage_default' │ 'google_cloud_platform' │ None │ None │ False │ True │ 'gAAAAABfdZs2...RNLazPEE7gQ==' │
╘════════════════════════════════╧═════════════════════════════╧════════════════════════════════╧════════╧════════════════╧══════════════════════╧════════════════════════════════╛这个输出的问题是,它返回了一个难以使用的数据表(参见下面的图像)。理想情况下,它应该返回类似于json的结构。
此外,对awk/grep模式的任何其他解释都将不胜感激。
发布于 2020-10-02 14:28:53
使用cat file代替gcloud命令,我不需要在您的问题中生成表:
$ cat tst.awk
!/[[:space:]]/ {
# Skip all lines that separate the data rows
next
}
(++nr) == 1 {
# Set FS to whatever the combination of control chars is at the start of the first data line
match($0,/[^[:blank:]]+/)
FS = "[[:blank:]]*" substr($0,1,RLENGTH) "[[:blank:]]*"
}
{
# Get rid of the FSs at the start and end of the line to avoid leading/trailing null fields
gsub("^" FS "|" FS "$","")
}
nr == 1 {
# Store the header lines for later use
for (i=1; i<=NF; i++) {
gsub(/[[:blank:]]+/,"_",$i)
hdr[i] = $i
}
print "{"
next
}
{
# Print the json-equivalent for the data on the current line
gsub(/\047/,"\"")
printf "%s %s: {\n", (nr>2 ? ",\n" : ""), $1
for (i=2; i<=NF; i++) {
printf " \"%s\": %s%s\n", hdr[i], $i, (i<NF ? "," : "")
}
printf " }", $1
}
END {
print "\n}"
}$ cat file | awk -f tst.awk
{
"airflow_db": {
"Conn_Type": "mysql",
"Host": "airflow-sqlp...rvice.default",
"Port": None,
"Is_Encrypted": True,
"Is_Extra_Encrypted": False,
"Extra": None
},
"beeline_default": {
"Conn_Type": "beeline",
"Host": "localhost",
"Port": 10000,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...yjt7nj1C2Dzgm"
},
"bigquery_default": {
"Conn_Type": "google_cloud_platform",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs2...AOdwY-EnZLg=="
},
"local_mysql": {
"Conn_Type": "mysql",
"Host": "localhost",
"Port": None,
"Is_Encrypted": True,
"Is_Extra_Encrypted": False,
"Extra": None
},
"presto_default": {
"Conn_Type": "presto",
"Host": "localhost",
"Port": 3400,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"google_cloud_default": {
"Conn_Type": "google_cloud_platform",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs2...oMm2saUwAxQ=="
},
"hive_cli_default": {
"Conn_Type": "hive_cli",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"pig_cli_default": {
"Conn_Type": "pig_cli",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"hiveserver2_default": {
"Conn_Type": "hiveserver2",
"Host": "localhost",
"Port": 10000,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"metastore_default": {
"Conn_Type": "hive_metastore",
"Host": "localhost",
"Port": 9083,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...vNSgFh1mE1HY="
},
"mongo_default": {
"Conn_Type": "mongo",
"Host": "mongo",
"Port": 27017,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"mysql_default": {
"Conn_Type": "mysql",
"Host": "mysql",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"postgres_default": {
"Conn_Type": "postgres",
"Host": "postgres",
"Port": None,
"Is_Encrypted": True,
"Is_Extra_Encrypted": False,
"Extra": None
},
"sqlite_default": {
"Conn_Type": "sqlite",
"Host": "/tmp/sqlite_default.db",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"http_default": {
"Conn_Type": "http",
"Host": "https://www.httpbin.org/",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"mssql_default": {
"Conn_Type": "mssql",
"Host": "localhost",
"Port": 1433,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"vertica_default": {
"Conn_Type": "vertica",
"Host": "localhost",
"Port": 5433,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"wasb_default": {
"Conn_Type": "wasb",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...ST7E2347-uG4="
},
"webhdfs_default": {
"Conn_Type": "hdfs",
"Host": "localhost",
"Port": 50070,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"ssh_default": {
"Conn_Type": "ssh",
"Host": "localhost",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"sftp_default": {
"Conn_Type": "sftp",
"Host": "localhost",
"Port": 22,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...guLrr1ky5XpN2"
},
"fs_default": {
"Conn_Type": "fs",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...WqhP9ZLa8gQ=="
},
"aws_default": {
"Conn_Type": "aws",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"spark_default": {
"Conn_Type": "spark",
"Host": "yarn",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...18ws2BelkcL8="
},
"druid_broker_default": {
"Conn_Type": "druid",
"Host": "druid-broker",
"Port": 8082,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...sC6Kcd9mOKhE="
},
"druid_ingest_default": {
"Conn_Type": "druid",
"Host": "druid-overlord",
"Port": 8081,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...CpBdCkHuk5lqw"
},
"redis_default": {
"Conn_Type": "redis",
"Host": "redis",
"Port": 6379,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...E1qdjhMngIg=="
},
"sqoop_default": {
"Conn_Type": "sqoop",
"Host": "rmdbs",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": ""
},
"emr_default": {
"Conn_Type": "emr",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...GsJIS8IjaBuM="
},
"databricks_default": {
"Conn_Type": "databricks",
"Host": "localhost",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"qubole_default": {
"Conn_Type": "qubole",
"Host": "localhost",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"segment_default": {
"Conn_Type": "segment",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...oawClUj4Qzj8i"
},
"azure_data_lake_default": {
"Conn_Type": "azure_data_lake",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...DMIAMmOeZNg=="
},
"azure_cosmos_default": {
"Conn_Type": "azure_cosmos",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...tusOfGrWviAk="
},
"azure_contai...ances_default": {
"Conn_Type": "azure_container_instances",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs0...q460BKvTu4Lk="
},
"cassandra_default": {
"Conn_Type": "cassandra",
"Host": "cassandra",
"Port": 9042,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"dingding_default": {
"Conn_Type": "http",
"Host": "",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"opsgenie_default": {
"Conn_Type": "http",
"Host": "",
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": False,
"Extra": None
},
"google_cloud...store_default": {
"Conn_Type": "google_cloud_platform",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs2...ltsxQHWUgxA=="
},
"google_cloud_storage_default": {
"Conn_Type": "google_cloud_platform",
"Host": None,
"Port": None,
"Is_Encrypted": False,
"Is_Extra_Encrypted": True,
"Extra": "gAAAAABfdZs2...RNLazPEE7gQ=="
}
}注意,如果这是有效的json,我只是将文本块从输入中的位置移动到输出中的位置。希望你能很容易地做出你需要做的任何改变来产生你真正想要得到的任何输出(这是你的问题中缺少的)。
发布于 2022-04-04 19:29:41
用于气流1
gcloud container clusters get-credentials PROJECT_PATH --zone ZONEPROJECT_PATH类似于projects/. GKE :这是环境细节部分中GKE集群字段的值
kubectl get pods --all-namespaceskubectl exec -itn composer-1-10-0-airflow-1-10-6-5983e0fe airflow-worker-8d8c49c87-9v7c4 -- /bin/bashairflow shell注意:如果shell命令不可用,请使用https://cloud.google.com/composer/docs/access-airflow-database#airflow-1上的指令访问数据库。然后运行SQL。
select json_arrayagg(
json_object(
'id', id,
'conn_id', conn_id,
'conn_type', conn_type,
'host', host,
'login',login,
'password',password,
'port',port,
'extra', extra,
'is_encrypted', is_encrypted,
'is_extra_encrypted', is_extra_encrypted,
'schema',IFNULL(`schema`, null)
)
) as json_result
from (select * from connection ) x;发布于 2020-10-02 12:13:43
如我所见,您希望将连接导出到.json文件。
目前,根据文档,使用最新和稳定版本的气流,您可以导出连接到一个.json文件。命令如下,
airflow connections export connections.json或,
airflow connections export /tmp/connections --format json.json格式使用模式,
{
"airflow_db": {
"conn_type": "mysql",
"host": "mysql",
"login": "root",
"password": "plainpassword",
"schema": "airflow",
"port": null,
"extra": null
},
"druid_broker_default": {
"conn_type": "druid",
"host": "druid-broker",
"login": null,
"password": null,
"schema": null,
"port": 8082,
"extra": "{\"endpoint\": \"druid/v2/sql\"}"
}
} 此外,每个连接都存储在与此命名约定有关的环境变量中:AIRFLOW_CONN_{CONN_ID}、这里。
https://stackoverflow.com/questions/64159695
复制相似问题