google-bigquery - 在 BigQuery 中提取一个又一个的数字或字符串

标签 google-bigquery

我有几个 1.000 个 URL,想要从 URL 参数中提取一些值。 以下是数据库中的一些示例:

["www.xxx.com?uci=6666&rci=fefw"]
[“www.xxx.com?uci=61
[“www.xxx.com?rci=62&uci=5536”]
[“www.xxx.com?uci=6666&utm_source=XXX”]
[“www.xxx.com?pccst=TEST%20sTESTg”]
["www.xxx.com?pccst=TEST2%20s&uci=1"]
[“www.xxx.com?uci=1pccst=TEST42rt24&rci=2”]

如何提取参数 UCI 的值。它始终是一个数字(不知道确切的长度)。 我用 REGEXP_EXTRACT 尝试过。但我没有成功:

REGEXP_EXTRACT(URL, '(uci)\=[0-9]+') AS UCI_extract

而且我还想提取参数 pccst 的值。它可以是每个字符,但我不知道确切的长度。但它总是以“或?或&

我也尝试使用 REGEXP_EXTRACT 但没有成功:

REGEXP_EXTRACT(URL, r'pccst\=(.*)(\"|\&|\?)') AS pccst_extract

我真的不是正则表达式专家。 如果有人能帮助我那就太好了。 预先非常感谢, 彼得

最佳答案

您可以适应this解决方案

#standardSQL
# Extract query parameters from a URL as ARRAY in BigQuery; standard-sql; 2018-04-08
# @see http://www.pascallandau.com/bigquery-snippets/extract-url-parameters-array/
WITH examples AS (
  SELECT 1   AS id, 'www.xxx.com?uci=6666&rci=fefw' AS query 
  UNION ALL SELECT 2, 'www.xxx.com?uci=1pccst%20TEST42rt24&rci=2'
  UNION ALL SELECT 3, 'www.xxx.com?pccst=TEST2%20s&uci=1'
)
SELECT 
  id, 
  query,
  REGEXP_EXTRACT_ALL(query,r'(?:\?|&)((?:[^=]+)=(?:[^&]*))') as params,
  REGEXP_EXTRACT_ALL(query,r'(?:\?|&)(?:([^=]+)=(?:[^&]*))') as keys,
  REGEXP_EXTRACT_ALL(query,r'(?:\?|&)(?:(?:[^=]+)=([^&]*))') as values
FROM examples

enter image description here

关于google-bigquery - 在 BigQuery 中提取一个又一个的数字或字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51969352/

相关文章:

google-bigquery - 如何根据 BQ 中的时间戳列创建分区表

python - Http 错误 : 400 Missing Required Parameter while using jobs. query() BQ API

google-bigquery - 具有多个分隔符的 BigQuery SPLIT

google-bigquery - 停止执行管道转换,同时其他管道转换继续运行

google-app-engine - GAE App 在与 BigQuery 通信时出现套接字错误

azure - Google 的 BigQuery 与 Azure 数据湖 U-SQL

json - 有没有办法将 BigQuery 表的架构导出为 JSON?

google-sheets - 是什么导致从 Google 表格创建的 BigQuery 表中出现 `Failure to read the spreadsheet` 错误?

python - Google Bigquery 的 TIMESTAMP 的 python 数据类型是什么?

google-bigquery - Google Cloud上的ETL-(数据流与 Spring 批处理)-> BigQuery