2016-09-27 176 views
1

我一直在研究这个问题几个小时,而且我还没有与Neo4j的社区Golang驱动程序运气。如何在Go中的Neo4j数据库上运行Cypher查询?

我试图运行“movies-go-cq”和“neoism”示例。 movies-go-cq示例不适用于我,it crashes when localhost:8080 is loaded in the browser

Cypher在我的Neo4j数据库上使用neoism查询只返回空白/空白数据。但是,当我在localhost:7474的Neo4j浏览器中运行相同的查询时,将返回预期的数据。

这里是Go代码我与neoism运行:

package main 

import (
    "fmt" 

    "github.com/jmcvetta/neoism" 
) 

func main() { 
    // Reference: https://godoc.org/github.com/jmcvetta/neoism 

    // Connect to the Neo4j server 
    db, _ := neoism.Connect("http://localhost:7474/db/data") 

    // Issue a query 
    res1 := []struct { 
     A string `json:"path1"` // `json` tag matches column name in query 
     B string `json:"path2"` 
    }{} 
    cq1 := neoism.CypherQuery{ 
     // Use backticks for long statements - Cypher is whitespace indifferent 
     Statement: ` 
     MATCH path1 = shortestPath((plant:Plant {Term: "Vaccinium corymbosum"})-[*..5]-(gene:Gene {Description: "adenylate cyclase activating polypeptide 1"})) 
     MATCH path2 = shortestPath((gene)-[*..5]-(disease:Medical_Heading {Term: "Alzheimer Disease"})) 
     RETURN path1, path2 
     `, 
     Result: &res1, 
    } 
    db.Cypher(&cq1) 
    r := res1[0] 
    fmt.Println(r.A, r.B) 
} 

我正在考虑写在Go使用的Neo4j的HTTP REST的API,如果我不能得到现有转到驱动程序才能正常工作,我自己的API包装;我是Golang的新手,我会很感激任何有关调试Go代码的建议或在Golang中使用Neo4j的技巧。感谢您的时间。

+2

[db.Cypher](https://godoc.org/github.com/jmcvetta/neoism#Database.Cypher)返回错误。你能捕捉到('err:= db.Cypher(&cq1)')并用'err'的值更新你的问题吗? – algrebe

回答

0

我知道你现在面临的是什么。在我面临同样问题之前的一段时间。 有2种可能的情况存在: -

1)您应该始终声明结构变量Capital。

res1 := []struct { CAPITAL_STR1 string `json:"path1"` CAPITAL_STR2 string `json:"path2"` }{} 你正在做的完全正确A和B.

2)你必须粘贴确切的JSON类型(有误)

res1 := []struct { CAPITAL_STR1 string `json:"path1.distance"` CAPITAL_STR2 string `json:"path2.distance"` }{}

为了得到确切的JSON格式输出检查JSON响应在你的Neo4J浏览器中。它在部分代码下可用。

+0

谢谢,Shivendra!我将在明天工作时查看我的代码,以检查这一点。 – Grace

相关问题