图形数据库--neo4j分享(嵌入式数据库)


由于项目需要首次使用neo4j,个人觉得Neo4j确保了在一个事务里面的多个操作同时发生,保证数据一致性。不管是采用嵌入模式还是多服务器集群部署,都支持这一特性。
1.neo4j各类基本元素
  节点---在Neo4j中,节点和关系都可以有自己特有的属性。
  关系---节点之间的关系是图数据库很重要的一部分。通过关系可以找到很多关联的数据,比如            节点集合,关系集合以及他们的属性集合。
  属性---属性是由Key-Value键值对组成,键名是字符串。属性值是要么是原始值,要么是原始值类型的一个数组。比如string,int 和int[]都是合法的。
  索引---给某一类资源节点,或者关系,创建一个索引在添加数据的同时,添加索引.这样可以在查询数据的时候很方便,效率不错.
例如定义以下关系



2.创建各基本元素
  节点:
   // 创建根节点
            refRootNode = createNode(referenceNode, CustomerRelationshipType.REF_ROOT, "REF_ROOT");
            // 创建行政区划节点
            refAreaNode = createNode(refRootNode, CustomerRelationshipType.REF_AREA, "行政区划");
            // 创建站点节点
            refStationNode = createNode(refRootNode, CustomerRelationshipType.REF_STATION, "站点");

  关系:定义所有会使用的RelationshipType,关系类型.例如:
  public enum CustomerRelationshipType implements RelationshipType {
      REF_ROOT,REF_AREA,REF_STATION
  }
  //创建关系
       node = neo.createNode();
       refAreaNode.createRelationshipTo(node, CustomerRelationshipType.REF_AREA);
  属性:
  //节点添加属性
   areaIndex = indexManager.forNodes("area");
   this.setNodeProperty("name", name);
   areaIndex.add(underlyingNode, "name", name);

  索引:注意更新属性同时要更新索引时,要删除再添加
/ create a node with a property
// so we have something to update later on
Node fishburn = graphDb.createNode();
fishburn.setProperty( "name", "Fishburn" );
// index it
actors.add( fishburn, "name", fishburn.getProperty( "name" ) );
// update the index entry
// when the property value changes
actors.remove( fishburn, "name", fishburn.getProperty( "name" ) );
fishburn.setProperty( "name", "Laurence Fishburn" );
actors.add( fishburn, "name", fishburn.getProperty( "name" ) );

//索引
IndexManager index = graphDb.index();
Index<Node> actors = index.forNodes( "actors" );
Index<Node> movies = index.forNodes( "movies" );
RelationshipIndex roles = index.forRelationships( "roles" );

3.基本的几个方法如上述2.
还有是最好将真的neo4j操作的方法封装好然后调用
因为如果有数据同步,或者其他操作的时候.使用使用法会太频繁.
public interface Station extends Serializable{
    /**
     * @return the code
     */
    String getCode();

    /**
     * @return the id
     */
    String getId();

    /**
     * @return the name
     */
    String getName();
   
     Area getArea();
   
    void setArea(String area,Node node);
   
    StationType getType();
   
    void setType(String type,Node node);
    /**
     * @param code the code to set
     */
    void setCode(String code);

    /**
     * @param id the id to set
     */
    void setId(String id);

    /**
     * @param name the name to set
     */
    void setName(String name);
   
    void remove();
   
    String getStationUrl(UriInfo uriInfo);
   
     String getRoomsUrl(UriInfo uriInfo);
    
     String getFibersUrl(UriInfo uriInfo);
    
     JSONObject toJSON(UriInfo uriInfo);
}

neo4j 提供三种查询方法:
1、使用查询方法以及索引查询指定类型数据
2、遍历查询框架
3、类似SQL的 Cypher查询语言(neo4j-enterprise-1.8.1工具)
从性能上来说 1 优于2, 2 优于 3
根据属性名称查询:
  private Node findStaTypeNodeByName(String value) {
        Index<Node> areaIndex = neo.index().forNodes("statype");
        Node node = areaIndex.get("name", value).getSingle();
        if (null == node) {
            return null;
        }
        return node;
    }
查询全部区域:
public List<Area> findAllArea() {
        List<Area> result = new ArrayList<Area>();
        Iterable<Relationship> rs = refAreaNode.getRelationships(CustomerRelationshipType.AREA, Direction.OUTGOING);
        for (Relationship r : rs) {
            Node node = r.getEndNode();
            Area area = new AreaImpl(node);
            result.add(area);
        }
        return result;
    }
本人是也刚刚学习,总结一下学习过程,希望对有需求的朋友有帮助.
Neo4j中文API地址:http://www.neo4j.org.cn/old-docs/

猜你喜欢

转载自xustar.iteye.com/blog/1774895