Integrating mongodb with neo4j, is there any API that will link them?

前端 未结 5 1778
故里飘歌
故里飘歌 2021-02-03 16:02

I am working on a recommendation engine. The user data is collected (Their friendship, locations, likes,education,...) and is already stored in mongodb. I need to recommend rel

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 16:53

    There is another solution if we want to use R. The following R code, will get the data from mongodb

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

    The result will be as following

      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
    

    The following R code will connect to Neo4j and create the graph. It is not efficient but it works

    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)
    

提交回复
热议问题