CSV文件导入示例

以下示例将指导您如何使用 Nebula Importer 将 CSV 数据导入到 Nebula Graph 中。在此示例中,Nebula Graph 通过 DockerDocker Compose 安装。我们将通过以下步骤引导您完成该示例:

启动 Nebula Graph 服务

您可以按照以下步骤启动 Nebula Graph 服务:

  1. 在命令行界面上,进入 nebula-docker-compose 目录。
  2. 执行以下命令以启动 Nebula Graph 服务:
  1. $ sudo docker-compose up -d
  1. 执行以下命令把 Nebula Graph 镜像文件下拉到本地:
  1. $ sudo docker pull vesoft/nebula-console:nightly
  1. 执行以下命令以连接 Nebula Graph 服务器:
  1. $ sudo docker run --rm -ti --network=host vesoft/nebula-console:nightly --addr=127.0.0.1 --port=3699

注意:您必须确保 IP 地址和端口号配置正确。

创建点和边的 Schema

在输入 schema 之前,必须创建一个空间并使用它。在此示例中,我们创建一个 nba 空间并使用它。

我们使用以下命令创建两个标签和两个边类型:

  1. nebula> CREATE TAG player (name string, age int);
  2. nebula> CREATE TAG team (name string);
  3. nebula> CREATE EDGE serve (start_year int, end_year int);
  4. nebula> CREATE EDGE follow (degree, int);

准备配置文件

您必须配置 .yaml 配置文件,该文件规定了 CSV 文件中数据的格式。在本例中,我们创建一个名为 config.yaml 的配置文件。

在此示例中,我们按以下方式配置 config.yaml 文件:

  1. version: v1rc1
  2. description: example
  3. clientSettings:
  4. concurrency: 2 # number of graph clients
  5. channelBufferSize: 50
  6. space: nba
  7. connection:
  8. user: user
  9. password: password
  10. address: 127.0.0.1:3699
  11. logPath: ./err/test.log
  12. files:
  13. - path: /home/nebula/serve.csv
  14. failDataPath: ./err/serve.csv
  15. batchSize: 10
  16. type: csv
  17. csv:
  18. withHeader: false
  19. withLabel: false
  20. schema:
  21. type: edge
  22. edge:
  23. name: serve
  24. withRanking: false
  25. props:
  26. - name: start_year
  27. type: int
  28. - name: end_year
  29. type: int
  30. - path: /home/nebula/follow.csv
  31. failDataPath: ./err/follow.csv
  32. batchSize: 10
  33. type: csv
  34. csv:
  35. withHeader: false
  36. withLabel: false
  37. schema:
  38. type: edge
  39. edge:
  40. name: follow
  41. withRanking: false
  42. props:
  43. - name: degree
  44. type: int
  45. - path: /home/nebula/player.csv
  46. failDataPath: ./err/player.csv
  47. batchSize: 10
  48. type: csv
  49. csv:
  50. withHeader: false
  51. withLabel: false
  52. schema:
  53. type: vertex
  54. vertex:
  55. tags:
  56. - name: player
  57. props:
  58. - name: name
  59. type: string
  60. - name: age
  61. type: int
  62. - path: /home/nebula/team.csv
  63. failDataPath: ./err/team.csv
  64. batchSize: 10
  65. type: csv
  66. csv:
  67. withHeader: false
  68. withLabel: false
  69. schema:
  70. type: vertex
  71. vertex:
  72. tags:
  73. - name: team
  74. props:
  75. - name: name
  76. type: string

注意

  • 在上面的配置文件中,您必须将 IP 地址和端口号更改为您自己的 IP 地址和端口号。

  • 您必须将 CSV 文件的目录更改为您自己的目录,否则,Nebula Importer 无法找到 CSV 文件。

准备 CSV 数据

在此示例中,我们准备了四个 CSV 数据文件:player.csvteam.csvserve.csv 以及 follow.csv

serve.csv 文件中的数据如下:

  1. 100,200,1997,2016
  2. 101,201,1999,2018
  3. 102,203,2006,2015
  4. 102,204,2015,2019
  5. 103,204,2017,2019
  6. 104,200,2007,2009

follow.csv 文件中的数据如下:

  1. 100,101,95
  2. 100,102,90
  3. 101,100,95
  4. 102,101,75
  5. 102,100,75
  6. 103,102,70
  7. 104,101,50
  8. 104,105,60
  9. 105,104,83

player.csv 文件中的数据如下:

  1. 100,Tim Duncan,42
  2. 101,Tony Parker,36
  3. 102,LaMarcus Aldridge,33
  4. 103,Rudy Gay,32
  5. 104,Marco Belinelli,32
  6. 105,Danny Green,31
  7. 106,Kyle Anderson,25
  8. 107,Aron Baynes,32
  9. 108,Boris Diaw,36

team.csv 文件中的数据如下:

  1. 200,Warriors
  2. 201,Nuggets
  3. 202,Rockets
  4. 203,Trail
  5. 204,Spurs
  6. 205,Thunders
  7. 206,Jazz
  8. 207,Clippers
  9. 208,Kings

注意

  • servefollow CSV 文件中,第一列是起始点 ID、第二列是目标点 ID,其他列与config.yaml文件一致。

  • playerteam CSV 文件中,第一列是点 ID,其他列与 config.yaml 文件一致。

导入 CSV 数据

完成上述所有四个步骤后,您可以使用 DockerGo 导入 CSV 数据。

使用 Go-importer 导入 CSV 数据

请参见Nebula Importer