导入ClickHouse数据

本文以一个示例说明如何使用Exchange将存储在ClickHouse上的数据导入Nebula Graph。

数据集

本文以basketballplayer数据集为例。

环境配置

本文示例在MacOS下完成,以下是相关的环境配置信息:

  • 硬件规格:

    • CPU:1.7 GHz Quad-Core Intel Core i7
    • 内存:16 GB
  • Spark:2.4.7,单机版

  • Hadoop:2.9.2,伪分布式部署

  • ClickHouse:docker部署yandex/clickhouse-server tag: latest(2021.07.01)

  • Nebula Graph:2.6.1。使用Docker Compose部署

前提条件

开始导入数据之前,用户需要确认以下信息:

  • 已经安装部署Nebula Graph并获取如下信息:

    • Graph服务和Meta服务的的IP地址和端口。

    • 拥有Nebula Graph写权限的用户名和密码。

  • 已经编译Exchange。详情请参见编译Exchange。本示例中使用Exchange 2.6.0。

  • 已经安装Spark。

  • 了解Nebula Graph中创建Schema的信息,包括Tag和Edge type的名称、属性等。

  • 已经安装并开启Hadoop服务。

操作步骤

步骤 1:在Nebula Graph中创建Schema

分析数据,按以下步骤在Nebula Graph中创建Schema:

  1. 确认Schema要素。Nebula Graph中的Schema要素如下表所示。

    要素名称属性
    Tagplayername string, age int
    Tagteamname string
    Edge Typefollowdegree int
    Edge Typeservestart_year int, end_year int
  2. 在Nebula Graph中创建一个图空间basketballplayer,并创建一个Schema,如下所示。

    1. ## 创建图空间
    2. nebula> CREATE SPACE basketballplayer \
    3. (partition_num = 10, \
    4. replica_factor = 1, \
    5. vid_type = FIXED_STRING(30));
    6. ## 选择图空间basketballplayer
    7. nebula> USE basketballplayer;
    8. ## 创建Tag player
    9. nebula> CREATE TAG player(name string, age int);
    10. ## 创建Tag team
    11. nebula> CREATE TAG team(name string);
    12. ## 创建Edge type follow
    13. nebula> CREATE EDGE follow(degree int);
    14. ## 创建Edge type serve
    15. nebula> CREATE EDGE serve(start_year int, end_year int);

更多信息,请参见快速开始

步骤 2:修改配置文件

编译Exchange后,复制target/classes/application.conf文件设置ClickHouse数据源相关的配置。在本示例中,复制的文件名为clickhouse_application.conf。各个配置项的详细说明请参见配置说明

  1. {
  2. # Spark相关配置
  3. spark: {
  4. app: {
  5. name: Nebula Exchange 2.6.0
  6. }
  7. driver: {
  8. cores: 1
  9. maxResultSize: 1G
  10. }
  11. cores {
  12. max: 16
  13. }
  14. }
  15. # Nebula Graph相关配置
  16. nebula: {
  17. address:{
  18. # 以下为Nebula Graph的Graph服务和Meta服务所在机器的IP地址及端口。
  19. # 如果有多个地址,格式为 "ip1:port","ip2:port","ip3:port"。
  20. # 不同地址之间以英文逗号 (,) 隔开。
  21. graph:["127.0.0.1:9669"]
  22. meta:["127.0.0.1:9559"]
  23. }
  24. # 填写的账号必须拥有Nebula Graph相应图空间的写数据权限。
  25. user: root
  26. pswd: nebula
  27. # 填写Nebula Graph中需要写入数据的图空间名称。
  28. space: basketballplayer
  29. connection {
  30. timeout: 3000
  31. retry: 3
  32. }
  33. execution {
  34. retry: 3
  35. }
  36. error: {
  37. max: 32
  38. output: /tmp/errors
  39. }
  40. rate: {
  41. limit: 1024
  42. timeout: 1000
  43. }
  44. }
  45. # 处理点
  46. tags: [
  47. # 设置Tag player相关信息。
  48. {
  49. name: player
  50. type: {
  51. # 指定数据源文件格式,设置为ClickHouse。
  52. source: clickhouse
  53. # 指定如何将点数据导入Nebula Graph:Client或SST。
  54. sink: client
  55. }
  56. # ClickHouse的JDBC URL
  57. url:"jdbc:clickhouse://192.168.*.*:8123/basketballplayer"
  58. user:"user"
  59. password:"123456"
  60. # ClickHouse分区数
  61. numPartition:"5"
  62. sentence:"select * from player"
  63. # 在fields里指定player表中的列名称,其对应的value会作为Nebula Graph中指定属性。
  64. # fields和nebula.fields里的配置必须一一对应。
  65. # 如果需要指定多个列名称,用英文逗号(,)隔开。
  66. fields: [name,age]
  67. nebula.fields: [name,age]
  68. # 指定表中某一列数据为Nebula Graph中点VID的来源。
  69. vertex: {
  70. field:playerid
  71. # policy:hash
  72. }
  73. # 单批次写入 Nebula Graph 的数据条数。
  74. batch: 256
  75. # Spark 分区数量
  76. partition: 32
  77. }
  78. # 设置Tag team相关信息。
  79. {
  80. name: team
  81. type: {
  82. source: clickhouse
  83. sink: client
  84. }
  85. url:"jdbc:clickhouse://192.168.*.*:8123/basketballplayer"
  86. user:"user"
  87. password:"123456"
  88. numPartition:"5"
  89. sentence:"select * from team"
  90. fields: [name]
  91. nebula.fields: [name]
  92. vertex: {
  93. field:teamid
  94. }
  95. batch: 256
  96. partition: 32
  97. }
  98. ]
  99. # 处理边数据
  100. edges: [
  101. # 设置Edge type follow相关信息
  102. {
  103. # Nebula Graph中对应的Edge type名称。
  104. name: follow
  105. type: {
  106. # 指定数据源文件格式,设置为ClickHouse。
  107. source: clickhouse
  108. # 指定边数据导入Nebula Graph的方式,
  109. # 指定如何将点数据导入Nebula Graph:Client或SST。
  110. sink: client
  111. }
  112. # ClickHouse的JDBC URL
  113. url:"jdbc:clickhouse://192.168.*.*:8123/basketballplayer"
  114. user:"user"
  115. password:"123456"
  116. # ClickHouse分区数
  117. numPartition:"5"
  118. sentence:"select * from follow"
  119. # 在fields里指定follow表中的列名称,其对应的value会作为Nebula Graph中指定属性。
  120. # fields和nebula.fields里的配置必须一一对应。
  121. # 如果需要指定多个列名称,用英文逗号(,)隔开。
  122. fields: [degree]
  123. nebula.fields: [degree]
  124. # 在source里,将follow表中某一列作为边的起始点数据源。
  125. source: {
  126. field:src_player
  127. }
  128. # 在target里,将follow表中某一列作为边的目的点数据源。
  129. target: {
  130. field:dst_player
  131. }
  132. # 单批次写入 Nebula Graph 的数据条数。
  133. batch: 256
  134. # Spark 分区数量
  135. partition: 32
  136. }
  137. # 设置Edge type serve相关信息
  138. {
  139. name: serve
  140. type: {
  141. source: clickhouse
  142. sink: client
  143. }
  144. url:"jdbc:clickhouse://192.168.*.*:8123/basketballplayer"
  145. user:"user"
  146. password:"123456"
  147. numPartition:"5"
  148. sentence:"select * from serve"
  149. fields: [start_year,end_year]
  150. nebula.fields: [start_year,end_year]
  151. source: {
  152. field:playerid
  153. }
  154. target: {
  155. field:teamid
  156. }
  157. batch: 256
  158. partition: 32
  159. }
  160. ]
  161. }

步骤 3:向Nebula Graph导入数据

运行如下命令将ClickHouse数据导入到Nebula Graph中。关于参数的说明,请参见导入命令参数

  1. ${SPARK_HOME}/bin/spark-submit --master "local" --class com.vesoft.nebula.exchange.Exchange <nebula-exchange-2.6.0.jar_path> -c <clickhouse_application.conf_path>

Note

JAR包有两种获取方式:自行编译或者从maven仓库下载。

示例:

  1. ${SPARK_HOME}/bin/spark-submit --master "local" --class com.vesoft.nebula.exchange.Exchange /root/nebula-exchange/nebula-exchange/target/nebula-exchange-2.6.0.jar -c /root/nebula-exchange/nebula-exchange/target/classes/clickhouse_application.conf

用户可以在返回信息中搜索batchSuccess.<tag_name/edge_name>,确认成功的数量。例如batchSuccess.follow: 300

步骤 4:(可选)验证数据

用户可以在Nebula Graph客户端(例如Nebula Graph Studio)中执行查询语句,确认数据是否已导入。例如:

  1. GO FROM "player100" OVER follow;

用户也可以使用命令SHOW STATS查看统计数据。

步骤 5:(如有)在Nebula Graph中重建索引

导入数据后,用户可以在Nebula Graph中重新创建并重建索引。详情请参见索引介绍


最后更新: October 18, 2021