0

我正在尝试加载使用 TinkerGraph.FileType.GRAPHML 类型保存的 tinkerpop 数据库。

这是我的代码:

public Graph getDatabase(String path) {
    org.apache.commons.configuration.Configuration conf = new BaseConfiguration();
    conf.setProperty("blueprints.tg.directory", path);
    conf.setProperty("blueprints.tg.file-type", "GRAPHML");
    conf.setProperty("blueprints.graph", "com.tinkerpop.blueprints.impls.tg.TinkerGraph");
    return GraphFactory.open(conf);
}

这段代码给了我一个 java.lang.RuntimeException:GraphFactory 无法实例化这个 Graph 实现 [com.tinkerpop.blueprints.impls.tg.TinkerGraph]。

原因是数字格式异常

Caused by: java.lang.NumberFormatException: For input string: "2306416072"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.valueOf(Integer.java:582)
at com.tinkerpop.blueprints.util.io.graphml.GraphMLReader.typeCastValue(GraphMLReader.java:313)
at com.tinkerpop.blueprints.util.io.graphml.GraphMLReader.inputGraph(GraphMLReader.java:252)
at com.tinkerpop.blueprints.util.io.graphml.GraphMLReader.inputGraph(GraphMLReader.java:116)
at com.tinkerpop.blueprints.impls.tg.TinkerStorageFactory$GraphMLTinkerStorage.loadGraphData(TinkerStorageFactory.java:187)
at com.tinkerpop.blueprints.impls.tg.TinkerStorageFactory$AbstractSeparateTinkerStorage.load(TinkerStorageFactory.java:94)
at com.tinkerpop.blueprints.impls.tg.TinkerGraph.init(TinkerGraph.java:134)
... 51 more

我在 xml 文件(数据库)中搜索了“2306416072”。这是一个属性 id <-data key="user_id"->2306416072</-/data->

不知道为什么我会收到数字格式异常。我在类 GraphMLReader 中导航,这就是方法

private static Object typeCastValue(String key, String value, Map<String, String> keyTypes) {
    String type = keyTypes.get(key);
    if (null == type || type.equals(GraphMLTokens.STRING))
         return value;
    else if (type.equals(GraphMLTokens.FLOAT)) 
         return Float.valueOf(value);
    else if (type.equals(GraphMLTokens.INT)) 
         return Integer.valueOf(value);
    else if (type.equals(GraphMLTokens.DOUBLE)) 
         return Double.valueOf(value);
    else if (type.equals(GraphMLTokens.BOOLEAN)) 
         return Boolean.valueOf(value);
    else if (type.equals(GraphMLTokens.LONG)) 
         return Long.valueOf(value);
    else 
        return value;
}
4

1 回答 1

0

Integer.MAX_VALUE是 2147483647(显然小于 2306416072)。我认为读者不是问题;要么写错了,要么你手动向你的 XML 文件添加了一些新元素而忘记更新类型定义。我认为是后者,因为我刚刚尝试了以下方法:

gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> g.addVertex(["num":123])
==>v[0]
gremlin> g.addVertex(["num":2306416072])
==>v[1]
gremlin> g.v(0).num.getClass()
==>class java.lang.Integer
gremlin> g.v(1).num.getClass()
==>class java.lang.Long
gremlin> g.saveGraphML("/tmp/numtest.xml")
==>null

gremlin> h = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> h.loadGraphML("/tmp/numtest.xml")
==>null
gremlin> h.V().map()
==>{num=2306416072}
==>{num=123}
gremlin> h.v(0).num.getClass()
==>class java.lang.Long
gremlin> h.v(1).num.getClass()
==>class java.lang.Long

XML 开头的类型定义是怎样的?对于上面创建的 numtest.xml,它是:

<key id="num" for="node" attr.name="num" attr.type="long"></key>

干杯,丹尼尔

于 2014-03-26T10:55:58.403 回答