簡體   English   中英

Google Cloud DataLab + BigQuery:如何設定地區/區域/位置

[英]Google Cloud DataLab + BigQuery: how to set region/zone/location

我正在將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的代碼。

您是否嘗試過bq.Dataset('[your_dataset]')。create(location ='EU')?

BigQuery位置是在數據集級別上設置的。 表根據其所在的數據集進行定位。

至少在Datalab外部設置數據集的位置:

from google.cloud import bigquery
bigquery_client = bigquery.Client(project='your_project')

dataset_ref = bigquery_client.dataset('your_dataset_name')
dataset = bigquery.Dataset(dataset_ref)
dataset.location = 'EU'

dataset = bigquery_client.create_dataset(dataset)

根據此處的代碼段: https//cloud.google.com/bigquery/docs/datasets

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM