当前位置: 首页 > 面试题库 >

BigQuery UPDATE嵌套数组字段

赖诚
2023-03-14
问题内容

我想出了一些可行的方法,但并非完全符合我的期望。这是我的解决方案:

#standardSQL

UPDATE

  `attribution.daily_sessions_20180301_copy1` AS target

SET

hits = ARRAY(

  SELECT AS STRUCT * REPLACE(ARRAY(

    SELECT AS STRUCT *

    FROM(

      SELECT AS STRUCT * REPLACE(map.category AS productCategoryAttribute) FROM UNNEST(product))) AS product) FROM UNNEST(hits)

)

FROM

  `attribution.attribute_category_map`

AS map

WHERE

  (

    SELECT REPLACE(LOWER(prod.productCategory), 'amp;', '') FROM UNNEST(target.hits) AS h,

    UNNEST(h.product) AS prod LIMIT 1) = map.raw_name

attribute_category_map是一个具有两列的表,我在其中查找第1列中的对应值,并用第2列中的值替换目标表中的数据。我实现的最佳结果-
用相同的值更新了一行中的所有嵌套字段,这仅适用于第一个嵌套字段,而不是使用特定值更新每个嵌套字段。

主表的简化架构:

[  
   {  
      "name":"sessionId",
      "type":"STRING",
      "mode":"NULLABLE"
   },
   {  
      "name":"hits",
      "type":"RECORD",
      "mode":"REPEATED",
      "fields":[  
         {  
            "name":"product",
            "type":"RECORD",
            "mode":"REPEATED",
            "fields":[  
               {  
                  "name":"productCategory",
                  "type":"STRING",
                  "mode":"NULLABLE"
               },
               {  
                  "name":"productCategoryAttribute",
                  "type":"STRING",
                  "mode":"NULLABLE"
               }
            ]
         }
      ]
   }
]

会话行中通常有多个匹配项,一个匹配项中通常包含多个产品。看起来像那些值(如果您嵌套):

-----------------------------------------------------------------------------
sessionId | hits.product.productCategory| hit.product.productCategoryAttribute
-----------------------------------------------------------------------------
1         | automotive chemicals        | null
1         | automotive tools            | null
1         | null                        | null
2         | null                        | null
2         | automotive chemicals        | null
2         | null                        | null
3         | null                        | null
3         | bed accessories             | null
4         | null                        | null
4         | null                        | null
4         | automotive chemicals        | null
4         | null                        | null
-----------------------------------------------------------------------------

映射表的架构:

[  
   {  
      "name":"raw_name",
      "type":"STRING",
      "mode":"NULLABLE"
   },
   {  
      "name":"category",
      "type":"STRING",
      "mode":"NULLABLE"
   }
]

具有这样的值:

---------------------------------------------------
raw_name              |category                   |
---------------------------------------------------
automotive chemicals  |d1y2 - automotive chemicals|
automotive paint      |dijf1 - automotive paint   |
automotive tools      |efw1 - automotive tools    |
baby & infant toys    |wwfw - baby & infant toys  |
batteries & power     |fdsv- batteries & power    |
bed accessories       |0k77 - bed accessories     |
bike racks            |12df - bike racks          |
--------------------------------------------------

结果是我想要什么:

-----------------------------------------------------------------------------
    sessionId | hits.product.productCategory| hit.product.productCategoryAttribute
-----------------------------------------------------------------------------
    1         | automotive chemicals        | d1y2 - automotive chemicals
    1         | automotive tools            | efw1 - automotive tools
    1         | null                        | null
    2         | null                        | null
    2         | automotive chemicals        | d1y2 - automotive chemicals
    2         | null                        | null
    3         | null                        | null
    3         | bed accessories             | 0k77 - bed accessories
    4         | null                        | null
    4         | null                        | null
    4         | automotive chemicals        | d1y2 - automotive chemicals
    4         | null                        | null
    -----------------------------------------------------------------------------

我需要从主表中获取值productCategory,在map表中的raw_name列中查找它,从colum类别中获取值,并将其放入主表的productCategoryAttribute列中。主要问题是目标字段是双重嵌套的,我不知道如何直接加入它们。


问题答案:

下面经过测试!
保留整个表的架构/数据不变,并且仅根据各自的映射更新productCategoryAttribute的值

#standardSQL
UPDATE `project.dataset.your_table` t
SET hits = 
  ARRAY(
    SELECT AS STRUCT * REPLACE(
      ARRAY(
        SELECT AS STRUCT product.* REPLACE(
          CASE WHEN map.raw_name = product.productCategory THEN category 
            ELSE productCategoryAttribute END AS productCategoryAttribute)
        FROM UNNEST(product) product
        LEFT JOIN UNNEST(agg_map.map) map 
        ON map.raw_name = product.productCategory
      ) AS product)
    FROM UNNEST(hits) hit
  ) 
FROM (SELECT ARRAY_AGG(row) map FROM `project.dataset.map` row) agg_map 
WHERE TRUE

注意:以上解决方案假定映射表不是那么大,因为它依赖于将整个映射表聚合到一个数组中



 类似资料:
  • 问题内容: 我有一个从数据库(JSON MySQL中的数据存储)检索数据的程序。 我设法得到对象。输出为: JSON对象: 我需要有关如何处理数据并将信息放入不同数组/对象的建议。例如 谢谢。 问题答案: 您可以使用Jackson Api来实现。 您必须创建与json对象相同的Pojo类(该类应具有“ attributes”,“ uuid”之类的成员)。 这是您必须使用的类 和代码 现在,您可以使

  • 问题内容: 我对ES还是相当陌生,并正在将其用于我的新项目。首先,我为客户提供了一个简单的映射,其中包含名字和姓氏以及付款信息对象列表。如果我在SQL中执行此操作,那将类似于客户表和具有1:许多关系的付款信息表。 这是我要执行的操作的一个简单示例:https : //gist.github.com/anonymous/6109593 我希望根据payInfos嵌套数组中的任何匹配项找到任何客户,即

  • 嗨,我是ReactJS平台的学生开发者。我以前在render方法中使用过类组件,但现在我学习了钩子和函数组件对它的重要性,就像每个Reactjs开发人员所知道的那样。我在使用嵌套组件时遇到问题,我面临如下错误: index.js:1警告:函数作为React子函数无效。如果返回组件而不是从渲染返回组件,可能会发生这种情况。或者你想调用这个函数而不是返回它 你能帮我解决这个问题吗?如何在返回部分有效地

  • 本文向大家介绍简化嵌套数组JavaScript,包括了简化嵌套数组JavaScript的使用技巧和注意事项,需要的朋友参考一下 假设我们有一个数组数组,其中包含一些这样的元素- 我们的工作是编写一个递归函数,该函数接受此嵌套数组,并将数组中的所有fale值(NaN,undefined和null)替换为0。 因此,让我们为该函数编写代码- 示例 输出结果 控制台中的输出将为-

  • 问题内容: 我有一个嵌套的数组,我想在其中显示结果的子集。例如,在下面的数组中,我想遍历嵌套array [1]中的所有值。 我试图使用foreach函数,但似乎无法正常工作。这是我的原始语法(尽管我意识到这是错误的)。 我试图避免对键是否与我要搜索的键相同的变量进行比较,即 有任何想法吗? 问题答案: 如果您知道嵌套数组中的级别数,则可以简单地执行嵌套循环。像这样: 如果您不知道数组的深度,则需要

  • 本文向大家介绍C ++中的数组嵌套,包括了C ++中的数组嵌套的使用技巧和注意事项,需要的朋友参考一下 假设我们有一个长度为N的零索引数组A,其中包含从0到N-1的所有整数。我们必须找到并返回集合S的最长长度,其中S [i] = {A [i],A [A [i]],A [A [A [i []]],...}以下规则。现在考虑S中的第一个元素以索引= i的元素A [i]的选择开始,S中的下一个元素应为A