我将 Datalab 用于 Python 笔记本,它将数据从 Cloud Storage 加载到 BigQuery 中,基本上遵循此示例。
然后我看到我在 Cloud Storage 存储桶中的原始数据在欧盟(eu-west3-a),执行 Datalab 的 VM 在同一区域,但 BigQuery 中的最终数据在美国。
根据这篇文章,我尝试在代码中设置数据集的位置,但没有奏效。这是因为Datalab.Bigquery Python 模块中没有定义这样的选项。
所以我的问题是:如何为 BigQuery 数据集及其包含的表设置位置(区域和区域)?
这是我的代码:
# data: https://www.kaggle.com/benhamner/sf-bay-area-bike-share/data
%%gcs read --object gs://my_bucket/kaggle/station.csv --variable stations
# CSV will be read as bytes first
df_stations = pd.read_csv(StringIO(stations))
schema = bq.Schema.from_data(df_stations)
# Create an empty dataset
#bq.Dataset('kaggle_bike_rentals').create(location='europe-west3-a')
bq.Dataset('kaggle_bike_rentals').create()
# Create an empty table within the dataset
table_stations = bq.Table('kaggle_bike_rentals.stations').create(schema = schema, overwrite = True)
# load data directly from cloud storage into the bigquery table. the locally loaded Pandas dataframe won't be used here
table_stations.load('gs://my_bucket/kaggle/station.csv', mode='append', source_format = 'csv', csv_options=bq.CSVOptions(skip_leading_rows = 1))
更新:同时,我在 BigQuery Web-UI 中手动创建了数据集,并在代码中使用它而不在那里创建它。现在,如果数据集不存在则会引发异常,因此禁止在代码中创建一个将导致默认位置 US 的数据集。