Tinkerpop框架写入数据库 - 新手

时间:2014-05-24 18:52:49

标签: tinkerpop

我第一次使用Frames并且我的Java非常生疏。我坚持通过Frames将信息写入数据库。我一直在关注文档并拥有Person界面。

public interface Person {
  @Property("name")
  public String getName();

  @Adjacency(label="knows")
  public Iterable<Person> getKnowsPeople();

  @Adjacency(label="knows")
  public void addKnowsPerson(final Person person);

  @GremlinGroovy("it.out('knows').out('knows').dedup") //Make sure you use the GremlinGroovy module! #1
  public Iterable<Person> getFriendsOfAFriend()
}

哪个来自文档。 我可以使用这个简单的代码从图中获取数据。

TinkerGraph graph = TinkerGraphFactory.createTinkerGraph(); //This graph is pre-populated.
FramedGraphFactory factory = new FramedGraphFactory(new GremlinGroovyModule()); //(1) Factories should be reused for performance and memory conservation.

FramedGraph framedGraph = factory.create(graph); //Frame the graph.

Person person = framedGraph.getVertex(1, Person.class);
person.getName(); // equals "marko" 

我想知道的是如何创建一个新的Person对象并将其写入图形。因为Person只是一个我无法做到的界面:

Person person2 = new Person();
person2.setName("John");
person2.setAge(36);
framedGraph.addVertex(person2);

所以我尝试了一个实现Person的PersonImpl类,并添加了以下代码

PersonImpl johnBoy = new PersonImpl();
johnBoy.setName("John");
johnBoy.setAge(36);
johnBoy.addKnowsPerson(person);
person.addKnowsPerson(johnBoy);

然而,我得到了以下NullPointer,而我现在真的陷入困境。我希望有人可以帮助我。

Exception in thread "main" java.lang.NullPointerException
    at com.tinkerpop.blueprints.impls.tg.TinkerGraph.addEdge(TinkerGraph.java:331)
    at com.tinkerpop.frames.FramedGraph.addEdge(FramedGraph.java:310)
    at com.tinkerpop.frames.annotations.AdjacencyAnnotationHandler.addEdges(AdjacencyAnnotationHandler.java:87)
    at com.tinkerpop.frames.annotations.AdjacencyAnnotationHandler.processVertex(AdjacencyAnnotationHandler.java:53)
    at com.tinkerpop.frames.annotations.AdjacencyAnnotationHandler.processElement(AdjacencyAnnotationHandler.java:26)
    at com.tinkerpop.frames.annotations.AdjacencyAnnotationHandler.processElement(AdjacencyAnnotationHandler.java:15)
    at com.tinkerpop.frames.FramedElement.invoke(FramedElement.java:89)
    at com.sun.proxy.$Proxy4.addKnowsPerson(Unknown Source)
    at com.elecrticdataland.utility.TinkerTest.main(TinkerTest.java:45)

非常感谢,

约翰

1 个答案:

答案 0 :(得分:1)

除代理方式外,您无法创建Person。换句话说,您不能使用该接口的具体实现,它必须由FramesGraph动态构造。

您可以在此处创建Person代码:

FramedGraph framedGraph = factory.create(graph); //Frame the graph.

Person person = framedGraph.getVertex(1, Person.class);
person.getName(); // equals "marko" 

如果没有这个,创建的Person实现将不会知道有关Graph

的基础和注入factory.create()实例的任何信息