我正在尝试加载使用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;
}
答案 0 :(得分: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>
干杯, 丹尼尔