【问题标题】:BigQuery: join a firestore collection with its subcollectionBigQuery:加入 Firestore 集合及其子集合
【发布时间】:2019-11-01 12:22:52
【问题描述】:

我从 firestore 导入了我的数据,我有一个集合 users 和一个子集合 profiles。用户的密钥可以在matchingUsers.__key__.name 中找到(例如“USER_KEY”),而profiles 子集合的__key__.path 属性相当于'"users", "USER_KEY", "profiles", "PROFILE_KEY"'

我正在尝试获取所有用户的个人资料,因此我正在两个表之间进行连接。例如,我将matchingUsers.__key__.name 替换为userId,将profiles.__key__.path 替换为path

WITH users AS (
  SELECT "micheleId" AS userId, "Michele" as name UNION ALL
  SELECT "matteoId", "Matteo"
),
profiles AS (
    SELECT "x" AS profileId, '"users", "micheleId", "profiles", "x"' AS path, 'player' AS type UNION ALL
  SELECT "y", '"users", "micheleId", "profiles", "y"', 'coach' UNION ALL
  SELECT "z", '"users", "matteoId", "profiles", "z"', 'team'
)
SELECT userId, profileId, type 
FROM users JOIN profiles ON users.userId IN UNNEST(SPLIT(profiles.path ));

SPLIT获取数组的路径,然后只有当用户密钥在路径中时才使用IN UNNEST加入。

我得到了一个空的结果,而我完全期望:

+-----------+-----------+--------+
| userId    | profileId | type   |
+-----------+-----------+--------+
| micheleId | x         | player |
| micheleId | y         | coach  |
| matteoId  | z         | team   |
+--------------------------------+

【问题讨论】:

  • 您的查询看起来像是编造的(假设它会对我们有所帮助)或过于简单化了您的实际情况 - 所以在这里很难为您提供帮助。见How to create a Minimal, Reproducible Example
  • @MikhailBerlyant 和 Gordon 我添加了一个可重现的示例和预期的结果。现在应该更清楚了

标签: sql join google-cloud-firestore google-bigquery


【解决方案1】:

以下是一种“修复”查询的方法(更改仅在最后一行)

#standardSQL
WITH users AS (
  SELECT "micheleId" AS userId, "Michele" AS name UNION ALL
  SELECT "matteoId", "Matteo"
),
profiles AS (
  SELECT "x" AS profileId, 'users, micheleId, profiles, x' AS path, 'player' AS type UNION ALL
  SELECT "y", 'users, micheleId, profiles, y', 'coach' UNION ALL
  SELECT "z", 'users, matteoId, profiles, z', 'team'
)
SELECT userId, profileId, type 
FROM users 
JOIN profiles 
ON users.userId IN UNNEST(SPLIT(REPLACE(profiles.path, ' ', '')))

取决于您的实际用例 - 上面可能会有如下变化

ON users.userId IN UNNEST(SPLIT(profiles.path, ', '))   

ON users.userId IN UNNEST(SPLIT(REGEXP_REPLACE(profiles.path, r'\s', '')))   

...等等

在所有上述情况下 - 结果是

Row userId      profileId   type     
1   micheleId   x           player   
2   micheleId   y           coach    
3   matteoId    z           team     

我的错,我错误地添加了路径字符串。正确的格式是问题中更新的 '"users"、"micheleId"、"profiles"、"x"'

下面也是“修复”它

#standardSQL
WITH users AS (
  SELECT "micheleId" AS userId, "Michele" AS name UNION ALL
  SELECT "matteoId", "Matteo"
),
profiles AS (
    SELECT "x" AS profileId, '"users", "micheleId", "profiles", "x"' AS path, 'player' AS type UNION ALL
  SELECT "y", '"users", "micheleId", "profiles", "y"', 'coach' UNION ALL
  SELECT "z", '"users", "matteoId", "profiles", "z"', 'team'
)
SELECT userId, profileId, type 
FROM users JOIN profiles 
ON users.userId IN UNNEST(SPLIT(REGEXP_REPLACE(profiles.path, r'[" ]', '' )))

显然,结果相同

Row userId      profileId   type     
1   micheleId   x           player   
2   micheleId   y           coach    
3   matteoId    z           team    

如你所见 - 修复的相同想法

【讨论】:

  • 我的错,我错误地添加了路径字符串。正确的格式是问题中更新的 '"users"、"micheleId"、"profiles"、"x"'。你能看看这个格式吗?
猜你喜欢
  • 2021-03-10
  • 2021-01-25
  • 1970-01-01
  • 2020-09-24
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
相关资源
最近更新 更多