Skip to main content
本文に 39 文字追加
ソース リンク
apstndb
  • 101
  • 1

UDF で JSON.parse する手はどうでしょう。

#standardSQL
# STRUCT の型は JSON に合わせる
CREATE TEMPORARY FUNCTION
  parse_logdata(logdata STRING)
  RETURNS STRUCT>>
  LANGUAGE js AS """
  return JSON.parse(logdata);
""";
# LOGDATA_TABLE にログが入っている前提
WITH
  LOGDATA_TABLE AS (
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12345,  "friends":[{"userno":1, "nickname":"hogehoge"}, {"userno":1, "nickname":"fugafuga"}]}' AS logdata
  UNION ALL
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12346,  "friends":[{"userno":1, "nickname":"foobar"}, {"userno":1, "nickname":"piyopiyo"}]}')
SELECT
  logdata.userno AS userno,
  friends.userno AS friend_no,
  nickname
FROM (
  SELECT
    parse_logdata(logdata).*
  FROM
    LOGDATA_TABLE) AS logdata
INNER JOIN
  UNNEST(logdata.friends) AS friends

追記: UDF の入ったクエリの同時実行数には注意する必要があるかも。
https://cloud.google.com/bigquery/quotas?hl=en
https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions?hl=en#limits
また、 VIEW では使えないし BI ツールでも使えないのが普通ですね。

UDF で JSON.parse する手はどうでしょう。

#standardSQL
# STRUCT の型は JSON に合わせる
CREATE TEMPORARY FUNCTION
  parse_logdata(logdata STRING)
  RETURNS STRUCT>>
  LANGUAGE js AS """
  return JSON.parse(logdata);
""";
# LOGDATA_TABLE にログが入っている前提
WITH
  LOGDATA_TABLE AS (
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12345,  "friends":[{"userno":1, "nickname":"hogehoge"}, {"userno":1, "nickname":"fugafuga"}]}' AS logdata
  UNION ALL
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12346,  "friends":[{"userno":1, "nickname":"foobar"}, {"userno":1, "nickname":"piyopiyo"}]}')
SELECT
  logdata.userno AS userno,
  friends.userno AS friend_no,
  nickname
FROM (
  SELECT
    parse_logdata(logdata).*
  FROM
    LOGDATA_TABLE) AS logdata
INNER JOIN
  UNNEST(logdata.friends) AS friends

追記: UDF の入ったクエリの同時実行数には注意する必要があるかも。
https://cloud.google.com/bigquery/quotas?hl=en

UDF で JSON.parse する手はどうでしょう。

#standardSQL
# STRUCT の型は JSON に合わせる
CREATE TEMPORARY FUNCTION
  parse_logdata(logdata STRING)
  RETURNS STRUCT>>
  LANGUAGE js AS """
  return JSON.parse(logdata);
""";
# LOGDATA_TABLE にログが入っている前提
WITH
  LOGDATA_TABLE AS (
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12345,  "friends":[{"userno":1, "nickname":"hogehoge"}, {"userno":1, "nickname":"fugafuga"}]}' AS logdata
  UNION ALL
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12346,  "friends":[{"userno":1, "nickname":"foobar"}, {"userno":1, "nickname":"piyopiyo"}]}')
SELECT
  logdata.userno AS userno,
  friends.userno AS friend_no,
  nickname
FROM (
  SELECT
    parse_logdata(logdata).*
  FROM
    LOGDATA_TABLE) AS logdata
INNER JOIN
  UNNEST(logdata.friends) AS friends

追記: UDF の入ったクエリの同時実行数には注意する必要があるかも。
https://cloud.google.com/bigquery/quotas?hl=en
https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions?hl=en#limits
また、 VIEW では使えないし BI ツールでも使えないのが普通ですね。

本文に 87 文字追加
ソース リンク
apstndb
  • 101
  • 1

UDF で JSON.parse する手はどうでしょう。

#standardSQL
# STRUCT の型は JSON に合わせる
CREATE TEMPORARY FUNCTION
  parse_logdata(logdata STRING)
  RETURNS STRUCT>>
  LANGUAGE js AS """
  return JSON.parse(logdata);
""";
# LOGDATA_TABLE にログが入っている前提
WITH
  LOGDATA_TABLE AS (
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12345,  "friends":[{"userno":1, "nickname":"hogehoge"}, {"userno":1, "nickname":"fugafuga"}]}' AS logdata
  UNION ALL
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12346,  "friends":[{"userno":1, "nickname":"foobar"}, {"userno":1, "nickname":"piyopiyo"}]}')
SELECT
  logdata.userno AS userno,
  friends.userno AS friend_no,
  nickname
FROM (
  SELECT
    parse_logdata(logdata).*
  FROM
    LOGDATA_TABLE) AS logdata
INNER JOIN
  UNNEST(logdata.friends) AS friends

追記: UDF の入ったクエリの同時実行数には注意する必要があるかも。
https://cloud.google.com/bigquery/quotas?hl=en

UDF で JSON.parse する手はどうでしょう。

#standardSQL
# STRUCT の型は JSON に合わせる
CREATE TEMPORARY FUNCTION
  parse_logdata(logdata STRING)
  RETURNS STRUCT>>
  LANGUAGE js AS """
  return JSON.parse(logdata);
""";
# LOGDATA_TABLE にログが入っている前提
WITH
  LOGDATA_TABLE AS (
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12345,  "friends":[{"userno":1, "nickname":"hogehoge"}, {"userno":1, "nickname":"fugafuga"}]}' AS logdata
  UNION ALL
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12346,  "friends":[{"userno":1, "nickname":"foobar"}, {"userno":1, "nickname":"piyopiyo"}]}')
SELECT
  logdata.userno AS userno,
  friends.userno AS friend_no,
  nickname
FROM (
  SELECT
    parse_logdata(logdata).*
  FROM
    LOGDATA_TABLE) AS logdata
INNER JOIN
  UNNEST(logdata.friends) AS friends

UDF で JSON.parse する手はどうでしょう。

#standardSQL
# STRUCT の型は JSON に合わせる
CREATE TEMPORARY FUNCTION
  parse_logdata(logdata STRING)
  RETURNS STRUCT>>
  LANGUAGE js AS """
  return JSON.parse(logdata);
""";
# LOGDATA_TABLE にログが入っている前提
WITH
  LOGDATA_TABLE AS (
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12345,  "friends":[{"userno":1, "nickname":"hogehoge"}, {"userno":1, "nickname":"fugafuga"}]}' AS logdata
  UNION ALL
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12346,  "friends":[{"userno":1, "nickname":"foobar"}, {"userno":1, "nickname":"piyopiyo"}]}')
SELECT
  logdata.userno AS userno,
  friends.userno AS friend_no,
  nickname
FROM (
  SELECT
    parse_logdata(logdata).*
  FROM
    LOGDATA_TABLE) AS logdata
INNER JOIN
  UNNEST(logdata.friends) AS friends

追記: UDF の入ったクエリの同時実行数には注意する必要があるかも。
https://cloud.google.com/bigquery/quotas?hl=en

ソース リンク
apstndb
  • 101
  • 1

UDF で JSON.parse する手はどうでしょう。

#standardSQL
# STRUCT の型は JSON に合わせる
CREATE TEMPORARY FUNCTION
  parse_logdata(logdata STRING)
  RETURNS STRUCT>>
  LANGUAGE js AS """
  return JSON.parse(logdata);
""";
# LOGDATA_TABLE にログが入っている前提
WITH
  LOGDATA_TABLE AS (
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12345,  "friends":[{"userno":1, "nickname":"hogehoge"}, {"userno":1, "nickname":"fugafuga"}]}' AS logdata
  UNION ALL
  SELECT
    '{  "date": "2015-08-31",  "time": "00:00:00",  "type": "RESPONSE",  "userno": 12346,  "friends":[{"userno":1, "nickname":"foobar"}, {"userno":1, "nickname":"piyopiyo"}]}')
SELECT
  logdata.userno AS userno,
  friends.userno AS friend_no,
  nickname
FROM (
  SELECT
    parse_logdata(logdata).*
  FROM
    LOGDATA_TABLE) AS logdata
INNER JOIN
  UNNEST(logdata.friends) AS friends