首先,安装neo4j数据库。我的开发环境是Ubuntu,安装过程参考官网:Neo4j Debian Packages,安装后配置:Post-installation tasks。
然后安装Neo4j Spatial的插件,参加Github介绍:neo4j-contrib/spatial。即把下载的文件解压到安装目录的plugins目录下$NEO4J_HOME/plugins。
接下来,我们使用Java代码在客户端插入数据到neo4j数据库。 我使用Maven构建工程(需要使用Java8进行maven编译)。pom文件如下:
<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>cn.tzy</groupId> <artifactId>neo4j</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>neo4j</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <repositories> <repository> <id>repo2.maven.org</id> <name>maven2 repository</name> <url>http://repo2.maven.org/maven2/</url> </repository> <repository> <id>osgeo</id> <name>Open Source Geospatial Foundation Repository</name> <url>http://download.osgeo.org/webdav/geotools/</url> </repository> <repository> <id>neo4j-contrib-releases</id> <url>https://raw.github.com/neo4j-contrib/m2/master/releases</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>neo4j-contrib-snapshots</id> <url>https://raw.github.com/neo4j-contrib/m2/master/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j</artifactId> <version>3.0.4</version> </dependency> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-spatial</artifactId> <version>0.23-neo4j-3.0.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
导入shapefile文件的代码入下:
package cn.tzy.neo4j; import java.io.File; import java.io.IOException; import org.neo4j.gis.spatial.ShapefileImporter; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.factory.GraphDatabaseFactory; public class Neo4jEx { public static void main(String[] args) throws IOException { File storeDir = new File("C:/Users/theone/Desktop/spatial.db"); GraphDatabaseService database = new GraphDatabaseFactory().newEmbeddedDatabase(storeDir); try (Transaction tx = database.beginTx()) { ShapefileImporter importer = new ShapefileImporter(database); importer.importFile("F:/2016/Data/World/continent.shp", "continent"); tx.success(); } finally { database.shutdown(); } } }
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句