转:OrientDB2.0安装及实例、场景

OrientDB相对于Mongodb的优点:

1.支持本地操作数据库(即不需要启动服务器,类似Access),适用于轻量级应用 
2.可以通过JVM控制内存,而mongodb只能通过系统层面控制用户的资源来实现 
3.根据上述2点,OrientDB很好的集成到系统中、与应用部署到同一台服务器,非常适合用于记录日志

共同点:

1.文档数据库
2.支持分布式部署

1、安装并启动。下载window版的,解压,进入解压目录的bin目录,双击“server.bat”,按照提示输入初始密码,输入“123456”,确认后,出现“OrientDB Server v2.0 is active.”即启动成功。

2、Java实例,把解压目录的lib目录下的orientdb-*.jar导入到项目中。(因为我是在现有项目的基础上集成的,所以不用导入其它依赖包,报错的话根据自己的情况导入,或者整个lib目录导入),实例代码如下:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.List;
import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
  
public class OrientdbUtil {
  
     public static void createDb(){
         try {
             //本地创建数据库,不需要启动服务器
//          ODatabaseDocumentTx oDatabaseDocumentTx = new ODatabaseDocumentTx ("plocal:D:/DevProgram/Orientdb20/databases/testdb").create();
//          oDatabaseDocumentTx.close();
              
             //远程创建数据库,需要先启动服务器
             OServerAdmin oServerAdmin = new OServerAdmin( "remote:localhost" );
             oServerAdmin.connect( "root" , "123456" );
             oServerAdmin.createDatabase( "testdb" , "document" , "plocal" );
             oServerAdmin.close();
              
         } catch (Exception e) {
             e.printStackTrace();
         }
  
         return ;
     }
  
     public static void addRecord(){
          
//      ODatabaseDocumentTx oDatabaseDocumentTx = new ODatabaseDocumentTx("plocal:D:/DevProgram/Orientdb20/databases/testdb").open("admin", "admin");
         ODatabaseDocumentTx oDatabaseDocumentTx = new ODatabaseDocumentTx( "remote:localhost/testdb" ).open( "admin" , "admin" );
  
         ODocument oDocument = new ODocument( "Person" );
         oDocument.field( "name" , "张三" );
         oDocument.field( "surname" , "张小三" );
         oDocument.field( "city" , new ODocument( "City" ).field( "name" , "深圳" ).field( "country" , "中国" ) );
  
         oDocument.save();
  
         oDatabaseDocumentTx.close();
          
         return ;
     }
      
     public static void searchRecord(){
          
//      ODatabaseDocumentTx oDatabaseDocumentTx = new ODatabaseDocumentTx("plocal:D:/DevProgram/Orientdb20/databases/testdb").open("admin", "admin");
         ODatabaseDocumentTx oDatabaseDocumentTx = new ODatabaseDocumentTx( "remote:localhost/testdb" ).open( "admin" , "admin" );
  
         List<ODocument> listResult = oDatabaseDocumentTx.query(
                   new OSQLSynchQuery<ODocument>( "select * from Person where name = '张三'" ));
  
         for ( ODocument oDocument:listResult ){
             System.out.println(oDocument.field( "surname" ));
         }
         oDatabaseDocumentTx.close();
          
         return ;
     }
      
     public static void main(String[] args) {
         OrientdbUtil.createDb();
         OrientdbUtil.addRecord();
         OrientdbUtil.searchRecord();
     }
          
}


来自:http://my.oschina.net/u/1587335/blog/370345


你可能感兴趣的:(转:OrientDB2.0安装及实例、场景)