Neo4j从扩展程序返回嵌套对象

时间:2018-05-31 09:29:26

标签: neo4j

我正在尝试为Neo4j创建扩展程序,它将返回复杂的Object(意思是另一个对象的对象)。

public static class A {
    public final String a;

    public A(String a) {
        this.a = a;
    }
}

public static class Output {
    public Object out;

    public Output(Object out) {
        this.out = out;
    }
}

@Procedure(value = "my_proc", mode = Mode.READ)
public Stream<Output> myProc() {
    return Stream.of(new Output(new A("a")));
}

当我使用Neo4j浏览器执行call my_proc();时,它只显示进度循环并且永不返回。 当我使用Java驱动程序执行相同的操作时,我遇到以下异常:

SEVERE: [0xedd70cbd] Fatal error occurred in the pipeline
org.neo4j.driver.internal.shaded.io.netty.handler.codec.DecoderException: Failed to read inbound message:
   at org.neo4j.driver.internal.async.inbound.InboundMessageHandler.channelRead0(InboundMessageHandler.java:87)
   at org.neo4j.driver.internal.async.inbound.InboundMessageHandler.channelRead0(InboundMessageHandler.java:35)
   ....
   at org.neo4j.driver.internal.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645)
   at org.neo4j.driver.internal.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)
   at org.neo4j.driver.internal.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)
   at org.neo4j.driver.internal.shaded.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)
   at org.neo4j.driver.internal.shaded.io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
   at org.neo4j.driver.internal.shaded.io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)
   at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(49) + length(1) exceeds writerIndex(49): PooledDuplicatedByteBuf(ridx: 49, widx: 49, cap: 133, unwrapped: PooledUnsafeDirectByteBuf(ridx: 51, widx: 89, cap: 133))
    at org.neo4j.driver.internal.shaded.io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1401)
    at org.neo4j.driver.internal.shaded.io.netty.buffer.AbstractByteBuf.readByte(AbstractByteBuf.java:707)
    at org.neo4j.driver.internal.async.inbound.ByteBufInput.readByte(ByteBufInput.java:45)
    at org.neo4j.driver.internal.packstream.PackStream$Unpacker.unpackLong(PackStream.java:479)
    at org.neo4j.driver.internal.messaging.PackStreamMessageFormatV1$Reader.unpackValue(PackStreamMessageFormatV1.java:479)
    at org.neo4j.driver.internal.messaging.PackStreamMessageFormatV1$Reader.unpackRecordMessage(PackStreamMessageFormatV1.java:464)
    at org.neo4j.driver.internal.messaging.PackStreamMessageFormatV1$Reader.read(PackStreamMessageFormatV1.java:390)
    at org.neo4j.driver.internal.async.inbound.InboundMessageHandler.channelRead0(InboundMessageHandler.java:83)
    ... 39 more

有没有办法在返回嵌套对象之前将其序列化为json?

1 个答案:

答案 0 :(得分:0)

@Procedure(value = "ebc.neo4j.justamap", mode = Mode.READ)
public Stream<MapResult> justamap() {
    HashMap<String, Object> v1Map = new HashMap<String, Object>(1);
    HashMap<String, Object> v2Map = new HashMap<String, Object>(1);
    v2Map.put("a", "a string");
    v1Map.put("map inside", v2Map);

    return Stream.of(new MapResult(v1Map));
}


public static class MapResult {
    public Map internalmap;

    public MapResult(Map aInternalId) {
        this.internalmap = aInternalId;
    }
}

像上面这样的东西是可能的(尽可能深)但是返回要在Cypher中使用的自定义对象(这是你在编写程序时的想法)不会起作用,因为Cypher不知道这些自定义对象是什么。所以你必须做一些消毒

希望这有帮助。

此致 汤姆