BigQuery通过SQL导出到CSV文件

时间:2019-05-15 04:23:57

标签: google-cloud-platform google-bigquery google-cloud-storage

我想创建一个包含查询结果的CSV文件。 该CSV文件将保存在Google Cloud Storage中。 (此查询约为15GB)我需要将其作为单个文件。有可能吗?

CREATE OR REPLACE TABLE `your-project.your-dataset.chicago_taxitrips_mod` AS (
WITH
  taxitrips AS (
  SELECT
    trip_start_timestamp,
    trip_end_timestamp,
    trip_seconds,
    trip_miles,
    pickup_census_tract,
    dropoff_census_tract,
    pickup_community_area,
    dropoff_community_area,
    fare,
    tolls,
    extras,
    trip_total,
    payment_type,
    company,
    pickup_longitude,
    pickup_latitude,
    dropoff_longitude,
    dropoff_latitude,
    IF((tips/fare >= 0.2),
      1,
      0) AS tip_bin
  FROM
    `bigquery-public-data.chicago_taxi_trips.taxi_trips`
  WHERE
    trip_miles > 0
    AND fare > 0)
SELECT
  trip_start_timestamp,
  trip_end_timestamp,
  trip_seconds,
  trip_miles,
  pickup_census_tract,
  dropoff_census_tract,
  pickup_community_area,
  dropoff_community_area,
  fare,
  tolls,
  extras,
  trip_total,
  payment_type,
  company,
  tip_bin,
  ST_AsText(ST_SnapToGrid(ST_GeogPoint(pickup_longitude,
        pickup_latitude), 0.1)) AS pickup_grid,
  ST_AsText(ST_SnapToGrid(ST_GeogPoint(dropoff_longitude,
        dropoff_latitude), 0.1)) AS dropoff_grid,
  ST_Distance(ST_GeogPoint(pickup_longitude,
      pickup_latitude),
    ST_GeogPoint(dropoff_longitude,
      dropoff_latitude)) AS euclidean,
  CONCAT(ST_AsText(ST_SnapToGrid(ST_GeogPoint(pickup_longitude,
          pickup_latitude), 0.1)), ST_AsText(ST_SnapToGrid(ST_GeogPoint(dropoff_longitude,
          dropoff_latitude), 0.1))) AS loc_cross
FROM
  taxitrips
LIMIT
  100000000
  )

2 个答案:

答案 0 :(得分:1)

如果BigQuery需要输出多个文件,则可以通过gsutil操作将它们合并为一个文件,以用于GCS:

gsutil compose gs://bucket/obj1 [gs://bucket/obj2 ...] gs://bucket/composite
  

请注意,单个操作中可以组成的组件数量有一个限制(当前为32个)。

答案 1 :(得分:1)

无法将15GB导出到单个CSV文件(可以导出到多个文件)。我尝试了相同的查询(处理的字节数为15.66 GB),然后尝试将其导出到GCS中的CSV文件中,但由于此错误而失败

  

表gs:// [my_bucket] /bq_export/test.csv太大,无法导出到单个文件。指定一个带*的uri,以分片导出。请参阅https://cloud.google.com/bigquery/docs/exporting-data中的“将数据导出到一个或多个文件中”。

BQ Documentation仅允许您将最多1 GB的表数据导出到单个文件。由于表格超过1GB,因此您必须使用通配符,例如:

  

gs://您的存储桶名称/ csv文件名* .csv

enter image description here

不知道为什么要将导出的csv文件放在单个文件中,但是恕我直言,它太大而不能放在单个文件中。将其写入多个文件将更快,因为BQ会使用其并行性通过多个线程写入输出。