使用 Neo4j Java API 属性 容器
Using Neo4j Java API property Container
我正在尝试更新 Gephi 的 apoc 插件,以便我可以将权重从 Neo4j 发送到 Gephi。这是原始版本of the plugin。我是怎么想的,我可以轻松地重新排列
private Map<String, Object> data(PropertyContainer pc, Map<String, Map<String, Object>> colors) {
if (pc instanceof Node) {
Node n = (Node) pc;
String labels = Util.labelString(n);
Map<String, Object> attributes = map("label", caption(n), "TYPE", labels);
attributes.putAll(positions());
attributes.putAll(color(labels,colors));
return map(idStr(n), attributes);
}
if (pc instanceof Relationship) {
Relationship r = (Relationship) pc;
String type = r.getType().name();
Map<String, Object> attributes = map("label", type, "TYPE", type);
attributes.putAll(map("source", idStr(r.getStartNode()), "target", idStr(r.getEndNode()), "directed", true));
attributes.putAll(color(type, colors));
return map(String.valueOf(r.getId()), attributes);
}
return map();
}
像这样,我会改变关系的解析方式,并为 return
添加权重
private Map<String, Object> data(PropertyContainer pc, Map<String, Map<String, Object>> colors) {
if (pc instanceof Node) {
Node n = (Node) pc;
String labels = Util.labelString(n);
Map<String, Object> attributes = map("label", caption(n), "TYPE", labels);
attributes.putAll(positions());
attributes.putAll(color(labels,colors));
return map(idStr(n), attributes);
}
if (pc instanceof Relationship) {
Relationship r = (Relationship) pc;
String type = r.getType().name();
String weight = r.getProperty("weight");
Map<String, Object> attributes = map("label", type, "TYPE", type);
attributes.putAll(map("source", idStr(r.getStartNode()), "target", idStr(r.getEndNode()), "directed", false,"weight",weight));
attributes.putAll(color(type, colors));
return map(String.valueOf(r.getId()), attributes);
}
return map();
}
现在问题出现了,我是一个 JAVA 菜鸟,不知道如何与 JAVA 数据类型交互。我找到了这个 documentation on property container,在那里我找到了 getProperty(String key)
方法,并且我尝试使用它,就像上面一样但是得到了下一个错误
/Users/Hal/Job/neo4j-apoc-procedures/src/main/java/apoc/gephi/Gephi.java:81:
error: incompatible types: Object cannot be converted to String
String weight = r.getProperty("weight");
我认为这是一些基本的 JAVA 问题,但我不知道如何调试它。我也试过 String weight = r.getProperty(weight);
但得到了同样的错误。帮助赞赏
改为尝试Object weight = r.getProperty("weight");
文档说 getProperty
returns 对象。
字符串是一个对象。但是对象不是(必然)字符串。
我正在尝试更新 Gephi 的 apoc 插件,以便我可以将权重从 Neo4j 发送到 Gephi。这是原始版本of the plugin。我是怎么想的,我可以轻松地重新排列
private Map<String, Object> data(PropertyContainer pc, Map<String, Map<String, Object>> colors) {
if (pc instanceof Node) {
Node n = (Node) pc;
String labels = Util.labelString(n);
Map<String, Object> attributes = map("label", caption(n), "TYPE", labels);
attributes.putAll(positions());
attributes.putAll(color(labels,colors));
return map(idStr(n), attributes);
}
if (pc instanceof Relationship) {
Relationship r = (Relationship) pc;
String type = r.getType().name();
Map<String, Object> attributes = map("label", type, "TYPE", type);
attributes.putAll(map("source", idStr(r.getStartNode()), "target", idStr(r.getEndNode()), "directed", true));
attributes.putAll(color(type, colors));
return map(String.valueOf(r.getId()), attributes);
}
return map();
}
像这样,我会改变关系的解析方式,并为 return
添加权重private Map<String, Object> data(PropertyContainer pc, Map<String, Map<String, Object>> colors) {
if (pc instanceof Node) {
Node n = (Node) pc;
String labels = Util.labelString(n);
Map<String, Object> attributes = map("label", caption(n), "TYPE", labels);
attributes.putAll(positions());
attributes.putAll(color(labels,colors));
return map(idStr(n), attributes);
}
if (pc instanceof Relationship) {
Relationship r = (Relationship) pc;
String type = r.getType().name();
String weight = r.getProperty("weight");
Map<String, Object> attributes = map("label", type, "TYPE", type);
attributes.putAll(map("source", idStr(r.getStartNode()), "target", idStr(r.getEndNode()), "directed", false,"weight",weight));
attributes.putAll(color(type, colors));
return map(String.valueOf(r.getId()), attributes);
}
return map();
}
现在问题出现了,我是一个 JAVA 菜鸟,不知道如何与 JAVA 数据类型交互。我找到了这个 documentation on property container,在那里我找到了 getProperty(String key)
方法,并且我尝试使用它,就像上面一样但是得到了下一个错误
/Users/Hal/Job/neo4j-apoc-procedures/src/main/java/apoc/gephi/Gephi.java:81: error: incompatible types: Object cannot be converted to String String weight = r.getProperty("weight");
我认为这是一些基本的 JAVA 问题,但我不知道如何调试它。我也试过 String weight = r.getProperty(weight);
但得到了同样的错误。帮助赞赏
改为尝试Object weight = r.getProperty("weight");
文档说 getProperty
returns 对象。
字符串是一个对象。但是对象不是(必然)字符串。