You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@predictionio.apache.org by Noelia Osés Fernández <no...@vicomtech.org> on 2017/12/22 11:37:38 UTC
How to import item properties dynamically?
Hi all,
I have a pio app and I need to update item properties regularly. However,
not all items will have all properties always. So I want to update the
properties dynamically doing something similiar to the following:
# create properties json
propertiesjson = '{'
if "tiempo" in dfcolumns:
propertiesjson = propertiesjson + '"tiempo": ' +
str(int(plan.tiempo))
if "duracion" in dfcolumns:
propertiesjson = propertiesjson + ', "duracion": ' +
str(plan.duracion)
propertiesjson = propertiesjson + '}'
# add event
client.create_event(
event="$set",
entity_type="item",
entity_id=plan.id_product,
properties=json.dumps(propertiesjson)
)
However, this results in an error message:
Traceback (most recent call last):
File "import_itemproperties.py", line 110, in <module>
import_events(client, args.dbuser, args.dbpasswd, args.dbhost,
args.dbname)
File "import_itemproperties.py", line 73, in import_events
properties=json.dumps(propertiesjson)
File
"/home/ubuntu/.local/lib/python2.7/site-packages/predictionio/__init__.py",
line 255, in create_event
event_time).get_response()
File
"/home/ubuntu/.local/lib/python2.7/site-packages/predictionio/connection.py",
line 111, in get_response
self._response = self.rfunc(tmp_response)
File
"/home/ubuntu/.local/lib/python2.7/site-packages/predictionio/__init__.py",
line 130, in _acreate_resp
response.body))
predictionio.NotCreatedError: request: POST
/events.json?accessKey=0Hys1qwfgo3vF16jElBDJJnSLmrkN5Tg86qAPqepYPK_-lXMqI4NMjLXaBGgQJ4U
{'entityId': 8, 'entityType': 'item', 'properties': '"{\\"tiempo\\": 2,
\\"duracion\\": 60}"', 'event': '$set', 'eventTime':
'2017-12-22T11:29:59.762+0000'}
/events.json?accessKey=0Hys1qwfgo3vF16jElBDJJnSLmrkN5Tg86qAPqepYPK_-lXMqI4NMjLXaBGgQJ4U?entityId=8&entityType=item&properties=%22%7B%5C%22tiempo%5C%22%3A+2%2C+%5C%22duracion%5C%22%3A+60%2C&event=%24set&eventTime=2017-12-22T11%3A29%3A59.762%2B0000
status: 400 body: {"message":"org.json4s.package$MappingException: Expected
object but got JString(\"{\\\"tiempo\\\": 2, \\\"duracion\\\": 60}\")"}
Any help is much appreciated!
Season's greetings!
Noelia
Re: How to import item properties dynamically?
Posted by Pat Ferrel <pa...@occamsmachete.com>.
The properties go into the Event Store immediately but you have to train to get them into the model, this assuming your template support item properties. If yo uare using the UR, the properties will not get into the model until the next `pio train…`
On Dec 22, 2017, at 3:37 AM, Noelia Osés Fernández <no...@vicomtech.org> wrote:
Hi all,
I have a pio app and I need to update item properties regularly. However, not all items will have all properties always. So I want to update the properties dynamically doing something similiar to the following:
# create properties json
propertiesjson = '{'
if "tiempo" in dfcolumns:
propertiesjson = propertiesjson + '"tiempo": ' + str(int(plan.tiempo))
if "duracion" in dfcolumns:
propertiesjson = propertiesjson + ', "duracion": ' + str(plan.duracion)
propertiesjson = propertiesjson + '}'
# add event
client.create_event(
event="$set",
entity_type="item",
entity_id=plan.id_product,
properties=json.dumps(propertiesjson)
)
However, this results in an error message:
Traceback (most recent call last):
File "import_itemproperties.py", line 110, in <module>
import_events(client, args.dbuser, args.dbpasswd, args.dbhost, args.dbname)
File "import_itemproperties.py", line 73, in import_events
properties=json.dumps(propertiesjson)
File "/home/ubuntu/.local/lib/python2.7/site-packages/predictionio/__init__.py", line 255, in create_event
event_time).get_response()
File "/home/ubuntu/.local/lib/python2.7/site-packages/predictionio/connection.py", line 111, in get_response
self._response = self.rfunc(tmp_response)
File "/home/ubuntu/.local/lib/python2.7/site-packages/predictionio/__init__.py", line 130, in _acreate_resp
response.body))
predictionio.NotCreatedError: request: POST /events.json?accessKey=0Hys1qwfgo3vF16jElBDJJnSLmrkN5Tg86qAPqepYPK_-lXMqI4NMjLXaBGgQJ4U {'entityId': 8, 'entityType': 'item', 'properties': '"{\\"tiempo\\": 2, \\"duracion\\": 60}"', 'event': '$set', 'eventTime': '2017-12-22T11:29:59.762+0000'} /events.json?accessKey=0Hys1qwfgo3vF16jElBDJJnSLmrkN5Tg86qAPqepYPK_-lXMqI4NMjLXaBGgQJ4U?entityId=8&entityType=item&properties=%22%7B%5C%22tiempo%5C%22%3A+2%2C+%5C%22duracion%5C%22%3A+60%2C&event=%24set&eventTime=2017-12-22T11%3A29%3A59.762%2B0000 status: 400 body: {"message":"org.json4s.package$MappingException: Expected object but got JString(\"{\\\"tiempo\\\": 2, \\\"duracion\\\": 60}\")"}
Any help is much appreciated!
Season's greetings!
Noelia