在Titan数据库中使用kryo序列化对象

时间:2014-07-10 14:45:14

标签: java titan kryo

我会在Titan数据库中序列化一个基本对象。但不起作用。我想序列化我的对象在我的titan数据库中注册它。但我不明白为什么会出现这个问题。我发布了我的源代码,如果你有文档,我很高兴接受。

通常我会创建我的对象:

public class Attribute implements KryoSerializable {

    private String typeunit;

    private Object value;

    private String valueS;


    public Attribute() {
        this.typeunit = "";
        this.value = null;
        this.valueS = "";
    }


    public void setValue(String type, Object value) {
        this.typeunit = type;
        this.value = value;

        try {
            if(value instanceof java.lang.String) {
                this.valueS = (String) value;
            }
        } catch(Exception e) {

        }
    }

    public Object getValue() {
        return value;
    }

    public String getValueS() {
        return valueS;
    }

    public String getTypeunit() {
        return typeunit;
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        return super.equals(obj);
    }

    public void read(Kryo kryo, Input input) {
        this.typeunit = input.readString();
        this.value = kryo.readObject(input, Object.class);
        this.valueS = input.readString();
    }

    public void write(Kryo kryo, Output output) {
        kryo.register(Object.class);

        output.writeString(this.typeunit);
        kryo.writeObject(output, this.value);
        output.writeString(this.valueS);
    }

}

在我尝试之后:

Vertex r = this.model.addVertex(null);
r.setProperty("uuid", UUID.randomUUID().toString());
r.setProperty("object", attr);

this.model.commit();

for(Vertex vertex : this.model.query().vertices()) { 
     Attribute test = vertex.getProperty("object");
     System.out.println(test.getTypeunit());
}

但我有一个例外:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
    at java.util.ArrayList.elementData(ArrayList.java:400)
    at java.util.ArrayList.get(ArrayList.java:413)
    at com.esotericsoftware.kryo.util.MapReferenceResolver.getReadObject(MapReferenceResolver.java:42)
    at com.esotericsoftware.kryo.Kryo.readReferenceOrNull(Kryo.java:773)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:624)
    at XXXX.XXXXXX.Attribute.read(Attribute.java:140)
    at com.esotericsoftware.kryo.serializers.DefaultSerializers$KryoSerializableSerializer.read(DefaultSerializers.java:363)
    at com.esotericsoftware.kryo.serializers.DefaultSerializers$KryoSerializableSerializer.read(DefaultSerializers.java:355)
    at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:729)
    at com.thinkaurelius.titan.graphdb.database.serialize.kryo.KryoSerializer.readClassAndObject(KryoSerializer.java:119)
    at com.thinkaurelius.titan.graphdb.database.EdgeSerializer.parseRelation(EdgeSerializer.java:211)
    at com.thinkaurelius.titan.graphdb.database.EdgeSerializer.readRelation(EdgeSerializer.java:119)
    at com.thinkaurelius.titan.graphdb.database.EdgeSerializer.readRelation(EdgeSerializer.java:59)
    at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx$4$3.apply(StandardTitanTx.java:780)
    at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx$4$3.apply(StandardTitanTx.java:777)
    at com.google.common.collect.Iterators$8.transform(Iterators.java:860)
    at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
    at com.thinkaurelius.titan.graphdb.query.LimitAdjustingIterator.next(LimitAdjustingIterator.java:55)
    at com.thinkaurelius.titan.graphdb.query.QueryProcessor$OuterIterator.nextInternal(QueryProcessor.java:76)
    at com.thinkaurelius.titan.graphdb.query.QueryProcessor$OuterIterator.<init>(QueryProcessor.java:65)
    at com.thinkaurelius.titan.graphdb.query.QueryProcessor.iterator(QueryProcessor.java:46)
    at com.thinkaurelius.titan.graphdb.vertices.AbstractVertex.getProperty(AbstractVertex.java:105)
    at com.thinkaurelius.titan.graphdb.vertices.AbstractVertex.getProperty(AbstractVertex.java:121)

你能帮我吗?

谢谢你。

2 个答案:

答案 0 :(得分:1)

请查看有关如何定义自定义序列化程序的文档:

https://github.com/thinkaurelius/titan/wiki/Datatype-and-Attribute-Serializer-Configuration

这可能需要配置选项以及序列化程序的要求听起来你现在还没有很多这样的选项。

答案 1 :(得分:0)

作为@stephen mallette答案的补充:

Titan使用了许多自己的自定义序列化程序,因此您需要在配置中指定足够大的数字,例如attributes.attribute20 = ...请注意,索引周围没有方括号(这就是为什么你得到NumberFormatException)。