1.下载以及安装
http://orientdb.com/docs/3.0.x/fiveminute/java.html
cd orientdb-3.0.0
cd bin
1.1 启动服务
sh server.sh
1.2 访问图形化界面
http://localhost:2480/studio/index.html
- Browse 可以查看user,命令: SELECT * FROM OUser
- SCHEMA 查看节点Vertex classes以及边Edge classes等信息
- SECURYITY 管理账户
- GRAPH 支持可视化,创建节点和边,例如输入命令:select from V; 可依可视化
- FUNCTIONS 可以定义函数来扩充sql
- DB 属性以及数据库的导入导出,支持其他数据库格式的导入
- 详情见Introduction to OrientDB Studio,http://orientdb.com/docs/3.0.x/studio/
1.3 创建maven项目
<dependencies>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-client</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
1.4 java链接db并创建基本表
import com.orientechnologies.orient.core.db.OData Session;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import com.orientechnologies.orient.core. data.schema.OClass;
import com.orientechnologies.orient.core. data.schema.OType;
import com.orientechnologies.orient.core.record.OEdge;
import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import java.util.HashMap;
import java.util.Map;
public class step5 {
public static void main(String[] args) {
OrientDB orient = new OrientDB(\"remote:localhost\", OrientDBConfig.defaultConfig());
OData Session db = orient.open(\"test\", \"admin\", \"admin\");
createSchema(db);
createPeople(db);
executeAQuery(db);
executeAnotherQuery(db);
db.close();
orient.close();
}
private static void createSchema(OData Session db) {
OClass person = db.getClass(\"Person\");
if (person == null) {
person = db.createVertexClass(\"Person\");
}
if (person.getProperty(\"name\") == null) {
person.createProperty(\"name\", OType.STRING);
person.createIndex(\"Person_name_index\", OClass.INDEX_TYPE.NOTUNIQUE, \"name\");
}
if (db.getClass(\"FriendOf\") == null) {
db.createEdgeClass(\"FriendOf\");
}
}
private static void createPeople(OData Session db) {
OVertex alice = createPerson(db, \"Alice\", \"Foo\");
OVertex bob = createPerson(db, \"Bob\", \"Bar\");
OVertex jim = createPerson(db, \"Jim\", \"Baz\");
OEdge edge1 = alice.addEdge(bob, \"FriendOf\");
edge1.save();
OEdge edge2 = bob.addEdge(jim, \"FriendOf\");
edge2.save();
}
private static OVertex createPerson(OData Session db, String name, String surname) {
OVertex result = db.newVertex(\"Person\");
result.setProperty(\"name\", name);
result.setProperty(\"surname\", surname);
result.save();
return result;
}
private static void executeAQuery(OData Session db) {
String query = \"SELECT expand(out(\'FriendOf\').out(\'FriendOf\')) from Person where name = ?\";
OResultSet rs = db.query(query, \"Alice\");
while (rs.hasNext()) {
OResult item = rs.next();
System.out.println(\"friend: \" + item.getProperty(\"name\"));
}
rs.close(); //REMEMBER TO ALWAYS CLOSE THE RESULT SET!!!
}
private static void executeAnotherQuery(OData Session db) {
String query =
\" MATCH \" +
\" {class:Person, as:a, where: (name = :name1)}, \" +
\" {class:Person, as:b, where: (name = :name2)}, \" +
\" {as:a} -FriendOf-> {as:x} -FriendOf-> {as:b} \" +
\" RETURN x.name as friend \";
Map<String, > params = new HashMap<String, >();
params.put(\"name1\", \"Alice\");
params.put(\"name2\", \"Jim\");
OResultSet rs = db.query(query, params);
while (rs.hasNext()) {
OResult item = rs.next();
System.out.println(\"friend: \" + item.getProperty(\"friend\"));
}
rs.close();
}
}
原始教程链接
OrientDB for Java Developers in Five Minutes
http://orientdb.com/docs/3.0.x/fiveminute/java-4.html
继续阅读与本文标签相同的文章
-
大智能时代,需要什么样的产品经理
2026-05-18栏目: 教程
-
怎样才能让用户更喜欢你的APP应用
2026-05-18栏目: 教程
-
上线5个月:夸克无障碍服务瞄准视障用户
2026-05-18栏目: 教程
-
重磅,蒲公英私有化部署上线,724成为贴身助理!
2026-05-18栏目: 教程
-
华登区块狗区块猫APP系统平台开发源码定制
2026-05-18栏目: 教程
