Cypher-shell

作者: 王十三 | 来源:发表于2022-05-23 15:48 被阅读0次

Cypher-shell

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

[Toc]

1 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/

  • 解压

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

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.
neo4j@jobgraph.db>
  • 可以看到当前的账户名为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命令试一下:

neo4j@jobgraph.db> 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
neo4j@jobgraph.db> 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命令运行脚本:

    neo4j@jobgraph.db> :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
    neo4j@jobgraph.db>
    

退出cypher-shell:

neo4j@jobgraph.db> :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 <password> --format plain
    所在位置 行:1 字符: 54
    + type match.cypher | bin/cypher-shell.bat -u neo4j -p <password> --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 <mypassword> --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 <mypassword> --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>
    

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

4 参考链接

相关文章

  • Cypher-shell

    Cypher-shell 今天帮人解决一个问题,要把命令写成cypher脚本,好家伙,学了这么久neo4j,还没用...

网友评论

    本文标题:Cypher-shell

    本文链接:https://www.haomeiwen.com/subject/zqirprtx.html