首页 > 解决方案 > 如何知道我的表中有哪些属于 bigQuery 中的长期存储?

问题描述

我目前在一个项目中有多个数据集,我想知道是否有办法一次性知道过去 90 天内未更新的表是什么,这意味着它属于 bigquery 中的长期存储桶。有一些 sql 语法吗?

谢谢

标签: google-bigquery

解决方案


不是 SQL,而是一个可能的解决方案是使用该bq工具。描述一个表将返回长期存储字节数 ( numLongTermBytes):

$ bq show --format=prettyjson dataset.table
...
  "kind": "bigquery#table", 
  "lastModifiedTime": "1534845362446", 
  "location": "US", 
  "numBytes": "56", 
  "numLongTermBytes": "56", 
  "numRows": "3", 
... 

因此,您可以将其扩展为列出每个数据集中的所有表,并从每个表中获取所需的信息。基于,我做了一个简单的例子:

#!/bin/bash

project=PROJECT_ID
dataset=DATASET_NAME
max_results=100

# get list of tables
tables=$(bq ls --max_results $max_results "$project:$dataset" | awk '{print $1}' | tail -n +3)

# get LTS bytes for each table
for table in $tables
do
    printf '%-35s %-50s\n' "$table" "$(bq show --format prettyjson $project:$dataset.$table | grep numLongTermBytes)"
done

您将获得类似于以下内容的输出:

Dfp                                   "numLongTermBytes": "0",                        
SO_55506947                           "numLongTermBytes": "144",                      
SO_55506947_bis                       "numLongTermBytes": "144",                      
a                                     "numLongTermBytes": "7",                        
a1                                    "numLongTermBytes": "399",                      
aaa                                   "numLongTermBytes": "8",                        
adaptive                              "numLongTermBytes": "1085",                     
adaptive_view                         "numLongTermBytes": "0",                        
audience_segment_map_exp_test         "numLongTermBytes": "300",                      
b                                     "numLongTermBytes": "7", 
... 

推荐阅读