OrientDB is a high-performance document and graph database, which has a great speed advantage in relational search and traversal, especially when dealing with join operations in traditional relational databases, graph databases have incomparable advantages. Although OrientDB officially provides a Java SDK, there is still a certain learning cost, and manual operation scripts are required. This warehouse repackages OrientDB’s Java SDK to operate OrientDB in a more natural language, reducing learning costs and making the project Can integrate OrientDB faster.
characteristic
Simpler API : Let’s not talk much about the example experience, if we want to save aPeople
Object to graph database, first look at the native SDK example:
public static void main(String[] args) { //使用原生JDK保存一个“人”顶点到图数据库 People people1 = new People("张三", "男", 18); try (ODatabaseSession session = OrientSessionFactory .getInstance() .getSession()) { //在图数据库创建Class:People if (session.getClass("People") == null) { session.createVertexClass("People"); } OVertex vertex = session.newInstance(); vertex.setProperty("name", people1.getName()); vertex.setProperty("age", people1.getAge()); vertex.save(); } }
The native SDK encapsulates vertices intoOvertex
object, you first need to get the sessionODatabaseSession
and create the corresponding vertex class, and then the attributes related to the entity need to be calledsetProperty
The method is stored in and saved, and we must also pay attention to closing the session. It is a disaster for entities with many attributes and a large number. Let’s take a look at using the OrientDB API:
public static void main(String[] args) { //创建实体对象 People people2 = new People("李四", "男", 21); //将实体对象包装成ResourceNode对象,其提供了对顶点的操作,对边的操作在ResourceRelation里 ResourceNode<People> li = new GraphResourceNode<>(people2); //直接调用save方法进行保存 li.save(); } @Getter @Setter @ToString //实现Vertex语义接口,表明该实体是一个图数据库的顶点对象 public class People implements Vertex { private String name; private String sex; private Integer age; public People(String name, String sex, Integer age) { this.name = name; this.sex = sex; this.age = age; } public People() { } }
As shown above, through the above statement, the entity objectPeople1
Stored in OrientDB.
more elegant query : The query of the native SDK will inevitably follow the OrientDBSQL
statement orMatch
Statements, and there are few Chinese documents and related blogs in China, and the learning cost is further increased. Therefore, the OrientDB API encapsulates common query operations to achieve complete transparency. Let’s look at an example below: using native SDK to query:
public static void main(String[] args) { try (ODatabaseSession session = OrientSessionFactory .getInstance() .getSession()) { OResultSet resultSet = session.query("select * from People where name = ?", "李四"); People people=new People(); while(resultSet.hasNext()){ OResult result= resultSet.next(); people.setName(result.getProperty("name")); people.setAge(result.getProperty("age")); } resultSet.close(); } }
The use of native JDK is similar to JDBC, and the experience is poor. Let’s take a look at the OrientDB API query:
public static void main(String[] args) { //创建资源图对象,其提供了很多对图的直接操作。使用OrientDB存储库(后续可以拓展Neo4j等存储库) ResourceGraph graph = new ResourceGraph(new OrientDBRepository()); //调用extractNode方法取出指定节点 ResourceNode<People> peopleResourceNode = graph.extractNode(QueryParamsBuilder .newInstance() .addParams("name", "李四") .getParams()); //获取节点对应的属性实体 People people = peopleResourceNode.getSource(); }
more humane traversal : A single query certainly cannot meet the actual needs. OrientDB provides graph traversal and supports more complex queries. Through traversal, we can find other nodes that have a certain relationship with any node. Use the native SDK as follows:
public static void main(String[] args) { try (ODatabaseSession session = OrientSessionFactory .getInstance() .getSession()) { //从顶点#74:0出发,深度优先遍历7层以内的同学,并且进行分页 OResultSet resultSet = session.query("select * from (traverse * from #74:0 MAXDEPTH 7 STRATEGY DEPTH_FIRST) where (@class = \"Classmate\") skip 0 limit 10"); List<ClassMates> classMates=new ArrayList<>(); while(resultSet.hasNext()){ ClassMates classMate=new ClassMates(null); OResult result= resultSet.next(); classMate.setDate(result.getProperty("date")); //... classMates.add(classMate); } resultSet.close(); } }
Using the OrientDB API
public static void main(String[] args) { //创建资源图对象,其提供了很多对图的直接操作。使用OrientDB存储库(后续可以拓展Neo4j等存储库) ResourceGraph graph = new ResourceGraph(new OrientDBRepository()); //直接调用traverse方法,参数分别是,出发节点、深度、遍历策略、分页配置、目标实体类型 PagedResult<ResourceNode<? extends Vertex>> result = graph.traverse(graph.extractNode("#74:0"), QueryParamsBuilder .newInstance() .getParams(), 7, TraverseStrategy.DEPTH_FIRST, new PageConfig(1, 10, true), ClassMates.class); result .getSources() .forEach(item -> System.out.println(item.getSource())); }
No need for words, transparent and more humane.
#OrientDB #API #Homepage #Documentation #Downloads #High #Performance #Documentation #Graph #Database #News Fast Delivery