欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JanusGraph实战笔记·数据写入·查询

程序员文章站 2024-01-19 13:34:16
...

Data Types·JanusGraph如何表示、写入和查询数组类型?

JanusGraph·How to represent, write or query an array in JanusGraph?

 

JanusGraph如何表示一个定点具有多个同名属性

JanusGraph定点的属性值不支持数组数据类型

collection的使用

If you are using Elasticsearch then you can index properties with SET and LIST cardinality. For instance:
 

JanusGraph·写入或导入数据·Load/Import/Write Data

 

 

以下Java函数中的codes在gremlin-server中当做

 

Collections(官方文档21.7)

If you are using Elasticsearch then you can index properties with SET and LIST cardinality(基数,不明白什意思). For instance:

mgmt = graph.openManagement()
nameProperty = mgmt.makePropertyKey("names").dataType(String.class).cardinality(Cardinality.SET).make() #加上.cardinality(Cardinality.SET)表示names的类型有String变为Set<String>吗?
mgmt.buildIndex("search", Vertex.class).addKey(nameProperty, Mapping.STRING.asParameter()).buildMixedIndex("search")
mgmt.commit()
//Insert a vertex
person = graph.addVertex()
person.property("names", "Robert")
person.property("names", "Bob")
graph.tx().commit()
//Now query it
g.V().has("names", "Bob").count().next() //1
g.V().has("names", "Robert").count().next() //1

 

Index

  •  

    参考

  • Indexing for Better Performance 使用索引加速性能

  • JanusGraph supports two different kinds of indexing to speed up query processing: graph indexes and vertex-centric indexes

  • Graph Index

    • The name of a graph index must be unique. 

       

       

      //建索引语句,提供索引名称、被索引的元素类型(Vertex.class、Edge.class)
      JanusGraphManagement.buildIndex(String:indexName, Class:className)  

       

    • 配置文件中强制开启索引,不然就会不使用索引
    • force-index =true

       

      • 这不是表示必须要使用索引吗? 不是“”“不然就不会使用索引”的意思吧

    • Composite Index

      •  Composite indexes are very fast and efficient but limited to equality lookups for a particular, previously-defined combination of property keys.

    • Mixed Index

      • Mixed indexes can be used for lookups on any combination of indexed keys and support multiple condition predicates in addition to equality depending on the backing index store.

 

query example

g.V().has("name", "hercules")
// 2) Find all vertices with an age greater than 50
g.V().has("age", gt(50))
// or find all vertices between 1000 (inclusive) and 5000 (exclusive) years of age and order by increasing age
g.V().has("age", inside(1000, 5000)).order().by("age", incr)
// which returns the same result set as the following query but in reverse order
g.V().has("age", inside(1000, 5000)).order().by("age", decr)
// 3) Find all edges where the place is at most 50 kilometers from the given latitude-longitude pair
g.E().has("place", geoWithin(Geoshape.circle(37.97, 23.72, 50)))
// 4) Find all edges where reason contains the word "loves"
g.E().has("reason", textContains("loves"))
// or all edges which contain two words (need to chunk into individual words)
g.E().has("reason", textContains("loves")).has("reason", textContains("breezes"))
// or all edges which contain words that start with "lov"
g.E().has("reason", textContainsPrefix("lov"))
// or all edges which contain words that match the regular expression "br[ez]*s" in their entirety
g.E().has("reason", textContainsRegex("br[ez]*s"))
// or all edges which contain words similar to "love"
g.E().has("reason", textContainsFuzzy("love"))
// 5) Find all vertices older than a thousand years and named "saturn"
g.V().has("age", gt(1000)).has("name", "saturn")

Java端的代码,略有不同,参见: 

  GraphTraversalSource g = graph.traversal();

        LinkedList<Long> times = new LinkedList<Long>();
        long time;
        Instant inst1;
        Instant inst2;

        //Query 1
        inst1 = Instant.now();
        List<Vertex> ret1 = g.V().has("type_object_type", "geography_mountain").toList();
        inst2 = Instant.now();
        time = Duration.between(inst1, inst2).toMillis();
        times.add(time);
        System.out.println("Query 1 used " + time + " ms. And query1 returns " + ret1.size() + " records.");

        //Query 2
        inst1 = Instant.now();
        List<Vertex> ret2 = g.V().has("type_object_name", "\"美国\"").toList();
        System.out.println("美国的records为" + ret2.size());
        inst2 = Instant.now();
        time = Duration.between(inst1, inst2).toMillis();
        times.add(time);
        System.out.println("Query 2 used " + time + " ms. And query2 returns " + ret2.size() + " records.");

        //Query 3
        inst1 = Instant.now();
        List<Vertex> ret3 = g.V().has("type_object_type", "geography_mountain")
                .has("geography_mountain_elevation", P.gt(1000)).toList();
        inst2 = Instant.now();
        time = Duration.between(inst1, inst2).toMillis();
        times.add(time);
        System.out.println("Query 3 used " + time + " ms. And query3 returns " + ret3.size() + " records.");

        //Query 4
        inst1 = Instant.now();

        List<Object> ret4 = g.V().has("type_object_type", "geography_mountain").order().values("type_object_name").toList();

        inst2 = Instant.now();
        time = Duration.between(inst1,inst2).toMillis();
        times.add(time);
        System.out.println("Query 4 used " + time + " ms. And query3 returns " + ret4.size() + " records.");

        //Query 5
        inst1 = Instant.now();
        List<Object> ret5 = g.V().has("name", "<http://knowledge.microsoft.com/5232ed96-85b1-2edb-12c6-63e6c597a1de>")
                .out("location_location_administrative_capital").has("type_object_alias").values("type_object_alias").toList();
        inst2 = Instant.now();
        time = Duration.between(inst1, inst2).toMillis();
        times.add(time);
        System.out.println("Query 5 used " + time + " ms. And query3 returns " + ret5.size() + " records.");

        //Query 6
        inst1 = Instant.now();
        List<Object> ret6 = g.V().has("type_object_type", "location_city").out("location_location_contained_by").
                out("location_location_contained_by").out("location_location_contained_by").
                values("location_location_area").limit(100)
                .toList();
        inst2 = Instant.now();
        time = Duration.between(inst1, inst2).toMillis();
        times.add(time);
        System.out.println("Query 6 used " + time + " ms. And query6 returns " + ret6.size() + " records.");


        return times;
相关标签: JanusGraph