【问题标题】:Tinkerpop Frames writing to database - newbieTinkerpop Frames 写入数据库 - 新手
【发布时间】:2014-05-24 18:52:48
【问题描述】:

我第一次使用 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)

非常感谢,

约翰

【问题讨论】:

    标签: tinkerpop


    【解决方案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()

    【讨论】:

    • 非常感谢斯蒂芬。我的想法是否正确,因此不可能通过接口向图形添加顶点,并且您需要使用这种代码来执行此操作:Graph graph = new Neo4jGraph("/tmp/my_graph");顶点 a = graph.addVertex(null);顶点 b = graph.addVertex(null); a.setProperty("名称","marko"); b.setProperty("姓名","彼得");边 e = graph.addEdge(null, a, b, "知道"); e.setProperty("自", 2006); graph.shutdown();
    • 我没有完全关注你。如果您想添加新的Person,请执行以下操作:Person p = g.addVertex(null, Person.class);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    相关资源
    最近更新 更多