如何将 Google Dataproc 查询的结果存储在变量 GCP 中

How to store the result of Google Dataproc query inside a variable GCP

我有一个要求,我需要计算 gcloud 配置单元中的记录数 table 并且需要将此结果存储在一个变量中。

下面是相同的代码:

test=$(gcloud dataproc jobs submit hive --cluster=$CLUSTER --region=$REGION --execute="select count(*) from db.table;")

但是,上面的变量并没有存储记录的数量,而是存储了一些对我没有用的日志。

谁能帮我找出如何在变量内重定向上述查询的输出。

我猜你提到的输出包括 Hive 命令的日志 你想要的输出。听起来你只想要后者。

我建议使用 grepsedPython 之类的东西来捕获输出。如果您知道正则表达式 (regex),这应该很容易 - this is a good example of what you might want to do. If you have not used regex before, a regex builder like this one 会很有用。

gcloud 命令的输出实际上由两部分组成:stderr 和标准输出。包含计数的输出实际上在 stderr 中。下面的命令可以做到这一点,

cnt_output=$((gcloud dataproc jobs submit hive --cluster=$CLUSTER --region=$REGION --execute "select count(*) from db.table;" 1>/dev/null) 2>&1)

这基本上是先剥离standout,然后将stderr转换为标准输出,以便将其保存到变量中,即cnt_output

之后就可以使用上面回答中提到的工具来抓取你想要的号码了