Get all used fieldnames of schema-hybrid Class in Orientdb

1k views Asked by At

I started to look at OrientDB and try to code a simple webapp with OrientDB as Database.

I have a schema-hybrid database schema, which includes the class Server. Is there any possibility to get all used fields, even the one that are not defined in the schema? So that in the webapp frontend a list of fields existing in the other Servers can be shown to the user.

So with following Situation:

CREATE CLASS Server EXTENDS V
CREATE PROPERTY Server.name string
CREATE PROPERTY Server.hostnames embeddedlist string
CREATE VERTEX Server SET name = "SRV1",hostnames = ["srv1.de.test.ag","srv1.test.ag"], cpu = "3-GHZ"

I can get the fields defined in the schema with:

select expand(properties) from (select expand(classes) from metadata:schema) where name = "Server"

But is there a way to get the cpu-field or rather all schema-less fields which are used in this class? Can't find something in the docs.

1

There are 1 answers

1
Omega Silva On

Conceptually what Schema-Hybrid means is;

  • Let you define a schema for a class, so that each of the records (or instances) complies to the schema
  • Let each of the record (or instance) define it's own custom fields

Therefore, the custom fields created are actually at individual record levels. Hence, two instances could have two different sets of custom fields apart from the fields defined from the schema.

Due to the above reason, the only way to get all the fields (i.e. schema defined + custom defined), is to scan through all the records, and figure out what each of the records have defined in custom.

You can do that using the API. Following is how you can invoke the API via JavaScript. You can do the same with Java.

var db = orient.getGraph();

var result = db.command("sql", "SELECT FROM V WHERE @rid = #9:2");
var fields = result[0].getRecord().fieldNames();

for (var i = 0; i < fields.length; i++) {
  print(fields[i]);
}

If you make sure that all the records (i.e. all the Server or Server1 records) will have the same set of custom fields, you can use the above code to query one record and get all the fields for that record. That'd do the job for you.

Hope this helps!