【问题标题】:invalid argument type in call to function 'ATTRIBUTES()' in ArangoDB在 ArangoDB 中调用函数“ATTRIBUTES()”时参数类型无效
【发布时间】:2016-06-21 00:17:12
【问题描述】:

我已将我的数据存储在 AreangoDB 中的给定格式中,我在 DSP 中的集合名称:

 "data": {
"1":   [ {"db_name": "DSP"}, {"rel": "2"} ], 
"2":   [ {"rel_name": "DataSource"}, {"tuple": "201"}, {"tuple": "202"}, {"tuple": "203"} ],
"201": [ {"src_id": "Pos201510070"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151007"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "postgres"}, {"port": "None"} ],
"202": [ {"src_id": "pos201510060"}, {"src_name": "Postgres"},{"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "DSP"}, {"port": "5432"} ],
"203": [ {"src_id": "pos201510060"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"},{"src_type": "Structured"},{"db_name": "maindb"},{"port": "5432"} ]
}

我正在使用以下格式的上述数据执行查询:

  FOR p IN NestedDSP
  LET attributes = ATTRIBUTES(p) 
  FOR attribute IN attributes 
      LET key = ATTRIBUTES(attribute)[0] 
      LET value = attribute[key] 
      RETURN { subject: attribute, predicate: key, object: value }

当我向 ArangoDB 提交查询时,它会返回如下响应:

    Warnings:

[1542], 'invalid argument type in call to function 'ATTRIBUTES()''
[1542], 'invalid argument type in call to function 'ATTRIBUTES()''
[1542], 'invalid argument type in call to function 'ATTRIBUTES()''
[1542], 'invalid argument type in call to function 'ATTRIBUTES()''

Result:

[
  {
    "subject": "data",
    "predicate": null,
    "object": null
  },
  {
    "subject": "_id",
    "predicate": null,
    "object": null
  },
  {
    "subject": "_rev",
    "predicate": null,
    "object": null
  },
  {
    "subject": "_key",
    "predicate": null,
    "object": null
  }
]

请告诉我这个查询有什么问题,为什么答案会像上面那样。我在 ArangoDB-2.7.3-win64 工作。

谢谢

【问题讨论】:

    标签: arangodb aql


    【解决方案1】:

    让我演示如何构建这样一个深入挖掘嵌套数据结构的复杂查询。我开始获取查询的外部部分,以获得内部结果:

    FOR p IN NestedDSP
      LET attributes = ATTRIBUTES(p) 
      FOR attribute IN attributes 
          RETURN attribute
    

    这给了我:

    [ 
      "data", 
      "_rev", 
      "_key", 
      "_id" 
    ]
    

    让我们更深入地了解下一层。我猜你只对data 键下面的值感兴趣,对吧?所以我们选择p.data

    FOR p IN NestedDSP
      LET attributes = ATTRIBUTES(p.data) 
      FOR attribute IN attributes 
          RETURN attribute
    

    然后给我你的下一个内部数组的键:

    [ 
      "203", 
      "202", 
      "201", 
      "2", 
      "1" 
    ]
    

    我们现在探索我们发现附加到这些节点的内容:

    FOR p IN NestedDSP
      LET attributes = ATTRIBUTES(p.data) 
      FOR oneAttribute IN attributes 
        LET keys = p.data[oneAttribute]
          RETURN keys
    

    它又是一个数组,我们需要使用 FOR 循环遍历键:

    [
      [ 
        { 
          "src_id" : "pos201510060" 
        }, 
        { 
          "src_name" : "Postgres" 
        }, ...
    

    我们添加了这个额外的FOR-loop:

    FOR p IN NestedDSP
      LET attributes = ATTRIBUTES(p.data) 
      FOR oneAttribute IN attributes 
        LET keys = p.data[oneAttribute]
        FOR key IN keys
          RETURN key
    

    我们得到最里面的对象:

    [ 
      { 
        "src_id" : "pos201510060" 
      }, 
      { 
        "src_name" : "Postgres" 
      }, 
      { 
        "password" : "root" 
      }, 
    ...
    

    你想使用ATTRIBUTES函数,但是对象只有一个成员,所以我们可以访问[0]

    FOR p IN NestedDSP
      LET attributes = ATTRIBUTES(p.data) 
      FOR oneAttribute IN attributes 
        LET keys = p.data[oneAttribute]
        FOR key IN keys
          LET keyAttributes=ATTRIBUTES(key)
            RETURN keyAttributes
    

    它为我们提供了对象键,每个最里面的对象一个:

    [ 
      [ 
        "src_id" 
      ], 
      [ 
        "src_name" 
      ], 
    

    我们检查我们现在是否只得到最内层结构的对象键;我们选择比上面更聪明的变量名:

      FOR p IN NestedDSP
        LET attributes = ATTRIBUTES(p.data) 
        FOR oneAttribute IN attributes 
          LET pairs = p.data[oneAttribute]
          FOR onePair IN pairs
            LET pairKey=ATTRIBUTES(onePair)[0]
              RETURN pairKey
    

    是的:

    [ 
      "src_id", 
      "src_name", 
      "password", 
      "host", 
        ...
    

    现在是时候按照您的需要构建结果对象了:

      FOR p IN NestedDSP
        LET attributes = ATTRIBUTES(p.data) 
        FOR oneAttribute IN attributes 
          LET pairs = p.data[oneAttribute]
          FOR onePair IN pairs
            LET pairKey=ATTRIBUTES(onePair)[0]
              RETURN { subject: oneAttribute, predicate: pairKey, object: onePair[pairKey] }
    

    subject 是标识最外层项目的数字,predicate 是对象键,object 是其中的值:

    [ 
      { 
        "subject" : "203", 
        "predicate" : "src_id", 
        "object" : "pos201510060" 
      }, 
      { 
        "subject" : "203", 
        "predicate" : "src_name", 
        "object" : "Postgres" 
      }
    

    希望哪一个是您想要的?

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2023-01-12
      • 2018-12-30
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      相关资源
      最近更新 更多