Repository navigation
How put an array of objects at Google Cloud Datastore? #3361
Copy link
Copy link
Closed
Labels
api: datastoreIssues related to the Datastore API.Issues related to the Datastore API.
Description
OS : Ubuntu 14.04.5
Python: 3.4.3
google-cloud-datastore: 0.24.0
Code example:
from google.cloud import datastore
class GoogleDatastorePipeline(object):
def __init__(self, settings, stats):
self.client = datastore.Client()
self.batch = self.client.batch()
def open(self):
self.batch.begin()
def process(self):
key = self.client.key('Article')
entity = datastore.Entity(key)
entity['name'] = 'A post'
entity['content'] = '<html></html>'
# How I insert this as array of objects? Can be indexed, if no, no problem.
entity['authors'] = [{
'name': 'Author 1',
'type': 'person',
},{
'name': 'Author 2',
'type': 'organization',
}]
self.batch.put(entity)
def close(self):
self.batch.commit()Stacktrace:
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/home/ubuntu/workspace/ze/pipelines/__init__.py", line 215, in process_item
self.batch.put(entity)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/batch.py", line 199, in put
_assign_entity_to_pb(entity_pb, entity)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/batch.py", line 319, in _assign_entity_to_pb
bare_entity_pb = helpers.entity_to_protobuf(entity)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/helpers.py", line 219, in entity_to_protobuf
_set_protobuf_value(value_pb, value)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/helpers.py", line 408, in _set_protobuf_value
attr, val = _pb_attr_value(val)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/helpers.py", line 325, in _pb_attr_value
raise ValueError("Unknown protobuf attr type %s" % type(val))
ValueError: Unknown protobuf attr type <class 'dict'>
2017-05-03 01:30:16 [scrapy.core.engine] INFO: Closing spider (finished)
2017-05-03 01:30:16 [google_auth_httplib2] DEBUG: Making request: POST https://accounts.google.com/o/oauth2/token
2017-05-03 01:30:16 [scrapy.core.engine] ERROR: Scraper close failure
Traceback (most recent call last):
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/retry.py", line 120, in inner
return to_call(*args)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/retry.py", line 68, in inner
return a_func(*updated_args, **kwargs)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/grpc/_channel.py", line 507, in __call__
return _end_unary_response_blocking(state, call, False, deadline)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/grpc/_channel.py", line 455, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Mutation is missing operation.)>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/_gax.py", line 74, in _catch_remap_gax_error
yield
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/_gax.py", line 173, in commit
return super(GAPICDatastoreAPI, self).commit(*args, **kwargs)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/gapic/datastore/v1/datastore_client.py", line 345, in commit
return self._commit(request, options)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/api_callable.py", line 419, in inner
return api_caller(api_call, this_settings, request)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/api_callable.py", line 407, in base_caller
return api_call(*args)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/api_callable.py", line 368, in inner
return a_func(*args, **kwargs)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/retry.py", line 126, in inner
' classified as transient', exception)
google.gax.errors.RetryError: GaxError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Mutation is missing operation.)>)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/home/ubuntu/workspace/ze/pipelines/__init__.py", line 193, in close_spider
self.batch.commit()
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/batch.py", line 273, in commit
self._commit()
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/batch.py", line 249, in _commit
self.project, mode, self._mutations, transaction=self._id)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/_gax.py", line 173, in commit
return super(GAPICDatastoreAPI, self).commit(*args, **kwargs)
File "/usr/lib/python3.4/contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/_gax.py", line 82, in _catch_remap_gax_error
six.reraise(error_class, new_exc, sys.exc_info()[2])
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/_gax.py", line 74, in _catch_remap_gax_error
yield
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/datastore/_gax.py", line 173, in commit
return super(GAPICDatastoreAPI, self).commit(*args, **kwargs)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/cloud/gapic/datastore/v1/datastore_client.py", line 345, in commit
return self._commit(request, options)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/api_callable.py", line 419, in inner
return api_caller(api_call, this_settings, request)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/api_callable.py", line 407, in base_caller
return api_call(*args)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/api_callable.py", line 368, in inner
return a_func(*args, **kwargs)
File "/home/ubuntu/workspace/env/lib/python3.4/site-packages/google/gax/retry.py", line 126, in inner
' classified as transient', exception)
google.cloud.exceptions.BadRequest: 400 Mutation is missing operation.Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
api: datastoreIssues related to the Datastore API.Issues related to the Datastore API.
Activity
gustavorps commentedon May 3, 2017
I tested the JSON below on Datastore Entites (https://console.cloud.google.com/datastore/entities/new) and worked!
{ "values": [ { "entityValue": { "properties": { "name": { "stringValue": "NAME1" }, "type": { "stringValue": "TYPE1" } } } }, { "entityValue": { "properties": { "name": { "stringValue": "NAME2" }, "type": { "stringValue": "TYPE2" } } } } ] }But in python don't
Traceback
dhermes commentedon May 3, 2017
Thanks for filing @gustavorps I'll look into it.
dhermes commentedon May 3, 2017
@gustavorps Two things:
dictis not supported as an entity type. Here is the list of supported types, though we should do a better job surfacing this list / making__setitem__fail for a bad type. What you want to use there is anotherEntity, not adict.Batchor aTransaction? Abatchwill just accumulate mutations, but won't commit them transactionally.I am preemptively closing this issue since the
dict->Entityfix covers your two errors (the second error occurs becausebatch.put()will fail with a bad entity with adictfield and if theput()failed then there will be no mutations to commit).We can re-open and continue discussion if you think there are more things to discuss.
Add better documentation for support types in datastore Entity.
gustavorps commentedon May 3, 2017
Thx for fas thet replay @dhermes.
Now i'm using a
Entityinstead of adictand try 2 forms to add to array of entityValues and the results is this:First Form
Traceback
Second Form
Traceback
I decided to use the
clientinstead of atransactionbecause my code runs for more than 1 minute and only make inserts andbatchhow you say will just accumulate mutations, but won't commit them transactionally.dhermes commentedon May 3, 2017
datastore.Entitytakes akeyas the first argument, not a string ('author1'or'author2'). This is why you sawgustavorps commentedon May 3, 2017
My bad @dhermes!
Everything is work now. Thx very much for the support.
You can close the issue.
dhermes commentedon May 3, 2017
Cheers!
Add better documentation for support types in datastore Entity. (#3363)
Add better documentation for support types in datastore Entity. (#3363)