douyi2107 2018-12-06 18:41
浏览 145
已采纳

如何解析官方neo4j go驱动程序中的结果?

I have a problem with parsing results from the neo4j-go-driver official Driver when Cypher query is MATCH. With CREATE query like in the example on README.md works fine but with MATCH does not do indexing with result Record().GetByIndex(0)

result, err = session.Run("match(n) where n.id = 1 return n", map[string]interface{}{})
if err != nil {
    panic(err)
}

for result.Next() {
    a := result.Record().GetByIndex(1)         //error: Index out or range
    b := result.Record().GetByIndex(0).(int64) //error: interface {} is *neo4j.nodeValue, not int64
    c := result.Record().GetByIndex(0) //prints corect result: &{14329224 [Item] map[id:1 name:Item 1]}
    fmt.Println(c)

}

Since nodeValue is not exported type I do not know hot to asserted attributes or whole interface back to nodeValue type.

  • 写回答

1条回答 默认 最新

  • dougong7850 2018-12-06 21:07
    关注

    The values you specify after the return in the query are 0 indexed from left to right. So in your example, since you are only returning one value from the MATCH (in this case defined as n), it will be available at index 0. Index one, as the error message shows, is out of range.

    //in the following example a node has an id of type int64, name of type string, and value of float32
    
    result, _ := session.Run(`
        match(n) where n.id = 1 return n.id, n.name, n.value`, nil)
                             // index 0 ^  idx 1^ . idx 2^
    
    for result.Next() {
       a, ok := result.Record().GetByIndex(0).(int64)  //n.id
       // ok == true
       b, ok := result.Record().GetByIndex(0).(string) //n.name
       // ok == true
       c, ok := result.Record().GetByIndex(0).(float64)//n.value
       // ok == true
    }
    

    This is probably a baseline for an idiomatic way to access property values on a node - instead of trying to access the whole node (which the driver implicitly is discouraging via keeping nodeValue an unexported struct) return individual properties from the node like the above example.

    A few other points to consider when working with the driver. Result also exposes a Get(key string) (interface{}, ok) method for accessing results by the name of the return value. In this way if you need to change the order of your results, your value extraction code won't break trying to access the wrong index. so taking the above and modifying it a little:

    result, _ := session.Run(`
            match(n) where n.id = 1 return n.id as nodeId, n.name as username, n.value as power`, nil)
    
    for result.Next() {
        record := result.Record()
        nodeID, ok := record.Get("nodeId")
        // ok == true and nodeID is an interface that can be asserted to int
        username, ok := record.Get("username")
        // ok == true and username is an interface that can be asserted to string
    
    }
    

    Last thing point out is the map[string]interface{} can be used to pass values as arguments to the query.

    session.Run("match(n) where n.id = $id return n", 
        map[string]interface{}{
          "id": 1237892
        })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭