【问题标题】:Migrate data from Google Analytics to AWS Athena将数据从 Google Analytics 迁移到 AWS Athena
【发布时间】:2020-07-23 14:50:49
【问题描述】:

我正在 AWS 中基于 Athena 创建一个 Datalake,我想查询我现在存储在 Google Analytics 中的数据。据我了解,我无权访问 Analytics 的原始数据,但我可以将其导出到 BigQuery,然后我可以从那里再次将其导出到 GCS(谷歌云存储)。我知道我可以创建一个自动化流程来将数据从 Analytics 导出到 BigQuery。

如何(轻松)创建从 BigQuery 到 GCS 的相同导出?

另外,导出所有历史数据的最简单方法是什么?我看到我可以从 BigQuery 控制台导出数据,但它只能导出一天的数据,而且该服务现在运行了一段时间。

一旦所有数据都在 GCS 中,我想我可以运行 AWS Lambda 将数据复制到我的 AWS 账户,这样我就可以查询它了。

【问题讨论】:

    标签: google-analytics google-bigquery


    【解决方案1】:

    根据文档,您不得在单个作业中从多个表中导出数据。

    如果您需要自动导出,我建议您使用Python 脚本,如下所示。

    在使用此脚本之前,请记住您需要为 Python 安装 BigQuery SDK。您可以通过在终端中运行 pip install google-cloud-bigquery 来做到这一点。还要记住,此代码正在考虑您要导出给定数据集中的所有表。如果数据集中有其他表而不是要导出的表,则需要过滤正确的表。

    from google.cloud import bigquery as bq
    
    # Defining the variables below to make the code cleaner
    project_id = "your-project-id"
    dataset_id = "your-dataset-id"
    
    # Starting client
    client = bq.Client(project=project_id)
    
    # Getting list of tables in your dataset
    t = client.list_tables(dataset=dataset_id)
    # Creating reference yo your dataset
    dataset_ref = bigquery.DatasetReference(project_id, dataset_id)
    
    
    # The loop below will repeat for all the tables listed in the dataset
    # The destination is in the format gs://<your-bucket>/<some-folder>/filename
    # The filename is in the format export_<table_name>_<hash>.csv
    # This hash is created by the wildcard (*). The wildcard is needed when 
    # your export is likely to generate a file bigger than 1 GB
    
    
    for i in t:
        table_id = i.table_id
        table_ref = dataset_ref.table(table_id)
        destination = "gs://your-bucket/your-folder/"+ table_id + "/export_" + table_id + "_*.csv"
        extract_job = client.extract_table(table_ref, destination, location="US")
    

    【讨论】:

    • 感谢@rmesteves,无法将数据直接从 Analytics 导出到 GCS?
    • @shlomiLan 很遗憾没有
    • 您还有其他问题吗?如果这些信息最终对您有所帮助,请考虑投票:)
    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 2016-10-21
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2014-10-16
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多