Cypher-shell

Cypher-shell

今天帮人解决一个问题,要把命令写成cypher脚本,好家伙,学了这么久neo4j,还没用过cypher脚本。开始必应:

[Toc]

1 cypher脚本

  • neo4j之通过脚本实现cypher任务 - (jianshu.com)

    参考:
    https://neo4j.com/docs/operations-manual/current/tools/cypher-shell/

    脚本

    examples.cypher

    MATCH (n) RETURN n;
    MATCH (batman:Person {name: 'Bruce Wayne'}) RETURN batman;
    

    通过脚本执行任务

    $neo4j-home> cat examples.cypher | bin/cypher-shell -u neo4j -p secret --format plain
    

    作者:王金松
    链接:https://www.jianshu.com/p/e58b110d5297
    来源:
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    就是把cypher命令封装到后缀为.cypher的文件中,然后就可以用shell运行这个脚本,执行cypher命令。

    看一下上面文章的参考链接Cypher Shell - Operations Manual (neo4j.com),是neo4j的官方手册,关于cypher shell的。

    应该是可以用这个cypher-shell运行cypher 脚本。

2 cypher-shell

官方手册Cypher Shell - Operations Manual (neo4j.com)一般比较靠谱:

1. About Cypher Shell CLI

Cypher Shell is a command-line tool that comes with the Neo4j distribution. It can also be downloaded from Neo4j Download Center and installed separately.

Cypher Shell CLI is used to run queries and perform administrative tasks against a Neo4j instance. By default, the shell is interactive, but you can also use it for scripting, by passing cypher directly on the command line or by piping a file with cypher statements (requires PowerShell on Windows). It communicates via the Bolt protocol.

Cypher Shell是Neo4j发行版附带的一个命令行工具。也可以从Neo4j Download Center下载并单独安装。Cypher Shell CLI用于对Neo4j实例运行查询和执行管理任务。默认情况下,shell是交互式的,但是您也可以将它用于脚本编写,方法是直接在命令行上传递cypher,或者使用cypher语句连接到文件(在Windows上需要PowerShell)。它通过Bolt协议进行通信。

6. Running Cypher statements

You can run Cypher statements in the following ways:

  • Typing Cypher statements directly into the interactive shell.
  • Running Cypher statements from a file with the interactive shell.
  • Running Cypher statements from a file as a cypher-shell argument.

您可以通过以下方式运行Cypher语句:

  • 直接在交互式shell中输入Cypher语句。
  • 使用交互式shell从文件运行Cypher语句。
  • 从文件中作为密码shell参数运行Cypher语句。

cypher shell 是neo4j出的一个终端,可以直接运行cypher命令,也可以执行cypher脚本,还可以通过其他终端以运行cypher-shell命令的方式,运行cypher命令(有点绕)。

下面看看如何运行

3 cypher shell运行cypher

3.1 安装cypher-shell

在安装过程中,发现,其实更早还有一个neo4j-shell,不过在3.0版本后就不支持了.

From Neo4j 3.0 access to neo4j-shell is no longer possible from the desktop-installers for Windows and OSX.

To use neo4j-shell, you have to download the TAR/ZIP distribution from: http://neo4j.com/download/other-releases/

  • 下载shell

    Neo4j Download Center - Neo4j Graph Data Platform

  • 解压

    完成下载后,把压缩包放到合适位置,解压即可。

3.2 运行

Ⅰ 直接运行

直接在power shell输入命令,显示如下:

PS C:\Users\w> cypher-shell
Unable to connect to localhost:7687, ensure the database is running and that there is a working network connection to it

看样子要先启动neo4j,启动后再输入cypher-shell,成功启动:

PS C:\Users\w> cypher-shell
username: neo4j
password: ************
Connected to Neo4j 4.0.11 at neo4j://localhost:7687 as user neo4j.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
[email protected]>
  • 可以看到当前的账户名为neo4j,数据库为jobgraph.db,这就是进入cypher shell了。

  • 也可以在命令中直接用相应参数带上账户名和密码,具体参见官方手册中参数部分:

    cypher-shell  [-h, --help]
                 [-a ADDRESS, --address ADDRESS]
                 [-u USERNAME, --username USERNAME]
                 [-p PASSWORD, --password PASSWORD]
                 [--encryption {true,false,default}]
                 [-d DATABASE, --database DATABASE]
                 [--format {auto,verbose,plain}]
                 [-P PARAM, --param PARAM]
                 [--debug]
                 [--non-interactive]
                 [--sample-rows SAMPLE-ROWS]
                 [--wrap {true,false}]
                 [-v, --version]
                 [--driver-version]
                 [-f FILE, --file FILE]
                 [--change-password]
                 [--fail-fast]
                 [--fail-at-end]
                 [cypher]
    

运行cypher命令试一下:

[email protected]> CREATE (:Person {name : 'Dick Grayson'});
0 rows available after 71 ms, consumed after another 0 ms
Added 1 nodes, Set 1 properties, Added 1 labels
[email protected]> MATCH (n:Person) RETURN n;
+-----------------------------------+
| n                                 |
+-----------------------------------+
| (:Person {name: "John", age: 30}) |
| (:Person {name: "John", age: 30}) |
| (:Person {name: "John", age: 30}) |
| (:Person {name: "Dick Grayson"})  |
+-----------------------------------+

4 rows available after 84 ms, consumed after another 0 ms

注意:跟browser不同的是,这里cypher命令之后一定要加分号;否则无法运行,只能换行!!!

Ⅱ 运行cypher脚本:

  • 先在vscode写一个cypher脚本:


    create.png
  • 脚本保存在了桌面,绝对路径为C:\Users\w\Desktop\create.cypher

  • 在cypher-shell界面使用source命令运行脚本:

    [email protected]> :source C:\Users\w\Desktop\create.cypher
    0 rows available after 285 ms, consumed after another 0 ms
    Added 1 nodes, Set 2 properties, Added 1 labels
    [email protected]>
    

退出cypher-shell:

[email protected]> :exit

Bye!
PS C:\Users\w>

Ⅲ 用powershell运行cypher脚本:

  • 再写一个vscode脚本:

    match.png
  • 在power-shell界面使用cypher-shell命令运行脚本,报错啦。。。:

    PS C:\Users\w> type match.cypher | bin/cypher-shell.bat -u neo4j -p  --format plain
    所在位置 行:1 字符: 54
    + type match.cypher | bin/cypher-shell.bat -u neo4j -p  --for ...
    +                                                      ~
    “<”运算符是为将来使用而保留的。
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : RedirectionNotSupported
    
    • 切换到match.cypher所在路径;修改参数 -u -p 为自己的账户名和密码,还是报错:
    PS C:\Users\w\Desktop> type match.cypher | bin/cypher-shell.bat -u neo4j -p  --format plain
    bin/cypher-shell.bat : 无法将“bin/cypher-shell.bat”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼
    写,如果包括路径,请确保路径正确,然后再试一次。
    所在位置 行:1 字符: 21
    + type match.cypher | bin/cypher-shell.bat -u neo4j -p casia123456. --f ...
    +                     ~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (bin/cypher-shell.bat:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    PS C:\Users\w\Desktop>
    
    • 修改.bat的路径,运行成功:
    PS C:\Users\w> cd .\Desktop\
    PS C:\Users\w\Desktop>
    PS C:\Users\w\Desktop> type match.cypher | cypher-shell.bat -u neo4j -p  --format plain
    n
    (:Person {name: "John", age: 30})
    (:Person {name: "John", age: 30})
    (:Person {name: "John", age: 30})
    (:Person {name: "Dick Grayson"})
    (:Person {name: "鑿插埄鏅?", age: 30})
    PS C:\Users\w\Desktop>
    

    因为编码问题,出现乱码,乱码应该是“菲利普”。

    • 乱码解决方案:windows 控制台 cmd/PowerShell 中文显示乱码,解决方法 | 安迪陈技术日志,架构、感悟、系统分析、团队管理 | 自强不息,厚德载物 (lovesofttech.com)

4 参考链接

  • neo4j之通过脚本实现cypher任务 - (jianshu.com)
  • Cypher Shell - Operations Manual (neo4j.com)
  • Neo4j Download Center - Neo4j Graph Data Platform
  • windows 控制台 cmd/PowerShell 中文显示乱码,解决方法 | 安迪陈技术日志,架构、感悟、系统分析、团队管理 | 自强不息,厚德载物 (lovesofttech.com)

你可能感兴趣的:(Cypher-shell)