Update 20170717 Neo4j and graph databases Getting started.md

This commit is contained in:
happygeorge01 2017-12-19 09:10:01 +08:00 committed by GitHub
parent c69d987e9d
commit de3ec13053

View File

@ -1,120 +1,114 @@
happygeorge01 is translating
Neo4j and graph databases: Getting started
Neo4j 和图数据库: 开始
============================================================
### In the second of a three-part series, install Neo4j and start using the web client to insert and query data in the graph.
### 这是三篇文章系列中的第二篇安装Neo4j并通过使用网页端在图中插入和查询数据。
![Neo4j and graph databases: Getting started](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/LIFE_wavegraph.png?itok=z4pXCf_c "Neo4j and graph databases: Getting started")
Image by : 
![Neo4j 和图数据库: 开始](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/LIFE_wavegraph.png?itok=z4pXCf_c "Neo4j and graph databases: Getting started")
图片来源 : 
opensource.com
In the [first article][8] in this series, we explored some core concepts of graph databases. This time, we'll install the [Neo4j][9] application and begin using the web client to insert and query data in the graph.
在系列的 [第一篇][8] 中,我们介绍了图数据库中的一些核心概念。在这篇,我们将安装 [Neo4j][9] 并通过网页端在图中插入并查询数据。
To download the Community Edition of Neo4J [head for their website][10]! You can download a package for Windows or OSX that will work just fine for testing, and there are links for installations on most Linux distros, and via Docker.
下载社区版的Neo4j可通过此 [传送门][10]你可以下载Windows或OSX版来测试也有各Linux发行版对应的版本还有Docker版。
I'll be installing the software on Debian 9 (stretch). You can get [full instructions here][11]. If you're running Debian 8 (jessie) or older, you can install the current Neo4j package, but it will be a bit more difficult as Neo4j requires the Java 8 runtime, which is not packaged with jessie.
我会在Debian 9 (stretch)上安装软件。你可通过 [传送门][11] 来查看完整说明。如果你正在使用Debian 8 (jessie)或更老的版本你可以安装现在的版本但会出现的问题是jessie中并没有安装Neo4j运行所需要的Java 8环境。
```
wget -O - https://debian.neo4j.org/neotechnology.gpg.key | sudo apt-key add - echo 'deb https://debian.neo4j.org/repo stable/' | sudo tee /etc/apt/sources.list.d/neo4j.list sudo apt-get update sudo apt-get install neo4j
```
On my system, I had to create **/var/run/neo4j**, for some reason, and then it started easily. I got a warning about maximum open files, but that turned out to be a non-issue since this is just a test. By default, Neo4j will listen for connections only on localhost. This is fine, if your Debian box is a desktop, but mine isn't. I edited **/etc/neo4j/neo4j.conf** and uncommented this line:
在我的系统中,因为有些不可知的原因,只有在我创建 **/var/run/neo4j** 之后才会它正常启动。Neo4j 也给了最大打开文件数 (maximum open files) 的警告但因为是测试环境所以不太需要关心这个问题。Neo4j 默认只会监听本机 localhost 。无论我使用的是服务器而你使用的 Debian 是台式机都是可以的。同时我在 **/etc/neo4j/neo4j.conf** 中取消注释了下面这行:
```
dbms.connectors.default_listen_address=0.0.0.0
```
After stopping and restarting Neo4j, I was able to connect by browsing to the server on port 7474\. The default password for the Neo4j user is **neo4j**; you'll have to set a new password, then the startup screen will display:
### [article_2_image_1.jpg][1]
在重启 Neo4j 之后,我可以通过 7474 端口来访问服务器的 Neo4j 服务。默认的用户名和密码是 Neo4j 和 **neo4j** 你需要设置一个新密码,然后会出现初始页面:
### [Installing.jpg][1]
![Installing Neo4J](https://opensource.com/sites/default/files/u128651/article_2_image_1.jpg "Installing Neo4J")
Let's use the graph from the last article, and create it in Neo4j. Here it is again:
我们在Neo4j上创建上篇文章中使用过的图。如下图
### [article_1_image_2.jpg][2]
### [图数据库节点图_2.jpg][2]
![Graph database image 2, defining a new type of node](https://opensource.com/sites/default/files/u128651/article_1_image_2.jpg "Graph database image 2, defining a new type of node")
Like MySQL and other database systems, Neo4j uses a query system for all operations. Cypher, the query language of Neo4j, has some syntactic quirks that take a little getting used to. Nodes are always encased in parentheses and relationships in square brackets. Since that's the only type of data there is, that's all you need.
类似 MySQL 和其他数据库系统Neo4j 的各类操作也使用一套查询语句。Cypher就是 Neo4j 使用的查询语句,但有一些语法区别需要去学习和注意。节点 (Nodes)需要用圆括号表示而关系 (relationships) 需要放在方括号中。因为这是系统中唯二的数据类型,所以了解这些就够了。
First, let's create all the nodes. You can copy and paste these into the top box in the browser window, which is where queries are run.
首先,我们创建所有的节点。你需要将下面内容复制黏贴到浏览器顶部用以运行查询语句的区域内。
```
CREATE (a:Person { name: 'Jane Doe', favorite_color: 'purple' }) CREATE (b:Person { name: 'John Doe' }) CREATE (c:Person { name: 'Mary Smith', favorite_color: 'red', dob: '1992-11-09' }) CREATE (d:Person { name: 'Robert Roe' }) CREATE (e:Person { name: 'Rhonda Roe' }) CREATE (f:Person { name: 'Ryan Roe' }) CREATE (t:City { name: 'Petaluma, CA' }) CREATE (u:City { name: 'Cypress, TX' }) CREATE (v:City { name: 'Grand Prairie, TX' }) CREATE (w:City { name: 'Houston, TX' })
```
Note that the letter before the label is a variable. These will show up elsewhere; they're not useful here, but you can't just blindly create without assignment, so we'll use them and then discard them.
注意,在标签前的字符就是变量。这些信息会在各处展示,但我们在这里并不会用到。但你不能不指定 (without assignment) 相应信息就盲目创建,所以我们使用它们然后就忽略它们。
You should be told that 10 nodes were created and 13 properties set. Want to see them? Here's a query that matches and returns all nodes:
在上面一共创建了10个节点和13个属性。想查看它们通过下面语句来匹配查询所有节点
```
MATCH (n) RETURN n
```
This will return a visual graph. (Within the app, you can use the "fullscreen" icon on the returned graph to see the whole thing.) You'll see something like this:
这条语句会返回一个可视化图像。(在应用内,你可以在返回的图像中使用”全屏”按钮来查看大图)。你将会看到类似下面的图像:
### [article_2_image_2.jpg][3]
### [可视化.jpg][3]
![Visual graph](https://opensource.com/sites/default/files/u128651/article_2_image_2.jpg "Visual graph")
Adding a relationship is a little bit trickier; you must have the nodes you want to connect "in scope," meaning within the scope of the current query. The variables we used earlier have gone out of scope, so let's find John and Jane and marry them:
添加关系需要一点技巧;你需要连接的节点必须是 “在限定范围内的 (in scope)”,意思连接的节点是在当前查询语句所限定的范围内的。我们之前使用的查询语句范围太大,所以让我们找到 John 和 Jane 并让他们结婚:
```
MATCH (a:Person),(b:Person) WHERE a.name='Jane Doe' AND b.name='John Doe' CREATE (a)-[r:MARRIAGE {date: '2017-03-04', place: 'Houston, TX'}]->(b)
```
This query will set two properties and create one relationship. Re-running the MATCH query shows that relationship. You can point your mouse arrow at any of the nodes or relationships to see the properties for that item.
这条语句会创建一个惯性并设置两个属性。重新运行 MATCH 语句会显示那个关系。你可以通过鼠标点击任意的节点或关系来查看它们的属性。
Let's add the rest of the relationships. Rather than doing a bunch of MATCH statements, I'm going to do it once and CREATE multiple relationships from them.
我们来添加其他的关系。比起使用一些列的 MATCH 语句,我会一次性做完并从中 CREATE 创建多个关系。
```
MATCH (a:Person),(b:Person),(c:Person),(d:Person),(e:Person),(f:Person),(t:City),(u:City),(v:City),(w:City) WHERE a.name='Jane Doe' AND b.name='John Doe' AND c.name='Mary Smith' AND d.name='Robert Roe' AND e.name='Rhonda Roe' AND f.name='Ryan Roe' AND t.name='Petaluma, CA' AND u.name='Cypress, TX' AND v.name='Grand Prairie, TX' AND w.name='Houston, TX' CREATE (d)-[m2:MARRIAGE {date: '1990-12-01', place: 'Chicago, IL'}]->(e) CREATE (a)-[n:CHILD]->(c) CREATE (d)-[n2:CHILD]->(f) CREATE (e)-[n3:CHILD]->(f) CREATE (b)-[n4:STEPCHILD]->(c) CREATE (a)-[o:BORN_IN]->(v) CREATE (b)-[o2:BORN_IN]->(t) CREATE (c)-[p:DATING]->(f) CREATE (a)-[q:LIVES_IN]->(u) CREATE (b)-[q1:LIVES_IN]->(u) CREATE (a)-[r:WORKS_IN]->(w) CREATE (a)-[s:FRIEND]->(d) CREATE (a)-[s2:FRIEND]->(e)
```
Re-query with the MATCH statement, and you should have a graph like this:
重新运行 MATCH 语句,你将会看到下面图像:
### [article_2_image_3.jpg][4]
### [Match查询.jpg][4]
![Graph after re-querying with MATCH](https://opensource.com/sites/default/files/u128651/article_2_image_3.jpg "Graph after re-querying with MATCH")
If you want, you can drag the nodes around and end up with the same graph as my drawing from before.
如果你喜欢,你可以将节点拖拉成想我之前画的图的样子。
In this example, the only MATCH we've done is to MATCH everything. Here's a query that will return the two married couples and show the relationship between them:
在这个例子中,我们唯一使用的 MATCH 就是 MATCH 所有的东西。下面这个查询会返回两个结婚了的夫妻并显示他们之间的关系:
```
MATCH (a)-[b:MARRIAGE]->(c) RETURN a,b,c
```
With a more elaborate graph, you could do much more detailed searching. For instance, if you had a graph of Movie and Person nodes, and the relationships were roles like ACTED IN, DIRECTED, WROTE SCREENPLAY, and so forth, you could do queries like this:
在一个更复杂的图中,你可以做更多的细节查询。(译者注此例子为Neo4j自带例子的) 例如,你有关于电影和人的节点,还有像 ACTED IN, DIRECTED, WROTE SCREENPLAY 等属性的关系,你可以运行下面这个查询:
```
MATCH (p:Person { name: 'Mel Gibson' })--(m:Movie) RETURN m.title
```
...and get back a list of films connected to Mel Gibson in any way. But if you only wanted movies where he was an actor, this query would be more useful:
... 上述是查询和Mel Gibson相关的所有影片。但如果你想查询他演过的所有电影下面这条语句会更有用
```
MATCH (p:Person { name: 'Mel Gibson' })-[r:ACTED_IN]->(m:movie) RETURN m.title,r.role
```
Fancier Cypher queries are possible, of course, and we're just scratching the surface here. The full documentation for the Cypher language is available [at the Neo4j website][12], and it has lots of examples to work with.
还有更多更炫酷的Cypher语句可以使用但我们就简单介绍这些。更详细完整的 Cypher 语句可以在 Neo4j 的[官网][12]上查看, 并且也有很多例子可以练习。
In the next article in this series, we'll write a little Perl script to create this same graph to show how to use graph databases in an application.
### About the author
Ruth Holloway - Ruth Holloway has been a system administrator and software developer for a long, long time, getting her professional start on a VAX 11/780, way back when. She spent a lot of her career (so far) serving the technology needs of libraries, and has been a contributor since 2008 to the Koha open source library automation suite.Ruth is currently a Perl Developer at cPanel in Houston, and also serves as chief of staff for an obnoxious cat. In her copious free time, she occasionally reviews old romance... [more about Ruth Holloway][6]
在此系列的下篇文章中,我们会通过写些 Perl 脚本来展示如何在应用中使用图数据库。
--------------------------------------------------------------------------------
via: https://opensource.com/article/17/7/neo4j-graph-databases-getting-started
作者:[Ruth Holloway ][a]
译者:[译者ID](https://github.com/译者ID)
译者:[happygeorge01](https://github.com/happygeorge01)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出