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

如何解析官方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 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同