2016-06-19 11 views
1

ich meine Daten in dem Geben Format in AreangoDB gespeichert habe, Meine Sammlung Namen in DSP:ungültiges Argument Typ in Aufruf funktioniert ‚attributes()‘ in ArangoDB

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

Ich bin eine Abfrage mit dem obigen Ausführung Daten in folgendem Format:

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 } 

Wenn ich meine Abfrage ArangoDB einreichen, gibt sie die Antwort als:

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

Bitte sagen Sie mir, was ist das Problem mit dieser Abfrage, und warum die Antwort wie oben ist .. Ich arbeite in ArangoDB-2.7.3-win64.

Dank

Antwort

2

Lassen Sie mich zeigen, wie man eine solche tief in verschachtelten Datenstrukturen Graben komplexe Abfrage zu konstruieren. Ich beginnen die äußeren Teile der Abfrage nehmen, ein inneres Ergebnis zu haben:

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

, die mir gibt:

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

So lässt tauchen tiefer in die nächste Schicht. Ich denke du bist nur an den Werten interessiert, die unter data Key Right liegen?so holen wir p.data:

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

das gibt mir dann die Schlüssel für Ihre innere Anordnung:

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

Wir nun erforschen, was wir an diesen Knoten anbei:

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

Seine Wieder ein Array, das wir mit einer FOR Schleife über Schlüssel durchlaufen müssen:

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

Wir fügen diese zusätzliche 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 

wir die inneren meisten Objekte erhalten:

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

Sie die ATTRIBUTES Funktion nutzen wollte, aber die Objekte nur ein Mitglied haben, so

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 

Whi: wir [0] zugreifen können ch gibt uns die Objektschlüssel, eine pro innerste Objekt:

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

Wir prüfen, ob wir jetzt nur die Objektschlüssel der innersten Struktur erhalten; wir holen die Variablennamen ein wenig schlauer als zuvor:

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 

YES:

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

So jetzt ist es Zeit die Ergebnisse zu konstruieren Objekt, wie Sie sie wollten:

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

Die subject ist die Nummer, die das äußerste Element identifiziert, predicate ist der Objektschlüssel und object ist der Wert darin:

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

Welches ist hoffentlich, was Sie wollten?

+0

Vielen Dank Herr –

+1

können Sie die Antwort als akzeptiert pls markieren? – dothebart

+0

Danke dothebart, du hilfst mir sehr .. –