【问题标题】:Integrating mongodb with neo4j, is there any API that will link them?将 mongodb 与 neo4j 集成,是否有任何 API 可以链接它们?
【发布时间】:2013-03-18 13:23:01
【问题描述】:

我正在开发推荐引擎。收集用户数据(他们的友谊、位置、喜欢、教育......)并且已经存储在 mongodb 中。我需要向这些用户推荐相关产品。出于显而易见的原因(节点之间的轻松遍历、路径信息……),我计划将 neo4j 用于推荐引擎。问题是我必须首先将 mongodb 数据转换为 neo4j 节点/关系,处理数据并将结果发送回 mongodb 数据库。主要问题是我们最终会维护两个数据库,这让开发团队很不高兴。我已经查看了mongodb-neo4jspring data 的类似帖子,但不确定如何解决这个问题。 这些是我的问题
1- 仅仅为了推荐引擎而添加另一个数据库是否值得(我们正在处理一个大型网络),尽管 neo4j 非常适合此类任务。
2- 我正在使用 cypher 进行查询,但对 java、rest API 和 spring 数据了解不多。我应该使用哪种 API 进行 mongodb-neo4j 通信?我目前的解决方案是使用 R 并将其用作连接 mongodb 和 neo4j 的平台。
3- 其他图数据库怎么样,有没有适合与Mongo集成的?

【问题讨论】:

    标签: mongodb neo4j graph-databases cypher spring-data-neo4j


    【解决方案1】:

    我找到了两种集成 mongodb 和 Neo4j 的方法。第一个是ryan1234 建议使用 Gremlin 和 Gmongo 的。根据这个优秀的blog
    ,步骤如下 1- 下载GmongoJava mongo driver
    2-复制neo4j/lib目录下的两个jar文件
    3- 这是一个例子。假设我们在 mongodb 中有这个集合(称为 follow)

    { "_id" : ObjectId("4ff74c4ae4b01be7d54cb2d3"), "followed" : "1", "followedBy" : "3", "createdAt" : ISODate("2013-01-01T20:36:26.804Z") }
    { "_id" : ObjectId("4ff74c58e4b01be7d54cb2d4"), "followed" : "2", "followedBy" : "3", "createdAt" : ISODate("2013-01-15T20:36:40.211Z") }
    { "_id" : ObjectId("4ff74d13e4b01be7d54cb2dd"), "followed" : "1", "followedBy" : "2", "createdAt" : ISODate("2013-01-07T20:39:47.283Z") }
    

    从 Neo4j 中的 Gremlin shell 运行以下命令。

    import com.gmongo.GMongo
    mongo = new GMongo() 
    db = mongo.getDB("local")
    db.follows.findOne().followed
    x=[] as Set; db.follows.find().each{x.add(it.followed); x.add(it.followedBy)}
    x.each{g.addVertex(it)}
    db.follows.find().each{g.addEdge(g.v(it.followedBy),g.v(it.followed),'follows',[followsTime:it.createdAt.getTime()])} 
    

    这就是我们在 neo4j 中创建的等效图

    【讨论】:

    • 在您的 sn-p 中实际上缺少链接到 neo4j 的行,但您使用的是参考,加上 db.follows.findOne().followed 可以省略。所以为了让 sn-p 工作,你可以用 g = new Neo4jGraph('/tmp/neo4j') 替换第 4 行
    【解决方案2】:

    https://github.com/tinkerpop/gremlin/wiki

    小鬼!

    专门设计用于 Neo4j/graph 数据库。您也可以轻松下载 GMongo 并连接到 Mongo。

    查看这篇描述如何在 Gremlin 中与多语言数据交互的文章:

    http://thinkaurelius.com/2013/02/04/polyglot-persistence-and-query-with-gremlin/

    【讨论】:

    • @ryn,在您看来,还有其他更容易链接到 mongo 或 neo4j 的图形数据库是一个不错的选择吗?感谢cmets,我会调查的。
    • 据我所知,没有任何东西会直接与 Mongo 集成。 Titan 是 Neo4j 的替代品。 thinkaurelius.github.com/titan 如果您使用 Gremlin,则不会直接将图形数据库与 Mongo 连接。相反,您将数据带回运行 Gremlin 的客户端,然后在那里处理数据。碰巧 Gremlin 有一些语法应该使图遍历更容易。
    【解决方案3】:

    为了同时使用 MongoDB 和 Neo4j,现在有 Neo4j Doc Manager 项目,它会自动将数据从 MongoDB 同步到 Neo4j,将文档转换为属性图结构。

    【讨论】:

      【解决方案4】:

      如果我们想使用R,还有另一种解决方案。下面的R代码,将从mongodb获取数据

      library(RMongo)
      library('bitops')
      library('RCurl')
      library('RJSONIO')
      mg <- mongoDbConnect("local", "127.0.0.1", 27017)
      mongoData <- dbGetQuery(mg, 'follows',"{}")
      

      结果如下

        followed followedBy                    createdAt
      1        1          3 Tue Jan 01 15:36:26 EST 2013
      2        2          3 Tue Jan 15 15:36:40 EST 2013
      3        1          2 Mon Jan 07 15:39:47 EST 2013
      

      以下 R 代码将连接到 Neo4j 并创建图形。它效率不高,但有效

      query <- function(querystring) {
        h = basicTextGatherer()
        curlPerform(url="http://localhost:7474/db/data/ext/CypherPlugin/graphdb/execute_query",
          postfields=paste('query',curlEscape(querystring), sep='='),
          writefunction = h$update,
          verbose = TRUE
        )
      
        result <- fromJSON(h$value())
        data <- data.frame(t(sapply(result$data, unlist)))
        names(data) <- result$columns
         data
      
      }
      
      nodes<-unique(c(mongoData$followed,mongoData$followedBy))
      nodes=paste("_",nodes,sep="")
      nodes<-paste(paste("(",nodes,collapse="),"),")")
      
      edges<-apply(mongoData[,3:2],1,function(x) paste("_",x,sep="",collapse="-[:follows]->"))
      edges<-paste(edges,collapse=",")    
      
      cmd<-paste(nodes,edges,sep=",")
      cmd=paste("create",cmd)
      query(cmd)
      

      【讨论】:

        【解决方案5】:

        您看过 Reco4j 了吗?它使用 neo4j 作为底层图形数据库。作为项目的一部分,他们实施了一些算法。这是链接reco4j。该链接目前不可用,但我在访问该站点时发现其功能很好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-16
          • 1970-01-01
          • 2015-11-29
          • 2015-10-23
          • 2015-11-22
          • 2023-03-12
          • 1970-01-01
          相关资源
          最近更新 更多