调用函数' ATTRIBUTES()'中的无效参数类型在ArangoDB中

时间:2016-06-19 06:11:05

标签: arangodb aql

我已将我的数据存储在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工作。

由于

1 个答案:

答案 0 :(得分:3)

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

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 - 循环:

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" 
  }

希望你想要什么?