从 BigQuery 查询 Bigtable 中的特定表

Query specific tables in Bigtable from BigQuery

我在 Google BigTable 中有一些数据,我在其上构建了一个 BigQuery 外部 table(根据 Querying Cloud Bigtable data 以便我可以查询 Bigtable table 使用常规 SQL(我非常熟悉)。

当我发出 select * 时,我得到了这个:

现在我想知道在这个嵌套数据中查询特定值的语法。例如,要获得 accountId 的列表,我可以这样做:

SELECT  ARRAY(SELECT timestamp FROM UNNEST(attributes.column[OFFSET(0)].cell)) AS timestamp,
        ARRAY(SELECT SAFE_CONVERT_BYTES_TO_STRING(value) FROM UNNEST(attributes.column[OFFSET(0)].cell)) AS values
FROM `table` 
where SAFE_CONVERT_BYTES_TO_STRING(rowkey) = 'XXXX'

其中 return 个:

嗯,有点方便。

类似地,我可以通过像这样更改 OFFSET 来获得 car#le11mcr#policyStartDate

SELECT  ARRAY(SELECT timestamp FROM UNNEST(attributes.column[OFFSET(6)].cell)) AS timestamp,
        ARRAY(SELECT SAFE_CONVERT_BYTES_TO_STRING(value) FROM UNNEST(attributes.column[OFFSET(6)].cell)) AS values
FROM `table` 
where SAFE_CONVERT_BYTES_TO_STRING(rowkey) = 'XXXX'

然而,这两个查询都要求我知道要传递给 OFFSET() 的值,并且该值似乎取决于 Bigtable 列的字母顺序,因此如果另一个列的名称以与(说)'b' 出现在未来我的查询将不再 return 同样的事情。

我需要一种比使用 OFFSET() 更好的查询 table 的方法。本质上我想说:

select the cell values and timestamp values for the cell whose name is accountId

select the cell values and timestamp values for the cell whose name is car#le11mcr#policyStartDate

有办法吗?我不太熟悉执行此操作的 BigQuery 语法。

好的,我取得了一点点进步。

这个:

SELECT  
    (
    select array(select timestamp from unnest(cell)) 
    from unnest(attributes.column) where name in ('accountId')
    ) accountIdTimestamp,
    (
    select array(select value from unnest(cell)) 
    from unnest(attributes.column) where name in ('accountId')
    ) accountIdValue
FROM `table` 
where SAFE_CONVERT_BYTES_TO_STRING(rowkey) = 'XXXX'
limit 3

returns:

哪个更好,但请注意前两行没有 return 任何内容。那是因为这两行没有名为 accountId 的单元格,我可以通过引入 WHERE 子句来解决这个问题:

SELECT  
    (
    select array(select timestamp from unnest(cell)) 
    from unnest(attributes.column) where name in ('accountId')
    ) accountIdTimestamp,
    (
    select array(select value from unnest(cell)) 
    from unnest(attributes.column) where name in ('accountId')
    ) accountIdValue
FROM `table` 
where ARRAY_LENGTH(ARRAY(
    select name from unnest(attributes.column) where name in ('accountId')
    )) > 0
limit 3

哪个 returns:

我想这就是我想要的,但我想有一种更好的方法可以实现这一点,它不需要那么多的输入和那么复杂的逻辑(尤其是 WHERE 子句感觉就像一种非常复杂的说法,如果有 accountId).

,只给我行

任何使本文更有效或更易读的建议都将不胜感激。

我要解决的下一个挑战是 return accountIdValue max(accountIdTimestamp)