Discussion Forum

Exception happens when inserting data to Grakn via Python client

Hi,

I am trying to insert two entities and relation into the Grakn data storage as follow:

insertTask = f’’‘insert $t isa Task, has Title “{task[‘Title’]}”, has TaskID {task[‘TaskID’]}, has TaskExternalID “{task[‘TaskExternalID’]}”, has TaskDescription “{task[‘TaskDescription’]}”, has StartDate {task[‘StartDate’]}, has Duration {task[‘Duration’]}, has IsRead {‘true’ if task[“IsRead”] else ‘false’}, has HasForms {‘true’ if task[“HasForms”] else ‘false’}, has CompanyID {task[‘CompanyID’]}, has CompletedDate {(task[‘CompletedDate’]).date()}, has CreatedDate {(task[‘CreatedDate’]).date()}, has IsReadDate {(task[‘IsReadDate’]).date()}, has CreatedFromParentID {task[‘CreatedFromParentID’]}, has CreatedFromParentType “{task[‘CreatedFromParentType’] }”, has DenySecurityLevelAccess {‘true’ if task[“DenySecurityLevelAccess”] else ‘false’}, has DeletedDate {(task[‘DeletedDate’]).date()}, has EntityLinkType “{task[‘EntityLinkType’]}”, has HasLinkedChildTasks {‘true’ if task[“HasLinkedChildTasks”] else ‘false’}, has IsApproved {‘true’ if task[“IsApproved”] else ‘false’}, has IsPersonal {‘true’ if task[“IsPersonal”] else ‘false’}, has MasterOfMultiAssignTo {‘true’ if task[“MasterOfMultiAssignTo”] else ‘false’}, has MasterTaskID {task[‘MasterTaskID’]}, has StartedDate {(task[‘StartedDate’]).date()}, has TaskStatus “{task[‘Status’]}”, has EndDate {(task[‘EndDate’]).date()}, has TimeSinceLastActivity {(timezone.now().date() - (task[‘ModifiedDate’]).date()).days}, has IsSecured {‘true’ if task[“IsSecured”] else ‘false’}, has AllSubTasksCompleted {‘true’ if task[“AllSubTasksCompleted”] else ‘false’}, has OriginOfTask “{task[‘OriginOfTask’]}”, has AssignedToPositionID {task[‘AssignedToPositionID’]}, has AssignedToWorkGroupID {task[‘AssignedToWorkGroupID’]};
match $t isa Task, has TaskID {task[‘TaskID’]}, has TaskDescription $td; insert $td has SentimentScore {ss};
match $c isa Account, has AccountID {creator[‘AccountID’]}; $a isa Account, has AccountID {assignTo[‘AccountID’]}; $t isa Task, has TaskID {task[‘TaskID’]}; insert $aaa (assigner: $c, assignee: $a, assigned: $t) isa assignment, has AssignedDate {(task[‘CreatedDate’]).date()}; ‘’’

I am able to use the above query via Grakn console, but when I use Python client, I receive the following exception:

Exception has occurred: GraknError
    Server/network error: <_Rendezvous of RPC that terminated with:
    status = StatusCode.UNKNOWN
    details = ". Please check server logs for the stack trace."
    debug_error_string = "{"created":"@1583573717.679000000","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1017,"grpc_message":". Please check server logs for the stack trace.","grpc_status":2}"
    > 
      File "….py", line 180, in <module>
        write_transaction.query(insertQuery)

Grakn log file:

[transaction-listener] ERROR g.c.s.r.SessionService$TransactionListener - Runtime Exception in RPC TransactionListener:
graql.lang.exception.GraqlException:
        at graql.lang.exception.GraqlException.create(GraqlException.java:50)
        at graql.lang.parser.Parser.parseQuery(Parser.java:115)
        at graql.lang.parser.Parser.parseQueryEOF(Parser.java:123)
        at graql.lang.Graql.parse(Graql.java:60)
        at grakn.core.server.rpc.SessionService$TransactionListener.query(SessionService.java:339)
        at grakn.core.server.rpc.SessionService$TransactionListener.handleRequest(SessionService.java:229)
        at grakn.core.server.rpc.SessionService$TransactionListener.lambda$onNext$1(SessionService.java:182)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748

I managed to sort it out by breaking the query down into three queries:

insertQuery = f'''insert  $t isa Task, has Title \"{task['Title']}\", has TaskID {task['TaskID']}, has TaskExternalID \"{task['TaskExternalID']}\", has TaskDescription \"{task['TaskDescription']}\", has StartDate {task['StartDate']}, has Duration {task['Duration']}, has IsRead {'true' if task["IsRead"] else 'false'}, has HasForms {'true' if task["HasForms"] else 'false'}, has CompanyID {task['CompanyID']}, has CompletedDate {(task['CompletedDate']).date()}, has CreatedDate {(task['CreatedDate']).date()},  has IsReadDate {(task['IsReadDate']).date()}, has CreatedFromParentID {task['CreatedFromParentID']}, has CreatedFromParentType \"{task['CreatedFromParentType'] }\", has DenySecurityLevelAccess {'true' if task["DenySecurityLevelAccess"] else 'false'}, has DeletedDate {(task['DeletedDate']).date()}, has EntityLinkType \"{task['EntityLinkType']}\", has HasLinkedChildTasks {'true' if task["HasLinkedChildTasks"] else 'false'}, has IsApproved {'true' if task["IsApproved"] else 'false'}, has IsPersonal {'true' if task["IsPersonal"] else 'false'},  has MasterOfMultiAssignTo {'true' if task["MasterOfMultiAssignTo"] else 'false'}, has MasterTaskID {task['MasterTaskID']}, has StartedDate {(task['StartedDate']).date()}, has TaskStatus \"{task['Status']}\", has EndDate {(task['EndDate']).date()}, has TimeSinceLastActivity {(timezone.now().date() - (task['ModifiedDate']).date()).days}, has IsSecured {'true' if task["IsSecured"] else 'false'}, has AllSubTasksCompleted {'true' if task["AllSubTasksCompleted"] else 'false'}, has OriginOfTask \"{task['OriginOfTask']}\", has AssignedToPositionID {task['AssignedToPositionID']}, has AssignedToWorkGroupID {task['AssignedToWorkGroupID']};'''

sentimentQuery = f'''match $t isa Task, has TaskID {task['TaskID']}, has TaskDescription $td; insert $td has SentimentScore {ss};'''

assignmentQuery = f'''match $c isa Account, has AccountID {creator['AccountID']}; $a isa Account, has AccountID {assignTo['AccountID']}; $t isa Task, has TaskID {task['TaskID']}; insert $aaa (assigner: $c, assignee: $a, assigned: $t) isa assignment, has AssignedDate {(task['CreatedDate']).date()}; '''

I was wondering why it behaves this way.

Hi - you can only execute a single statement (eg. match, insert, match...insert, match..delete, define or undefine) per query from the clients at this time.

The reason for this, and the reason that it might work in console and in gql files that are loaded via console, is that Java programs can import our native Graql parser (not yet available in other languages). The parser can then actually tokenise and recognise where each query starts and ends.

Hope that helps!

1 Like