美文网首页
图形数据库neo4j试玩

图形数据库neo4j试玩

作者: bobcorbett | 来源:发表于2017-08-15 16:08 被阅读0次

    neo4j是一套noSQL的图形数据库,适合存储点、线、面等图数据结构。
    基于Java开发,缺点是占用大量内存。
    适配Java、Python、ruby等语言。
    适用场景:
    Social Network(社交网络,企业用户比如Linkedin)
    Network and IT Operation(网络或者运维)
    Fraud Detection (反诈骗)
    Graph Based Search(图算法,类似于我之前研究的拓扑算法,ebay把这个应用在快递分发系统)
    Identity and Access(登陆信息验证,挪威电信Telenor公司在用,我的前东家甲方公司)
    Master Data Management(主数据管理,这个就不太清楚了,pitney bowes供应链公司)
    Recommendation Engine(商品评论引擎,Walmart德国沃尔玛等电商系统)

    官网地址:https://neo4j.com/

    Windows环境下载exe安装,用的是免费社区版

    启动服务,简单的Java Swing程序

    登陆试玩,引导界面还是蛮不错的:

    用了官方的一套demo:

    import org.neo4j.driver.v1.*;
     
    import java.util.Collections;
    import java.util.List;
     
    import static java.util.Arrays.asList;
    import static org.neo4j.driver.v1.Values.parameters;
     
    public class Social {
     
        public static void main(String...args) {
     
            Config noSSL = Config.build().withEncryptionLevel(Config.EncryptionLevel.NONE).toConfig();
            Driver driver = GraphDatabase.driver("bolt://localhost",AuthTokens.basic("neo4j","123456"),noSSL); // <password>
            try (Session session = driver.session()) {
     
                List data =
                        asList(asList("Jim","Mike"),asList("Jim","Billy"),asList("Anna","Jim"),
                                asList("Anna","Mike"),asList("Sally","Anna"),asList("Joe","Sally"),
                                asList("Joe","Bob"),asList("Bob","Sally"));
     
                String insertQuery = "UNWIND {pairs} as pair " +
                        "MERGE (p1:Person {name:pair[0]}) " +
                        "MERGE (p2:Person {name:pair[1]}) " +
                        "MERGE (p1)-[:KNOWS]-(p2);";
     
                session.run(insertQuery, Collections.<String, Object>singletonMap("pairs",data)).consume();
     
                StatementResult result;
     
                String foafQuery =
                        " MATCH (person:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf) "+
                                " WHERE person.name = {name} " +
                                "   AND NOT (person)-[:KNOWS]-(foaf) " +
                                " RETURN foaf.name AS name ";
                result = session.run(foafQuery, parameters("name","Joe"));
                while (result.hasNext()) System.out.println(result.next().get("name"));
     
                String commonFriendsQuery =
                        "MATCH (user:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf:Person) " +
                                " WHERE user.name = {from} AND foaf.name = {to} " +
                                " RETURN friend.name AS friend";
                result = session.run(commonFriendsQuery, parameters("from","Joe","to","Sally"));
                while (result.hasNext()) System.out.println(result.next().get("friend"));
     
                String connectingPathsQuery =
                        "MATCH path = shortestPath((p1:Person)-[:KNOWS*..6]-(p2:Person)) " +
                                " WHERE p1.name = {from} AND p2.name = {to} " +
                                " RETURN [n IN nodes(path) | n.name] as names";
                result = session.run(connectingPathsQuery, parameters("from","Joe","to","Billy"));
                while (result.hasNext()) System.out.println(result.next().get("names"));
     
            }
        }
    }
    
    MAVEN地址
    

    图数据库相关书籍
    《图数据库(第2版)》

    相关文章

      网友评论

          本文标题:图形数据库neo4j试玩

          本文链接:https://www.haomeiwen.com/subject/sakvrxtx.html