Discussion Forum

Json migration using python

I have writen a script like the demo phone_calls, however, when I run the script on ubuntu 16.04, it returns the error, but it works well on Mac.

Loading from [../topic1] into Grakn ...
Executing Graql Query: insert $topic isa topic, has normalized_name "thisisericsson", has url "https://www.yammer.com/api/v1/topics/17404043264", has web_url "https://www.yammer.com/ericsson.com/topics/17404043264";
Traceback (most recent call last):
  File "/home/ck/.local/lib/python3.5/site-packages/grakn/service/Session/TransactionService.py", line 156, in send
    response = next(self._response_iterator)
  File "/home/ck/.local/lib/python3.5/site-packages/grpc/_channel.py", line 364, in __next__
    return self._next()
  File "/home/ck/.local/lib/python3.5/site-packages/grpc/_channel.py", line 358, in _next
    raise self
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
        status = StatusCode.INVALID_ARGUMENT
        details = "GraqlSemanticException-web_url doesn't have an 'isa', a 'sub' or an 'id'. Please check server logs for the stack trace."
        debug_error_string = "{"created":"@1566388593.785173000","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1017,"grpc_message":"GraqlSemanticException-web_url doesn't have an 'isa', a 'sub' or an 'id'. Please check server logs for the stack trace.","grpc_status":3}"
>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "load.py", line 57, in <module>
    build_topic_graph(inputs)
  File "load.py", line 10, in build_topic_graph
    load_data_into_grakn(input, session)
  File "load.py", line 19, in load_data_into_grakn
    transaction.query(graql_insert_query)
  File "/home/ck/.local/lib/python3.5/site-packages/grakn/client.py", line 131, in query
    return self._tx_service.query(query, infer)
  File "/home/ck/.local/lib/python3.5/site-packages/grakn/service/Session/TransactionService.py", line 49, in query
    response = self._communicator.send(request)
  File "/home/ck/.local/lib/python3.5/site-packages/grakn/service/Session/TransactionService.py", line 160, in send
    raise GraknError("Server/network error: {0}\n\n generated from request: {1}".format(e, request))
grakn.exception.GraknError.GraknError: Server/network error: <_Rendezvous of RPC that terminated with:
        status = StatusCode.INVALID_ARGUMENT
        details = "GraqlSemanticException-web_url doesn't have an 'isa', a 'sub' or an 'id'. Please check server logs for the stack trace."
        debug_error_string = "{"created":"@1566388593.785173000","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1017,"grpc_message":"GraqlSemanticException-web_url doesn't have an 'isa', a 'sub' or an 'id'. Please check server logs for the stack trace.","grpc_status":3}"
>

 generated from request: query_req {
  query: "insert $topic isa topic, has normalized_name \"thisisericsson\", has url \"https://www.yammer.com/api/v1/topics/17404043264\", has web_url \"https://www.yammer.com/ericsson.com/topics/17404043264\";"
}
from grakn.client import GraknClient
import ijson
import os

def build_topic_graph(inputs):
    with GraknClient(uri="localhost:48555") as client:
        with client.session(keyspace = "topic") as session:
            for input in inputs:
                print("Loading from [" + input["data_path"] + "] into Grakn ...")
                load_data_into_grakn(input, session)

def load_data_into_grakn(input, session):
    items = parse_data_to_dictionaries(input)
    for new in items:
        for item in new:
            with session.transaction().write() as transaction:
                graql_insert_query = input["template"](item)
                print("Executing Graql Query: " + graql_insert_query)
                transaction.query(graql_insert_query)
                transaction.commit()

    print("\nInserted " + str(len(items)) + " items from [ " + input["data_path"] + "] into Grakn.\n")

def parse_data_to_dictionaries(input):
    items = []
    with open(input["data_path"] + ".json") as data:
        objects = ijson.items(data, 'references.item')
        topics = (o for o in objects if o['type'] == 'topic')
        for topic in topics:
            list = []
            Dict = {}
            Dict['normalized_name'] = topic['normalized_name']
            # Dict['id'] = topic['id']
            Dict['url'] = topic['url']
            Dict['web_url'] = topic['web_url']
            list.append(Dict)
            items.append(list)
    return items

def topic_template(topic):
    graql_insert_query = 'insert $topic isa topic, has normalized_name "' + topic["normalized_name"] + '"'
    # graql_insert_query += ', has id "' + str(topic["id"]) + '"'
    graql_insert_query += ', has url "' + topic["url"] + '"'
    graql_insert_query += ', has web_url "' + topic["web_url"] + '"'
    graql_insert_query += ";"

    return graql_insert_query


inputs = [
    {
        "data_path": os.getcwd() + "/topic1",
        "template": topic_template
    }
]

build_topic_graph(inputs)

Hi @suoyita,

it looks like you have not defined any schema before running the migration:

web_url doesn't have an 'isa', a 'sub' or an 'id'. Please check server logs for the stack trace.

Make sure to read this part as well: https://dev.grakn.ai/docs/examples/phone-calls-schema

After you define and commit you schema, all the data should be migrated correctly