You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向apoc.cypher.runFile调用的Cypher文件传递JSON路径变量?

How to Pass Dynamic JSON File Path to apoc.cypher.runFile via JdbcOperations

Got it, let's fix this dynamic path issue step by step. The core problem here is that when using apoc.cypher.runFile, you can't just inject variables into the loaded Cypher script via string concatenation—you need to use APOC's built-in parameter passing mechanism for script files. Here's how to do it properly:

1. Update Your Cypher Script to Accept a Parameter

First, modify your .cypher file to use a named parameter instead of hardcoding the JSON path. Replace your existing apoc.load.json line with:

CALL apoc.load.json($jsonFilePath) YIELD value AS item

The $jsonFilePath is a placeholder that will be populated with your dynamic value later.

2. Pass the Parameter via JdbcOperations

Adjust your Java code to pass the dynamic path as a parameter to apoc.cypher.runFile. The method accepts a configuration map where you can define params to pass values to the loaded script:

String cypherScriptPath = "/path/to/your/script.cypher";
String dynamicJsonPath = "/your/dynamic/json/path/data.json";

jdbcOperations.update(
    "CALL apoc.cypher.runFile(?, {params: {jsonFilePath: ?}})",
    new Object[]{cypherScriptPath, dynamicJsonPath}
);

Breakdown of this code:

  • The first ? is a placeholder for your Cypher script file path.
  • The second ? maps to the jsonFilePath parameter in the params map, which will replace $jsonFilePath in your Cypher script.

3. Why Your Original Approach Failed

When you tried concatenating the JSON path directly into the apoc.cypher.runFile call, the loaded Cypher script doesn't have access to that concatenated value—it runs in a separate context. APOC expects parameters to be explicitly passed via the params configuration key, which ensures the script can resolve them correctly.

Important Notes

  • Make sure your APOC version supports parameter passing for runFile (all recent versions do, but if you're on an older release, you might need to upgrade).
  • Verify Neo4j's file access permissions are set correctly: ensure apoc.file.enabled=true in your Neo4j configuration, and the user running Neo4j has read access to both the Cypher script and JSON file paths.

内容的提问来源于stack exchange,提问作者Mark Davydov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:23:49