如何使用 bash 批量检索 Airflow 连接? (Google 作曲家)
How to retrieve Airflow connections in bulk using bash? (Google 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==' │
╘════════════════════════════════╧═════════════════════════════╧════════════════════════════════╧════════╧════════════════╧══════════════════════╧════════════════════════════════╛
此输出的问题在于它 return 几乎无法使用 table 数据(见下图)。理想情况下,它应该 return 一个 json 结构。
- 问题 1:是否有快速导出(和导入?)连接(和变量)为 json 的方法?
- 问题 2 : 如果问题 1 是“没有办法”,那么如何将这些数据变成漂亮的字典或类似键值的结构?
此外,如果您对 awk/grep
模式有任何补充说明,我们将不胜感激。
据我所知,您想将连接导出到 .json 文件。
目前,根据documentation,使用最新稳定版的Airflow,可以将连接导出到.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}
、here。
使用 cat file
而不是您的 gcloud
命令,我不必在您的问题中生成 table:
$ 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([=10=],/[^[:blank:]]+/)
FS = "[[:blank:]]*" substr([=10=],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(/7/,"\"")
printf "%s %s: {\n", (nr>2 ? ",\n" : ""),
for (i=2; i<=NF; i++) {
printf " \"%s\": %s%s\n", hdr[i], $i, (i<NF ? "," : "")
}
printf " }",
}
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=="
}
}
请注意,idk 是否确实有效json,我只是将文本块从您输入的位置移动到输出的位置。希望您可以轻松地进行所需的任何更改,以生成您实际想要获得的任何输出(您的问题中缺少)。
气流 1
- 连接到正确的 pod
gcloud container clusters get-credentials PROJECT_PATH --zone ZONE
PROJECT_PATH 类似于 projects/..../..gke :这是环境详细信息部分中 GKE 集群字段的值
kubectl get pods --all-namespaces
- 从 NAMESPACE 列中获取 composer-* 行的名称,从 NAME 列中获取 airflow-worker-* 行的名称。然后连接worker如下:
kubectl exec -itn composer-1-10-0-airflow-1-10-6-5983e0fe airflow-worker-8d8c49c87-9v7c4 -- /bin/bash
- 进入 worker 后,连接到数据库 shell。
airflow 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;
我正在尝试使用基础架构即代码 的方法创建作曲家环境。 为此,我需要能够以编程方式存储和检索气流变量,并在某处对它们进行版本控制。
以下命令让我列出指定 $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==' │
╘════════════════════════════════╧═════════════════════════════╧════════════════════════════════╧════════╧════════════════╧══════════════════════╧════════════════════════════════╛
此输出的问题在于它 return 几乎无法使用 table 数据(见下图)。理想情况下,它应该 return 一个 json 结构。
- 问题 1:是否有快速导出(和导入?)连接(和变量)为 json 的方法?
- 问题 2 : 如果问题 1 是“没有办法”,那么如何将这些数据变成漂亮的字典或类似键值的结构?
此外,如果您对 awk/grep
模式有任何补充说明,我们将不胜感激。
据我所知,您想将连接导出到 .json 文件。
目前,根据documentation,使用最新稳定版的Airflow,可以将连接导出到.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}
、here。
使用 cat file
而不是您的 gcloud
命令,我不必在您的问题中生成 table:
$ 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([=10=],/[^[:blank:]]+/)
FS = "[[:blank:]]*" substr([=10=],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(/7/,"\"")
printf "%s %s: {\n", (nr>2 ? ",\n" : ""),
for (i=2; i<=NF; i++) {
printf " \"%s\": %s%s\n", hdr[i], $i, (i<NF ? "," : "")
}
printf " }",
}
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=="
}
}
请注意,idk 是否确实有效json,我只是将文本块从您输入的位置移动到输出的位置。希望您可以轻松地进行所需的任何更改,以生成您实际想要获得的任何输出(您的问题中缺少)。
气流 1
- 连接到正确的 pod
gcloud container clusters get-credentials PROJECT_PATH --zone ZONE
PROJECT_PATH 类似于 projects/..../..gke :这是环境详细信息部分中 GKE 集群字段的值
kubectl get pods --all-namespaces
- 从 NAMESPACE 列中获取 composer-* 行的名称,从 NAME 列中获取 airflow-worker-* 行的名称。然后连接worker如下:
kubectl exec -itn composer-1-10-0-airflow-1-10-6-5983e0fe airflow-worker-8d8c49c87-9v7c4 -- /bin/bash
- 进入 worker 后,连接到数据库 shell。
airflow 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;