日期:2014-05-16  浏览次数:20534 次

如何在java程序中引入neo4j数据库

       随着关系型数据库在某些方面的力不从心,了解当下流行的各种数据库模式的特点和性能,无疑会给我们提供更多的选择和方向。 neo4j是一种图形数据库,在遍历和关联查询方面具有突出的优势。废话少说,深入了解neo4j之前,先让我们尝试一下怎样在程序中使用neo4j。

        neo4j采用java语言开发,如果我们要在java程序中以内嵌方式使用neo4j,只需导入neo4j的对应包即可。

        首先,我们来创建一个maven项目并修改pom.xml添加对neo4j的依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>neo4j-learn</groupId>
    <artifactId>neo4j-learn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>1.9.4</version>
        </dependency>
    </dependencies>

</project>

         然后,我们在项目中创建一个neo4j.properties(数据库的配置文件)文件和一个java类(调用数据库)。

        neo4j.properties

# Default values for the low-level graph engine
#neostore.nodestore.db.mapped_memory=25M
#neostore.relationshipstore.db.mapped_memory=50M
#neostore.propertystore.db.mapped_memory=90M
#neostore.propertystore.db.strings.mapped_memory=130M
#neostore.propertystore.db.arrays.mapped_memory=130M

# Autoindexing

# Enable auto-indexing for nodes, default is false
#node_auto_indexing=true

# The node property keys to be auto-indexed, if enabled
#node_keys_indexable=name,age


# Enable auto-indexing for relationships, default is false
#relationship_auto_indexing=true

# The relationship property keys to be auto-indexed, if enabled
#relationship_keys_indexable=name,age

# Keep logical logs, needed for online backups to work
keep_logical_logs=true

# Enable online backups to be taken from this database.
online_backup_enabled=true

# Uncomment and specify these lines for running Neo4j in High Availability mode.
# ha.server_id is a unique integer for each instance of the Neo4j database in the cluster.
# (as opposed to the coordinator instance IDs)
# example: ha.server_id=1
#ha.server_id=

# ha.coordinators is a comma-separated list (without spaces) of the host:port of where to
# find one or more of the Neo4j coordinator servers.
# Avoid localhost due to IP resolution issues on some systems.
# example: ha.coordinators=localhost:2181,1.2.3.4:4321
#ha.coordinators=localhost:2181

# You can also, optionally, configure the ha.cluster_name. This is the name of the cluster this
# instance is supposed to join. Accepted characters are alphabetical, numerical, dot and dash. 
# This configuration is useful if you have multiple Neo4j HA clusters managed by the same
# Coordinator cluster.
# Example: ha.cluster_name = my.neo4j.ha.cluster
#ha.cluster_name =

# IP and port for this instance to bind to to communicate data with the
# other neo4j instances in the cluster. This is broadcasted to the other
# cluster members, so different members can have different communication ports.
# Optional if the members are on different machines so the IP is different for every member.
#ha.server = localhost:6001

# The interval at which slaves will pull updates from the master. Comment out 
# the option to disable periodic pulling of updates. Unit is seconds.
ha.pull_interval = 10

# The session timeout for the zookeeper client. Lower values make new master
# election happen closer to the master loosing connection but also more sensitive
# to zookeeper quorum hiccups. If experiencing master switches without reason
# consider increasing this value. Unit is seconds
#ha.zk_session_timeout = 5

# A