Get all used fieldnames of schema-hybrid Class in Orientdb

我只是一个虾纸丫 提交于 2019-12-25 03:33:06

问题


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:


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!



来源:https://stackoverflow.com/questions/30787637/get-all-used-fieldnames-of-schema-hybrid-class-in-orientdb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!