首页 文章

如何在titan graph 1.0.0中添加TitanVertext的属性

提问于
浏览
1

我正在使用泰坦1.0.0-hadoop1 . 我正在尝试将一些属性列表添加到我正在创建的Vertex中 . 在早期版本(如0.5.4)中,您可以使用setProperty直接添加属性,但在最新的API中,我发现很难添加属性 . 我甚至无法在互联网上找到正确的解决方案 .

请帮我在Titan Java API中将属性添加到Vertex .

2 回答

  • 1

    一个例子将有助于:

    Vertex vertex = graph.addVertex();
    vertex.property("ID", "123"); //Creates ID property with value 123
    

    创造 property . 要查询属性:

    vertex.property("ID"); //Returns the property object
    vertex.value("ID");    //Returns "123"
    vertex.values();       //Returns all the values of all the properties
    

    当您难以理解Titan API时 . 我建议看TinkerPop API . Titan实现了它,所以所有tinkerpop命令都适用于titan图形 .

  • 0

    我也在使用带有cassandra存储后端的 titan 1.0.0 图形数据库,从0.5.4版本升级后也遇到了同样的问题 . 我发现使用此方法将任何 Collection 对象( SetList )添加到顶点属性的简单通用解决方案 .

    public static void setMultiElementProperties(TitanElement element, String key, Collection collection) {
        if (element != null && key != null && collection != null) {
            // Put item from collection to the property of type Cardinality.LIST or Cardinality.SET
            for (Object item : collection) {
                if (item != null)
                    element.property(key, item);
            }
        }
    }
    

    Java 8 syntacs相同的方法实现:

    public static void setMultiElementProperties(TitanElement element, String key, Collection collection) {
        if (element != null && key != null && collection != null) {
            // Put item from collection to the property of type Cardinality.LIST or Cardinality.SET
            collection.stream().filter(item -> item != null).forEach(item -> element.property(key, item));
        }
    }
    

    TitanElement object是 TitanVertexTitanEdge 对象的父对象,因此您可以将顶点或边传递给此方法 . 当然,您需要首先使用 Cardinality.SetCardinality.List 使用TitanManagement声明元素属性以使用多值属性 .

    TitanManagement tm = tittanGraph.openManagement();
    tm.makePropertyKey(key).cardinality(Cardinality.LIST).make();// or Cardinality.SET
    tm.commit();
    

    要从element属性中检索集合,您可以简单地使用:

    Iterator<Object> collectionIter = element.values(key);
    

    这是Java 8迭代它的方法:

    List<Object> myList = new ArrayList<>();
    collectionIter.forEachRemaining(item -> myList.add(item));
    

相关问题