实践Neo4j之二安装和入门

Installation

操作系统:deepin linux 15.4.1

JDK

jdk:1.8.0_101

Neo4j

版本:community-3.2.1

设置NEO4J_HOME环境变量

1
2
$ $NEO4J_HOME/bin/neo4j --help
Usage: neo4j { console | start | stop | restart | status | version }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//console模式启动
$ $NEO4J_HOME/bin/neo4j console
Active database: graph.db
Directories in use:
  home:         /home/angi/software/neo4j
  config:       /home/angi/software/neo4j/conf
  logs:         /home/angi/software/neo4j/logs
  plugins:      /home/angi/software/neo4j/plugins
  import:       /home/angi/software/neo4j/import
  data:         /home/angi/software/neo4j/data
  certificates: /home/angi/software/neo4j/certificates
  run:          /home/angi/software/neo4j/run
Starting Neo4j.
WARNING: Max 10240 open files allowed, minimum of 40000 recommended. See the Neo4j manual.
2017-07-07 08:20:02.907+0000 INFO  ======== Neo4j 3.2.1 ========
2017-07-07 08:20:03.161+0000 INFO  Starting...
2017-07-07 08:20:04.691+0000 INFO  Bolt enabled on 127.0.0.1:7687.
2017-07-07 08:20:10.441+0000 INFO  Started.
2017-07-07 08:20:11.252+0000 INFO  Remote interface available at http://localhost:7474/

默认数据库数据目录为/home/angi/software/neo4j/data/databases/graph.db

Neo4j Browser

Neo4j Browser是Neo4j 2.0新推出的web控制台,2.0以前是Neo4j Web Admin Console。

首次访问需要重置neo4j的密码,初始密码是neo4j

Neo4j Browser (localhost:7474)

neo4j server connect

neo4j browser

Neo4j Shell

neo4j shell是neo4j自带的,可以连接访问本地或远程neo4j数据库等,基本信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ neo4j-shell --help
 -host      Domain name or IP of host to connect to (default: localhost)
 -port      Port of host to connect to (default: 1337)
 -name      RMI name, i.e. rmi://<host>:<port>/<name> (default: shell)
 -pid       Process ID to connect to
 -c         Command line to execute. After executing it the shell exits
 -file      File containing commands to execute, or '-' to read from stdin. After executing it the shell exits
 -readonly  Connect in readonly mode (only for connecting with -path)
 -path      Points to a neo4j db path so that a local server can be started there
 -config    Points to a config file when starting a local server

Example arguments for remote:
	-port 1337
	-host 192.168.1.234 -port 1337 -name shell
	-host localhost -readonly
	...or no arguments for default values
Example arguments for local:
	-path /path/to/db
	-path /path/to/db -config /path/to/neo4j.config
	-path /path/to/db -readonly
  • 连接本地

    本地不需要启动neo4j,-path指定neo4j数据库数据目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ $NEO4J_HOME/bin/neo4j-shell -path /home/angi/software/neo4j/data/databases/graph.db
NOTE: Local Neo4j graph database service at '/home/angi/software/neo4j/data/databases/graph.db'
Welcome to the Neo4j Shell! Enter 'help' for a list of commands. Please note that neo4j-shell is deprecated and to be replaced by cypher-shell.


neo4j-sh (?)$ start keanu = node(1) return keanu;
+----------------------------------------+
| keanu                                  |
+----------------------------------------+
| Node[1]{name:"Keanu Reeves",born:1964} |
+----------------------------------------+
1 row
619 ms
neo4j-sh (?)$ 

如果指定一个非neo4j数据库数据的目录,则会新建数据库数据,如下/home/angi/software/abc是个空目录

1
2
3
4
5
6
7
8
9
10
11
12
13
$ neo4j-shell -path /home/angi/software/abc
NOTE: Local Neo4j graph database service at '/home/angi/software/abc'
Welcome to the Neo4j Shell! Enter 'help' for a list of commands. Please note that neo4j-shell is deprecated and to be replaced by cypher-shell.


neo4j-sh (?)$ start keanu = node(1) return keanu;
+-------+
| keanu |
+-------+
+-------+
0 row
552 ms
neo4j-sh (?)$
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ls abc
index                                 neostore.nodestore.db.labels             neostore.relationshipgroupstore.db
logs                                  neostore.nodestore.db.labels.id          neostore.relationshipgroupstore.db.id
neostore                              neostore.propertystore.db                neostore.relationshipstore.db
neostore.counts.db.a                  neostore.propertystore.db.arrays         neostore.relationshipstore.db.id
neostore.id                           neostore.propertystore.db.arrays.id      neostore.relationshiptypestore.db
neostore.labelscanstore.db            neostore.propertystore.db.id             neostore.relationshiptypestore.db.id
neostore.labeltokenstore.db           neostore.propertystore.db.index          neostore.relationshiptypestore.db.names
neostore.labeltokenstore.db.id        neostore.propertystore.db.index.id       neostore.relationshiptypestore.db.names.id
neostore.labeltokenstore.db.names     neostore.propertystore.db.index.keys     neostore.schemastore.db
neostore.labeltokenstore.db.names.id  neostore.propertystore.db.index.keys.id  neostore.schemastore.db.id
neostore.nodestore.db                 neostore.propertystore.db.strings        neostore.transaction.db.0
neostore.nodestore.db.id              neostore.propertystore.db.strings.id     store_lock
angi@angi-deepin:~/software$ 
  • 连接远程

默认配置下没有开启远程shell服务,通过修改neo4j.conf中如下配置开启

1
2
3
4
5
6
## Enable a remote shell server which Neo4j Shell clients can log in to.
dbms.shell.enabled=true
## The network interface IP the shell will listen on (use 0.0.0.0 for all interfaces).
dbms.shell.host=127.0.0.1
## The port the shell will listen on, default is 1337.
dbms.shell.port=1337

远程neo4j启动后进行连接

1
2
3
4
5
6
7
8
9
10
11
12
13
$ $NEO4J_HOME/bin/neo4j-shell -host localhost -port 1337
Welcome to the Neo4j Shell! Enter 'help' for a list of commands. Please note that neo4j-shell is deprecated and to be replaced by cypher-shell.
NOTE: Remote Neo4j graph database service 'shell' at port 1337

neo4j-sh (?)$ start keanu = node(1) return keanu;
+----------------------------------------+
| keanu                                  |
+----------------------------------------+
| Node[1]{name:"Keanu Reeves",born:1964} |
+----------------------------------------+
1 row
20 ms
neo4j-sh (?)$ 

Neo4j Admin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$ neo4j-admin --help
unrecognized command: --help
usage: neo4j-admin <command>

Manage your Neo4j instance.

environment variables:
    NEO4J_CONF    Path to directory which contains neo4j.conf.
    NEO4J_DEBUG   Set to anything to enable debug output.
    NEO4J_HOME    Neo4j home directory.
    HEAP_SIZE     Set size of JVM heap during command execution.
                  Takes a number and a unit, for example 512m.

available commands:

General
    check-consistency
        Check the consistency of a database.
    help
        This help text, or help for the command specified in <command>.
    import
        Import from a collection of CSV files or a pre-3.0 database.
    store-info
        Prints information about a Neo4j database store.

Authentication
    set-default-admin
        Sets the default admin user when no roles are present.
    set-initial-password
        Sets the initial password of the initial admin user ('neo4j').

Offline backup
    dump
        Dump a database into a single-file archive.
    load
        Load a database from an archive created with the dump command.

Use neo4j-admin help <command> for more details.

Neo4j Import

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
$ neo4j-import --help
WARNING: neo4j-import is deprecated and support for it will be removed in a future
version of Neo4j; please use neo4j-admin import instead.

Neo4j Import Tool
	neo4j-import is used to create a new Neo4j database from data in CSV files. See 
	the chapter "Import Tool" in the Neo4j Manual for details on the CSV file format 
	- a special kind of header is required.
Usage:
--into <store-dir>
	Database directory to import into. Must not contain existing database.
--database <database-name>
	Database name to import into. Must not contain existing database.
--nodes[:Label1:Label2] "<file1>,<file2>,..."
	Node CSV header and data. Multiple files will be logically seen as one big file 
	from the perspective of the importer. The first line must contain the header. 
	Multiple data sources like these can be specified in one import, where each data 
	source has its own header. Note that file groups must be enclosed in quotation 
	marks. Each file can be a regular expression and will then include all matching 
	files. The file matching is done with number awareness such that e.g. 
	files:'File1Part_001.csv', 'File12Part_003' will be ordered in that order for a 
	pattern like: 'File.*'
--relationships[:RELATIONSHIP_TYPE] "<file1>,<file2>,..."
	Relationship CSV header and data. Multiple files will be logically seen as one 
	big file from the perspective of the importer. The first line must contain the 
	header. Multiple data sources like these can be specified in one import, where 
	each data source has its own header. Note that file groups must be enclosed in 
	quotation marks. Each file can be a regular expression and will then include all 
	matching files. The file matching is done with number awareness such that e.g. 
	files:'File1Part_001.csv', 'File12Part_003' will be ordered in that order for a 
	pattern like: 'File.*'
--delimiter <delimiter-character>
	Delimiter character, or 'TAB', between values in CSV data. The default option is 
	,.
--array-delimiter <array-delimiter-character>
	Delimiter character, or 'TAB', between array elements within a value in CSV 
	data. The default option is ;.
--quote <quotation-character>
	Character to treat as quotation character for values in CSV data. The default 
	option is ". Quotes inside quotes escaped like """Go away"", he said." and "\"Go 
	away\", he said." are supported. If you have set "'" to be used as the quotation 
	character, you could write the previous example like this instead: '"Go away", 
	he said.'
--multiline-fields <true/false>
	Whether or not fields from input source can span multiple lines, i.e. contain 
	newline characters. Default value: false
--trim-strings <true/false>
	Whether or not strings should be trimmed for whitespaces. Default value: false
--input-encoding <character set>
	Character set that input data is encoded in. Provided value must be one out of 
	the available character sets in the JVM, as provided by 
	Charset#availableCharsets(). If no input encoding is provided, the default 
	character set of the JVM will be used.
--ignore-empty-strings <true/false>
	Whether or not empty string fields, i.e. "" from input source are ignored, i.e. 
	treated as null. Default value: false
--id-type <id-type>
	One out of [STRING, INTEGER, ACTUAL] and specifies how ids in node/relationship 
	input files are treated.
	STRING: arbitrary strings for identifying nodes.
	INTEGER: arbitrary integer values for identifying nodes.
	ACTUAL: (advanced) actual node ids. The default option is STRING. Default value: 
	STRING
--processors <max processor count>
	(advanced) Max number of processors used by the importer. Defaults to the number 
	of available processors reported by the JVM (in your case 8). There is a certain 
	amount of minimum threads needed so for that reason there is no lower bound for 
	this value. For optimal performance this value shouldn't be greater than the 
	number of available processors.
--stacktrace <true/false>
	Enable printing of error stack traces. Default value: false
--bad-tolerance <max number of bad entries, or true for unlimited>
	Number of bad entries before the import is considered failed. This tolerance 
	threshold is about relationships refering to missing nodes. Format errors in 
	input data are still treated as errors. Default value: 1000
--skip-bad-entries-logging <true/false>
	Whether or not to skip logging bad entries detected during import. Default 
	value: false
--skip-bad-relationships <true/false>
	Whether or not to skip importing relationships that refers to missing node ids, 
	i.e. either start or end node id/group referring to node that wasn't specified 
	by the node input data. Skipped nodes will be logged, containing at most number 
	of entites specified by bad-tolerance, unless otherwise specified by 
	skip-bad-entries-logging option. Default value: true
--skip-duplicate-nodes <true/false>
	Whether or not to skip importing nodes that have the same id/group. In the event 
	of multiple nodes within the same group having the same id, the first 
	encountered will be imported whereas consecutive such nodes will be skipped. 
	Skipped nodes will be logged, containing at most number of entities specified by 
	bad-tolerance, unless otherwise specified by skip-bad-entries-loggingoption. 
	Default value: false
--ignore-extra-columns <true/false>
	Whether or not to ignore extra columns in the data not specified by the header. 
	Skipped columns will be logged, containing at most number of entities specified 
	by bad-tolerance, unless otherwise specified by skip-bad-entries-loggingoption. 
	Default value: false
--db-config <path/to/neo4j.conf>
	(advanced) File specifying database-specific configuration. For more information 
	consult manual about available configuration options for a neo4j configuration 
	file. Only configuration affecting store at time of creation will be read. 
	Examples of supported config are:
	dbms.relationship_grouping_threshold
	unsupported.dbms.block_size.strings
	unsupported.dbms.block_size.array_properties
--additional-config <path/to/neo4j.conf>
	(advanced) File specifying database-specific configuration. For more information 
	consult manual about available configuration options for a neo4j configuration 
	file. Only configuration affecting store at time of creation will be read. 
	Examples of supported config are:
	dbms.relationship_grouping_threshold
	unsupported.dbms.block_size.strings
	unsupported.dbms.block_size.array_properties
--legacy-style-quoting <true/false>
	Whether or not backslash-escaped quote e.g. \" is interpreted as inner quote. 
	Default value: true
--read-buffer-size <bytes, e.g. 10k, 4M>
	Size of each buffer for reading input data. It has to at least be large enough 
	to hold the biggest single value in the input data. Default value: 4194304
--max-memory <max memory that importer can use>
	(advanced) Maximum memory that importer can use for various data structures and 
	caching to improve performance. If left as unspecified (null) it is set to 90% 
	of (free memory on machine - max JVM memory). Values can be plain numbers, like 
	10000000 or e.g. 20G for 20 gigabyte, or even e.g. 70%.
Example:
	bin/neo4j-import --into retail.db --id-type string --nodes:Customer customers.csv 
	--nodes products.csv --nodes orders_header.csv,orders1.csv,orders2.csv 
	--relationships:CONTAINS order_details.csv 
	--relationships:ORDERED customer_orders_header.csv,orders1.csv,orders2.csv

Cypher Shell