python - 使用 Grakn 的 Python API 客户端加载数据时出现问题

标签 python vaticle-typedb vaticle-typeql knowledge-graph

我正在使用 Grakn 1.8.1、python 3.6.8、grakn-client 1.8.1 和 grpcio 1.24.1。

我定义了一个简单的模式 (schema.gql) 只是为了执行测试:

define

nome sub attribute,
    value string;

cpf_srfb sub attribute,
    value string;

id_srfb sub attribute,
    value string;

hops_de_contraparte sub attribute,
    value string;

pessoa_fisica sub entity,
    has nome,
    has cpf_srfb,
    has id_srfb,
    has hops_de_contraparte;

此方案已使用 grakn 控制台正确加载, key 空间称为“srfb”。

之后,我创建了一个只包含一个实例的数据文件(data.sql):

insert $pf isa pessoa_fisica, has nome "GUSTAVO RIBEIRO SOARES PINTO", has cpf_srfb "***106887**", has id_srfb "***106887**GUSTAVO RIBEIRO SOARES PINTO", has hops_de_contraparte "5";

同样,数据加载使用 grakn-console 完美运行。

当我通过控制台加载模式然后尝试通过 python api 加载数据时出现问题。

我的 python 脚本是:

from grakn.client import GraknClient

uri = "localhost:48555"
keyspace = 'srfb'
filepath = 'C:\grakn\grakn_181\data.gql'

with open(filepath, mode='r') as file:
    insert_query = file.read()

with GraknClient(uri) as client:
    with client.session(keyspace) as session:
        with session.transaction().write() as transaction:
            transaction.query(insert_query)
            transaction.commit()

在那一刻,发生了一个我无法纠正的错误:

Traceback (most recent call last):
  File "C:\Users\jorge\AppData\Local\Programs\Python\Python36\lib\site-packages\grakn\service\Session\util\Communicator.py", line 166, in _block_for_next
    response = next(self._response_iterator)
  File "C:\Users\jorge\AppData\Local\Programs\Python\Python36\lib\site-packages\grpc\_channel.py", line 388, in __next__
    return self._next()
  File "C:\Users\jorge\AppData\Local\Programs\Python\Python36\lib\site-packages\grpc\_channel.py", line 382, in _next
    raise self
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
        status = StatusCode.UNKNOWN
        details = "grakn.core.kb.graql.exception.GraqlSemanticException: hops_de_contraparte doesn't have an 'isa', a 'sub' or an 'id'. Please check server logs for the stack trace."
        debug_error_string = "{"created":"@1603911191.241000000","description":"Error received from peer ipv6:[::1]:48555","file":"src/core/lib/surface/call.cc","file_line":1055,"grpc_message":"grakn.core.kb.graql.exception.GraqlSemanticException: hops_de_contraparte doesn't have an 'isa', a 'sub' or an 'id'. Please check server logs for the stack trace.","grpc_status":2}"
>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "teste.py", line 17, in <module>
    transaction.commit()
  File "C:\Users\jorge\AppData\Local\Programs\Python\Python36\lib\site-packages\grakn\client.py", line 160, in commit
    self._tx_service.commit()
  File "C:\Users\jorge\AppData\Local\Programs\Python\Python36\lib\site-packages\grakn\service\Session\TransactionService.py", line 54, in commit
    self._communicator.single_request(request)
  File "C:\Users\jorge\AppData\Local\Programs\Python\Python36\lib\site-packages\grakn\service\Session\util\Communicator.py", line 192, in single_request
    return SingleResolver(self, request).get()
  File "C:\Users\jorge\AppData\Local\Programs\Python\Python36\lib\site-packages\grakn\service\Session\util\Communicator.py", line 70, in get
    response = self._communicator._block_for_next(self)
  File "C:\Users\jorge\AppData\Local\Programs\Python\Python36\lib\site-packages\grakn\service\Session\util\Communicator.py", line 184, in _block_for_next
    raise self._error
grakn.exception.GraknError.GraknError: Server/network error: <_Rendezvous of RPC that terminated with:
        status = StatusCode.UNKNOWN
        details = "grakn.core.kb.graql.exception.GraqlSemanticException: hops_de_contraparte doesn't have an 'isa', a 'sub' or an 'id'. Please check server logs for the stack trace."
        debug_error_string = "{"created":"@1603911191.241000000","description":"Error received from peer ipv6:[::1]:48555","file":"src/core/lib/surface/call.cc","file_line":1055,"grpc_message":"grakn.core.kb.graql.exception.GraqlSemanticException: hops_de_contraparte doesn't have an 'isa', a 'sub' or an 'id'. Please check server logs for the stack trace.","grpc_status":2}"
>

 generated from request: iter_req {
  options {
  }
  query_iter_req {
    query: "insert $pf isa pessoa_fisica, has nome \"GUSTAVO RIBEIRO SOARES PINTO\", has cpf_srfb \"***106887**\", has id_srfb \"***106887**GUSTAVO RIBEIRO SOARES PINTO\", has hops_de_contraparte \"5\";"
    options {
    }
  }
}


grakn 日志呈现如下记录:

2020-10-28 15:15:45,615 [grpc-request-handler-3] ERROR g.c.s.r.SessionService$TransactionListener - Runtime Exception in RPC TransactionListener: 
java.util.concurrent.ExecutionException: grakn.core.kb.graql.exception.GraqlSemanticException: hops_de_contraparte doesn't have an 'isa', a 'sub' or an 'id'
    at java.util.concurrent.FutureTask.report(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at grakn.core.server.rpc.SessionService$TransactionListener.process(SessionService.java:319)
    at grakn.core.server.rpc.SessionService$TransactionListener.onNext(SessionService.java:196)
    at grakn.core.server.rpc.SessionService$TransactionListener.onNext(SessionService.java:156)
    at io.grpc.stub.ServerCalls$StreamingServerCallHandler$StreamingServerCallListener.onMessage(ServerCalls.java:251)
    at io.grpc.internal.ServerCallImpl$ServerStreamListenerImpl.messagesAvailableInternal(ServerCallImpl.java:309)
    at io.grpc.internal.ServerCallImpl$ServerStreamListenerImpl.messagesAvailable(ServerCallImpl.java:292)
    at io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$1MessagesAvailable.runInContext(ServerImpl.java:779)
    at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
    at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: grakn.core.kb.graql.exception.GraqlSemanticException: hops_de_contraparte doesn't have an 'isa', a 'sub' or an 'id'

最初我认为属性的声明有问题,但事实并非如此,因为相同的插入通过控制台完美运行。

我该怎么做才能使用 python api?有谁知道我可能做错了什么?

最佳答案

正如您已经知道的那样,所有内容都通过控制台正确加载,您没有做任何明显的错误!

这条消息:

hops_de_contraparte doesn't have an 'isa', a 'sub' or an 'id'

是您从 python 客户端访问的键空间没有 hops_de_contraparte 定义为类型的明确标志。您可以从控制台访问与从客户端访问不同的 key 空间。因此,请仔细检查在使用控制台加载架构时是否使用 -k 参数来提供键空间,例如:.\grakn console -k srfb -f 'C:\grakn\grakn_181\schema.gql 以便它与您用于客户端的键空间相匹配。

如果失败,请尝试更改您所做的查询以删除 hops_de_contraparte 以查看它是否是未定义的特定类型。按照 Banjo 的建议进行操作,并尝试首先从 Python 字符串传递查询。

关于python - 使用 Grakn 的 Python API 客户端加载数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64579611/

相关文章:

vaticle-typedb - 如何在graql中定义传递关系

python - 将数据插入 grakn 模式

vaticle-typedb - 在 n 元关系中指定动态角色数

python - 有人知道如何修复 unicode 错误吗?

Python正则表达式多行括号之间的所有内容

vaticle-typedb - 格拉克 : how can I construct a knowledge graph from a collection of texts?

attributes - Grakn - 在关系中扮演角色的属性与具有属性的关系

python - mypy: "__eq__"与父类(super class)型 "object"不兼容

python - 将父类的自身更改为子类

model - 如何使用 Grakn/Graql 建模角色层次结构(以及它们之间的关系)?