Orient DB getting start

23 篇文章 0 订阅

Introduction

The Orient Document DB is the base of ObjectDatabase and Key/Value Server.

The Document Database can be used in schema-less mode. Take a look to this example:

// OPEN THE DATABASE 
ODatabaseDocumentTx db = new ODatabaseDocumentTx("remote:localhost/petshop").open("admin", "admin"); 
 
// CREATE A NEW DOCUMENT AND FILL IT 
ODocument doc = new ODocument(db, "Person"); 
doc.field( "name", "Luke" ); 
doc.field( "surname", "Skywalker" ); 
doc.field( "city", new ODocument(db, "City").field("name","Rome").field("country", "Italy") ); 
               
// SAVE THE DOCUMENT 
doc.save(); 
 
db.close();

This is the very first example. While the code it's pretty clear and easy to understand please note that we don't have declared the type "Person" before now. When a ODocument instance is saved, the declared type "Person" will be created without constraints.

Connection Pool

One of most common use case is to reuse the database avoiding to create it every time. It's also the typical scenario of the Web applications.

// OPEN THE DATABASE 
ODatabaseDocumentTx db= ODatabaseDocumentPool.global().acquire("remote:localhost/petshop", "admin", "admin"); 
 
... 
 
db.close();

The close() method doesn't close the database but release it to the owner pool. It could be reused in the future.

OrientDB Studio

To work with the database try the OrientDB Studio.

Database URL

In the example above a database of type Database Object Transactional has been created using the storage: remote:localhost/petshop. This address is a URL. To know more about database and storage types go to Database URL.

In this case the storage resides in the same computer of the client, but we're using the remote storage type. For this reason we need a Orient Server instance up and running. If we would open the database directly bypassing the server we had to use the local storage type such as "local:/usr/local/database/petshop/petshop" where, in this case, the storage was located in the /usr/local/database/petshop folder on the local file system.

Multi-threading

The ODatabaseObjectTx class is non thread-safe. For this reason use different ODatabaseObjectTx instances by multiple threads. They will share local cache once transactions are committed.

Work in schema-full mode

In the schema-full mode you need to declare the classes you're using. Each class contains one or multiple properties. This mode is similar to the classic Relational DBMS approach where you need to create tables before to store records. To work in schema-full mode take a look to the Schema APIs page.

Inheritance

Starting from the release 0.9.19 OrientDB supports the Inheritance.

Use the database

If you want to connect to a remote database using a OrientDB Server instance, you need to link the orient-client.jar in your classpath and to register the Remote Engine in your client application by calling:

Orient.instance().registerEngine(new OEngineRemote());

Before to use a database you need to open it:

ODatabaseDocumentTx db = new ODatabaseDocumentTx ("remote:localhost/petshop").open("admin", "admin");

The database instance will share the connection versus the storage. if it's a local storage, then all the database instances will be synchronized on it. If it's a remote storage then the network connection will be shared among all the database instances.

To get the reference to the current user use:

OUser user = db.getUser();

Once finished remember to close the database to free precious resources.

db.close();

Create a new document

ODocument instances can be saved by calling the save() method against the object itself. Note that the behaviour depends by the transaction begun if any. See Transactions.

ODocument animal = new ODocument(db, "Animal"); 
animal.field( "name", "Gaudi" ); 
animal.field( "location", "Madrid" ); 
animal.save();

Browse all the documents in a cluster

for (ODocument doc : database.browseCluster("CityCars")) { 
  System.out.println( doc.field("model") );

Browse all the records of a class

for (ODocument animal : database.browseClass("Animal")) { 
  System.out.println( animal.field( "name" ) );

Count records of a class

long cars = database.countClass("Car");

Count records of a cluster

long cityCars = database.countCluster("CityCar");

Execute a query

Orient supports two kinds of queries:

  • Native
  • SQL

 

Native query

Native queries are written in Java code. They are pretty fast since the JVM compiles it as for the rest of application.

Example:

List<ODocument> result = database.query(new ONativeSynchQuery<ODocument, OQueryContextNativeSchema<ODocument>>(database, 
                                "Person", new OQueryContextNativeSchema<ODocument>()) { 
                        @Override 
                        public boolean filter(OQueryContextNativeSchema<ODocument> iRecord) { 
                                return iRecord.field("city").field("name").eq("Rome").and().field("name").like("G%").go(); 
                        }; 
                });

SQL query

Although Orient is part of NoSQL databases, supports the SQL engine, or at least a subset of it with such extensions to work with links to documents and graphs.

To know more about the SQL syntax supported go to: SQL Query.

Example with limit of results fixed to 10:

List<ODocument> result = db.command( 
  new OSQLSynchQuery<ODocument>("select * from Animal where ID = 10 and name like 'G%'")).execute(10);

Update a document

Any persistent document can be updated using the Java language and then calling the db.save() method or save() method against the document to synchronize the changes to the repository. Behaviour depends by the transaction begun if any. See Transactions.

animal.field( "location", "Nairobi" ); 
animal.save();

Orient will update only the fields really changed.

Example of how to update the price of all the animals by 5% more:

for (ODocument animal : database.browseClass("Animal")) { 
  animal.field( "price", animal.field( "price" ) * 105 / 100 ); 
  animal.save(); 
}

Delete a document

To delete a document call the delete() method against the document instance loaded. Behaviour depends by the transaction begun if any. See Transactions.

animal.delete();

Example of deletion of all the documents of class "Animal".

for (ODocument animal : database.browseClass("Animal")) 
  animal.delete();

Javadoc

JavaDoc

 

转载地址http://code.google.com/p/orient/wiki/DocumentDatabase

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值