Tinkerpop Frames writing to database - newbie

2019-05-10 01:07发布

问题:

I'm having my first go with Frames and my Java is pretty rusty. I'm stuck on writing information through Frames into the database. I've been following the docs and have a Person interface.

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()
}

Which is taken from the docs. I can use this simple code to get data out of the graph.

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" 

What I'd like to know is how I would create a new Person object and write it to the graph. Because Person is only an interface I can't do:

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

So I've tried a PersonImpl class which implements Person and added the following code

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

However I'm getting the following NullPointer and I'm now really stuck. I was hoping someone might possibly be able to help me.

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)

With many thanks,

John

回答1:

You can't create a Person except by way of proxy. In other words, you can't use a concrete implementation of that interface, it has to be constructed dynamically by the FramesGraph.

You have the code to create a Person here:

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

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

Without that, the created Person implementation will not know anything about the underlying and injected Graph instance given to factory.create()



标签: tinkerpop