2

这是我的代码

from gcloud import bigquery
from google.oauth2 import service_account
import uuid

def send_data_from_gcs(dataset_name, table_name):

MAX_BAD_RECORDS = 5
job_name = str(uuid.uuid4())
credential = service_account.Credentials.from_service_account_file(r'C:\Users\ernes\Downloads\gcs\kola-business-manager-second.json')
bigquery_client = bigquery.Client(credentials= credential, project='kola-business-manager')
dataset = bigquery_client.dataset(dataset_name)
table = dataset.table(table_name)


bucket_name = "accountingbucket"
prefix = 'Accounting'
job = bigquery_client.load_table_from_storage(job_name, table, 'gs://'+bucket_name+'/'+prefix+'/*')
job.source_format = 'NEWLINE_DELIMITED_JSON'
job.encoding = 'UTF-8'
job.create_disposition = 'CREATE_IF_NEEDED'
job.write_disposition = 'WRITE_APPEND'
job.max_bad_records=MAX_BAD_RECORDS

job.begin()
wait_for_job(job)

if job.state == 'DONE' and not job.error_result and not job.errors:
    print("Updating report as done: " + prefix)
else:
    print("Inserting report as error: " + prefix)
    print(job.errors)

    raise RuntimeError(job.error_result)

这是我在运行我的函数时遇到的错误我不明白为什么会发生错误,我需要你的帮助来解决它。我在网上尝试了很多资源,但直到现在才解决。谢了,兄弟们

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-cc7c9bd422bc> in <module>
----> 1 send_data_from_gcs('kbm_data', 'actest')

<ipython-input-14-e4955f75f8f8> in send_data_from_gcs(dataset_name, table_name)
 23     job.max_bad_records=MAX_BAD_RECORDS
 24 
 ---> 25     job.begin()
 26     wait_for_job(job)
 27 
 ~\Anaconda3\lib\site-packages\gcloud\bigquery\job.py in begin(self, client)
361         client = self._require_client(client)
362         path = '/projects/%s/jobs' % (self.project,)
--> 363         api_response = client.connection.api_request(
364             method='POST', path=path, data=self._build_resource())
365         self._set_properties(api_response)
~\Anaconda3\lib\site-packages\gcloud\connection.py in api_request(self, method, path, 
query_params, data, content_type, api_base_url, api_version, expect_json, _target_object)
339             content_type = 'application/json'
340 
--> 341         response, content = self._make_request(
342             method=method, url=url, data=data, content_type=content_type,
343             target_object=_target_object)
~\Anaconda3\lib\site-packages\gcloud\connection.py in _make_request(self, method, url, data, 
content_type, headers, target_object)
239         headers['User-Agent'] = self.USER_AGENT
240 
--> 241         return self._do_request(method, url, headers, data, target_object)
242 
243     def _do_request(self, method, url, headers, data,
~\Anaconda3\lib\site-packages\gcloud\connection.py in _do_request(self, method, url, headers, 
data, target_object)
267         :returns: The HTTP response object and the content of the response.
268         """
--> 269         return self.http.request(uri=url, method=method, headers=headers,
270                                  body=data)
271 
~\Anaconda3\lib\site-packages\gcloud\connection.py in http(self)
 98             self._http = httplib2.Http()
 99             if self._credentials:
--> 100                 self._http = self._credentials.authorize(self._http)
101         return self._http
AttributeError: 'Credentials' object has no attribute 'authorize'
 

当调用 job.Begin() 时,它似乎返回了这个错误。这是我在运行我的函数时遇到的错误我不明白为什么会发生错误,我需要你的帮助来解决它。我在网上尝试了很多资源,但直到现在才解决。谢了,兄弟们

4

0 回答 0