Eco*_*ior 23 python google-cloud-storage google-cloud-platform google-cloud-datalab
您好,感谢您的时间和考虑.我正在Google Cloud Platform/Datalab中开发一个Jupyter笔记本.我创建了一个Pandas DataFrame,并希望将此DataFrame写入Google云端存储(GCS)和/或BigQuery.我在GCS中有一个存储桶,并通过以下代码创建了以下对象:
import gcp
import gcp.storage as storage
project = gcp.Context.default().project_id
bucket_name = 'steve-temp'
bucket_path = bucket_name
bucket = storage.Bucket(bucket_path)
bucket.exists()
Run Code Online (Sandbox Code Playgroud)
我尝试过基于Google Datalab文档的各种方法,但仍然失败.谢谢
The*_*heo 22
from google.cloud import storage
import os
import pandas as pd
# Only need this if you're running this code locally.
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'/your_GCP_creds/credentials.json'
df = pd.DataFrame(data=[{1,2,3},{4,5,6}],columns=['a','b','c'])
client = storage.Client()
bucket = client.get_bucket('my-bucket-name')
bucket.blob('upload_test/test.csv').upload_from_string(df.to_csv(), 'text/csv')
Run Code Online (Sandbox Code Playgroud)
Ant*_*iou 15
尝试以下工作示例:
from datalab.context import Context
import google.datalab.storage as storage
import google.datalab.bigquery as bq
import pandas as pd
# Dataframe to write
simple_dataframe = pd.DataFrame(data=[{1,2,3},{4,5,6}],columns=['a','b','c'])
sample_bucket_name = Context.default().project_id + '-datalab-example'
sample_bucket_path = 'gs://' + sample_bucket_name
sample_bucket_object = sample_bucket_path + '/Hello.txt'
bigquery_dataset_name = 'TestDataSet'
bigquery_table_name = 'TestTable'
# Define storage bucket
sample_bucket = storage.Bucket(sample_bucket_name)
# Create storage bucket if it does not exist
if not sample_bucket.exists():
sample_bucket.create()
# Define BigQuery dataset and table
dataset = bq.Dataset(bigquery_dataset_name)
table = bq.Table(bigquery_dataset_name + '.' + bigquery_table_name)
# Create BigQuery dataset
if not dataset.exists():
dataset.create()
# Create or overwrite the existing table if it exists
table_schema = bq.Schema.from_data(simple_dataframe)
table.create(schema = table_schema, overwrite = True)
# Write the DataFrame to GCS (Google Cloud Storage)
%storage write --variable simple_dataframe --object $sample_bucket_object
# Write the DataFrame to a BigQuery table
table.insert(simple_dataframe)
Run Code Online (Sandbox Code Playgroud)
我使用了这个例子,以及来自datalab github站点的_table.py文件作为参考.您可以在此链接中找到其他源代码文件.datalab
Jan*_*auw 12
使用Google Cloud Datalab文档
import datalab.storage as gcs
gcs.Bucket('bucket-name').item('to/data.csv').write_to(simple_dataframe.to_csv(),'text/csv')
Run Code Online (Sandbox Code Playgroud)
更新 @Anthonios Partheniou的回答.
代码现在有点不同 - 截至2017年11月29日
传递包含一个元组project_id并dataset_id给bq.Dataset.
# define a BigQuery dataset
bigquery_dataset_name = ('project_id', 'dataset_id')
dataset = bq.Dataset(name = bigquery_dataset_name)
Run Code Online (Sandbox Code Playgroud)
传递包含一个元组project_id,dataset_id和表名bq.Table.
# define a BigQuery table
bigquery_table_name = ('project_id', 'dataset_id', 'table_name')
table = bq.Table(bigquery_table_name)
Run Code Online (Sandbox Code Playgroud)
# Create BigQuery dataset
if not dataset.exists():
dataset.create()
# Create or overwrite the existing table if it exists
table_schema = bq.Schema.from_data(dataFrame_name)
table.create(schema = table_schema, overwrite = True)
# Write the DataFrame to a BigQuery table
table.insert(dataFrame_name)
Run Code Online (Sandbox Code Playgroud)
自 2017 年以来,Pandas 有一个 Dataframe 到 BigQuery 函数pandas.DataFrame.to_gbq
该文档有一个示例:
import pandas_gbq as gbq
gbq.to_gbq(df, 'my_dataset.my_table', projectid, if_exists='fail')
参数if_exists可以设置为“失败”、“替换”或“附加”
另请参阅此示例。
我花了很多时间找到最简单的方法来解决这个问题:
import pandas as pd
df = pd.DataFrame(...)
df.to_csv('gs://bucket/path')
Run Code Online (Sandbox Code Playgroud)
要将 parquet 文件保存在 GCS 中,并通过服务帐户进行身份验证:
df.to_parquet("gs://<bucket-name>/file.parquet",
storage_options={"token": <path-to-gcs-service-account-file>}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17670 次 |
| 最近记录: |