0Pricing
Neo4j Graph Database Fundamentals · 课时

导入与导出数据

了解从外部来源将数据加载到 Neo4j,以及导出图数据供其他用途使用的多种方法

导入与导出数据 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Moving Data In & Out

In this lesson, we'll explore how to get data into your Neo4j graph database and how to extract it for other uses. This is crucial for integrating Neo4j with existing systems, migrating data, or preparing it for analysis.

Importing Data: An Overview

Bringing external data into Neo4j is a common task. There are several powerful methods:

  • LOAD CSV: For structured data from CSV files.
  • APOC Procedures: For more complex formats like JSON, XML, or data from external databases.
  • Connectors & Drivers: Programmatic import via application code.

We'll focus on the first two methods.

LOAD CSV: The Basics

The LOAD CSV Cypher clause is your go-to for importing data from CSV files. It lets you specify the file path, handle headers, and access each row's data.

Here's a basic structure to read a CSV and return its rows:

LOAD CSV WITH HEADERS FROM 'file:///users.csv' AS row
RETURN row;

Creating Nodes from CSV

Let's say you have a users.csv file with id,name,email columns. You can use LOAD CSV to create User nodes:

LOAD CSV WITH HEADERS FROM 'file:///users.csv' AS row
CREATE (:User {id: row.id, name: row.name, email: row.email});

Creating Relationships from CSV

You can also create relationships. If you have a follows.csv with followerId,followedId, you can connect existing User nodes:

  • We use MATCH to find the existing nodes.
  • CREATE then forms the relationship between them.
LOAD CSV WITH HEADERS FROM 'file:///follows.csv' AS row
MATCH (u1:User {id: row.followerId})
MATCH (u2:User {id: row.followedId})
CREATE (u1)-[:FOLLOWS]->(u2);

Beyond CSV: APOC for Import

For more complex import scenarios, such as JSON or XML files, or data from external databases, the APOC (Awesome Procedures On Cypher) library is invaluable. APOC extends Cypher with hundreds of new procedures and functions.

You'll need to install the APOC plugin for your Neo4j instance to use these features.

Importing JSON with APOC

If you have data in a JSON file, like products.json, you can use apoc.load.json to parse it. Each item in the JSON array can then be processed to create nodes and relationships.

CALL apoc.load.json('file:///products.json') YIELD value
CREATE (:Product {id: value.id, name: value.name, price: toFloat(value.price)});

Exporting Data: An Overview

Just as important as importing is exporting data from your graph. You might need to:

  • Generate reports.
  • Migrate data to another system.
  • Perform advanced analytics outside Neo4j.
  • Create backups.

We'll look at basic Cypher export and advanced APOC export.

Basic Export with Cypher

The simplest way to export data is using standard Cypher MATCH and RETURN statements. The results are displayed in the Neo4j Browser and can be copied or downloaded as CSV/JSON.

MATCH (u:User)-[:FOLLOWS]->(f:User)
RETURN u.name AS Follower, f.name AS Followed;

Advanced Export with APOC

APOC offers powerful procedures to export your graph data directly to files in various formats (CSV, JSON, GraphML, etc.). These are ideal for full graph exports or specific subgraphs.

For example, to export all nodes and relationships to a CSV file:

CALL apoc.export.csv.all('full_graph_export.csv', {});

Import/Export Challenge

You have a CSV file named books.csv with the columns isbn, title, author. You want to import this data into Neo4j, creating nodes with the label Book and properties for each column.

Recap: Data Movement Skills

You've learned essential skills for moving data in and out of Neo4j:

  • LOAD CSV is powerful for structured data.
  • APOC procedures extend capabilities for JSON, complex exports, and more.
  • Standard Cypher MATCH and RETURN allow simple data extraction.

Mastering these techniques is key for managing your graph data effectively!

常见问题解答

「导入与导出数据」课时是免费的吗?

是的 — 「导入与导出数据」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

「导入与导出数据」这节课中我会学到什么?

了解从外部来源将数据加载到 Neo4j,以及导出图数据供其他用途使用的多种方法 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Neo4j Graph Database Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「导入与导出数据」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?

能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Neo4j 浏览器与管理工具
  2. 导入与导出数据
  3. 备份与恢复策略
  4. Neo4j 监控与日志记录
← 返回 Neo4j Graph Database Fundamentals