Update nested field in BigQuery table

后端 未结 3 1866
醉话见心
醉话见心 2020-12-20 22:44

I am trying to perform what, you would think, is a trivial operation in BigQuery; I am trying to update a nested field in a BigQuery table that is the result of a 360 export

相关标签:
3条回答
  • 2020-12-20 23:14

    hits is an array, so you need to use an array subquery to assign to it. It would look something like this:

    #standardSQL
    UPDATE `dataset_name`.`ga_sessions_20170705`
    SET hits =
      ARRAY(
        SELECT AS STRUCT * REPLACE(
          (SELECT AS STRUCT eventInfo.* REPLACE('some string' AS eventLabel)) AS eventInfo)
        FROM UNNEST(hits)
      )
    WHERE TRUE;
    
    0 讨论(0)
  • 2020-12-20 23:25

    Here is to mask PII data in ga sessions having an email

    UPDATE
         `<project-id>.<dataset-name>.<table-name>`
     
    SET hits =
      ARRAY(SELECT AS STRUCT * REPLACE (
        -- correcting pages here
        IF(REGEXP_CONTAINS(page.pagePath, r"@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+")
        ,STRUCT(
            REGEXP_REPLACE(page.pagePath, r"@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+", "[EMAIL]")
            ,page.pagePathLevel1
            ,page.pagePathLevel2
            ,page.pagePathLevel3
            ,page.pagePathLevel4
            ,page.hostname
            ,page.pageTitle
            ,page.searchKeyword
            ,page.searchCategory
        ), page) AS page)
        
        FROM UNNEST(hits)
      ) 
    WHERE ( -- only relevant sessions
      SELECT COUNT(1) > 0 
      FROM UNNEST(hits) AS hits
      WHERE totals.visits = 1
        AND hits.type = 'PAGE'
        AND REGEXP_CONTAINS(hits.page.pagePath, r"@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+") = true
    )
    
    0 讨论(0)
  • 2020-12-20 23:36

    If you need to modify a given custom dimension you can use this:

    #standardSQL
    UPDATE `tablename`
    SET hits = 
      ARRAY(
        SELECT AS STRUCT * REPLACE(
          ARRAY(
            SELECT AS STRUCT cd.index,
              CASE WHEN cd.index = index_number THEN 'new value'
              ELSE cd.value
              END
            FROM UNNEST(customDimensions) AS cd
          ) AS customDimensions)
        FROM UNNEST(hits) hit
      )
    WHERE TRUE
    

    But it takes a while to run.

    0 讨论(0)
提交回复
热议问题