云自然语言API Python脚本错误(客户端对象没有属性create_rows)



我试图创建一个脚本,通过自然语言API的分类工具提供文章,我找到了一个完全可以做到这一点的教程。我遵循这个简单的教程来介绍谷歌云和自然语言API。

最终结果应该是一个脚本,将一堆新文章从谷歌云存储发送到自然语言API,对文章进行分类,然后将整个内容保存到BigQuery中创建的表中。

我很好地遵循了这个例子,但在运行最终脚本时,我得到了以下错误:

Traceback (most recent call last):
File "classify-text.py", line 39, in <module>
errors = bq_client.create_rows(table, rows_for_bq)
AttributeError: 'Client' object has no attribute 'create_rows'

完整的脚本是:

from google.cloud import storage, language, bigquery
# Set up our GCS, NL, and BigQuery clients
storage_client = storage.Client()
nl_client = language.LanguageServiceClient()
# TODO: replace YOUR_PROJECT with your project name below
bq_client = bigquery.Client(project='Your_Project')
dataset_ref = bq_client.dataset('news_classification')
dataset = bigquery.Dataset(dataset_ref)
table_ref = dataset.table('article_data')
table = bq_client.get_table(table_ref)
# Send article text to the NL API's classifyText method
def classify_text(article):
response = nl_client.classify_text(
document=language.types.Document(
content=article,
type=language.enums.Document.Type.PLAIN_TEXT
)
)
return response

rows_for_bq = []
files = storage_client.bucket('text-classification-codelab').list_blobs()
print("Got article files from GCS, sending them to the NL API (this will take ~2 minutes)...")
# Send files to the NL API and save the result to send to BigQuery
for file in files:
if file.name.endswith('txt'):
article_text = file.download_as_string()
nl_response = classify_text(article_text)
if len(nl_response.categories) > 0:
rows_for_bq.append((article_text, nl_response.categories[0].name, nl_response.categories[0].confidence))
print("Writing NL API article data to BigQuery...")
# Write article text + category data to BQ
errors = bq_client.create_rows(table, rows_for_bq)
assert errors == []

您正在使用不推荐使用的方法;这些方法在0.29版本中被标记为过时,在1.0.0版本中被完全删除。

您应该使用client.insert_rows();该方法接受相同的参数:

errors = bq_client.insert_rows(table, rows_for_bq)

相关内容

最新更新