数据导入配置

在我们关于使用自定义图数据的指南中,我们介绍了通过简单YAML配置导入图数据的基础知识。本节将深入探讨,全面解析数据导入时可用的丰富配置选项。

支持的数据源

目前我们仅支持从本地csv文件或odps表导入数据到graph。请参阅配置项loading_config.data_source.scheme

列映射

将顶点和边数据导入图时,用户必须定义原始数据如何映射到图的模式。 这可以通过YAML配置完成,如下所示:

- column:
    index: 0  # Column index in the data source
    name: col_name # If a column name is present
  property: property_name  # The mapped property name

列映射要求根据数据源的不同而有所差异:

从CSV导入

您可以提供indexname或两者都提供。如果同时指定了indexname,我们将检查它们是否匹配。

从ODPS表导入

您只需指定column的名称即可,因为在ODPS表中名称保证是唯一的。index会被忽略。

从CSV文件加载“现代”图的示例配置

举例来说,让我们查看modern_graph_import.yaml文件。该配置文件专为导入"modern"图而设计,展示了完整的配置可能性。我们将在后续章节中逐一解析每个配置项。

注意

请注意,路径前添加了@符号,表示所有文件都将从本地进行上传。

loading_config: 
  x_csr_params:
    parallelism: 1
    build_csr_in_mem: true
    use_mmap_vector: true
  data_source:
    scheme: file
    location: "@/home/modern_graph/"
  format: 
    metadata: 
      delimiter: "|"  
      header_row: true
      quoting: false
      quote_char: '"'
      double_quote: true
      escape_char: '\'
      block_size: 4MB
vertex_mappings: 
  - type_name: person  
    inputs: 
      - person.csv  
    column_mappings:  
      - column:
          index: 0  
          name: id 
        property: id 
      - column:
          index: 1 
          name: name
        property: name
      - column:
          index: 2
          name: age
        property: age
  - type_name: software
    inputs:
      - software.csv
    column_mappings:
      - column:
          index: 0      
          name: id  
        property: id  
      - column:
          index: 1
          name: name
        property: name
      - column:
          index: 2
          name: lang
        property: lang
edge_mappings: 
  - type_triplet:
      edge: knows  
      source_vertex:  person 
      destination_vertex:  person  
    inputs: 
      - person_knows_person.csv 
    source_vertex_mappings:
      - column:  
          index: 0  
          name: src_id
        property: id
    destination_vertex_mappings:
      - column:  
          index: 1  
          name: dst_id
        property: id
    column_mappings:
      - column:
          index: 2
          name: weight
        property: weight
  - type_triplet:
      edge: created
      source_vertex: person
      destination_vertex: software
    inputs:
      -  person_created_software.csv
    source_vertex_mappings:
      - column:
          index: 0
          name: id
    destination_vertex_mappings:
      - column:
          index: 1
          name: id
    column_mappings:
      - column:
          index: 2
          name: weight
        property: weight

从ODPS表加载“现代图”的示例配置

graph: dd_graph
loading_config:
  data_source:
    scheme: odps  # file, odps
  import_option: init # init, overwrite
  format:
    type: arrow
vertex_mappings:
  - type_name: person  # must align with the schema
    inputs:
      - your_proj_name/table_name/partition_col_name=partition_name
    column_mappings:  
          - column:
              index: 0  
              name: id 
            property: id 
          - column:
              index: 1 
              name: name
            property: name
          - column:
              index: 2
              name: age
            property: age
  - type_name: software
    inputs:
      - your_proj_name/table_name/partition_col_name=partition_name
    column_mappings:
      - column:
          index: 0      
          name: id  
        property: id  
      - column:
          index: 1
          name: name
        property: name
      - column:
          index: 2
          name: lang
        property: lang
edge_mappings:
  - type_triplet:
      edge: knows
      source_vertex: person
      destination_vertex: person
    inputs:
      - your_proj_name/table_name/partition_col_name=partition_name
    source_vertex_mappings:
      - column:
          index: 0
          name: src_user_id
        property: id
    destination_vertex_mappings:
      - column:
          index: 1
          name: dst_user_id
        property: id
    column_mappings:
      - column:
          index: 2
          name: weight
        property: weight
  - type_triplet:
      edge: created
      source_vertex: person
      destination_vertex: software
    inputs:
      - your_proj_name/table_name/partition_col_name=partition_name
    source_vertex_mappings:
      - column:
          index: 0
          name: id
    destination_vertex_mappings:
      - column:
          index: 1
          name: id
    column_mappings:
      - column:
          index: 2
          name: weight
        property: weight

配置详解

下表详细列出了每个配置项的说明。在表格中,我们使用“.”符号来表示YAML结构中的层级关系。

默认值

描述

必填

loading_config

N/A

加载配置

loading_config.data_source

N/A

存储原始数据的位置

loading_config.data_source.location

N/A

容器内数据源的路径,该路径必须在初始化服务时从主机映射

Yes

loading_config.scheme

文件

输入数据的来源。目前仅支持 fileodps

loading_config.format

N/A

原始CSV数据的格式

Yes

loading_config.format.metadata

N/A

主要用于配置读取CSV的选项

Yes

loading_config.format.metadata.delimiter

用于分割数据行的分隔符,也支持转义字符,例如'\t'

loading_config.format.metadata.header_row

true

指示第一行是否应作为表头使用

No

loading_config.format.metadata.quoting

false

是否使用引号

loading_config.format.metadata.quote_char

‘”’

引用字符(如果 quoting 为真)

loading_config.format.metadata.double_quote

true

值内的引号是否使用双引号

No

loading_config.format.metadata.escaping

false

是否使用转义

No

loading_config.format.metadata.escape_char

‘\’

转义字符(如果 escaping 为 true)

loading_config.format.metadata.null_values

[]

识别空值的拼写形式

No

loading_config.format.metadata.batch_size

4MB

从文件读取时的批次大小

No

loading_config.x_csr_params.parallelism

1

用于批量加载的线程数

No

loading_config.x_csr_params.build_csr_in_mem

false

是否完全在内存中构建csr

No

loading_config.x_csr_params.use_mmap_vector

false

是否使用mmap_vector而非mmap_array进行构建

No

vertex_mappings

N/A

定义如何将原始数据映射到图模式中的顶点

Yes

vertex_mappings.type_name

N/A

顶点类型的名称

vertex_mappings.inputs

不适用

包含顶点类型原始数据的文件列表,相对于 loading_config.data_source.location

vertex_mappings.column_mappings

原始数据中的列将按给定顺序自动映射到属性

定义顶点数据的哪一列映射到指定属性

No

vertex_mappings.column_mappings.column.index

N/A

原始数据中的列索引

No

vertex_mappings.column_mappings.column.name

N/A

原始数据中的列名

vertex_mappings.column_mappings.property

N/A

列映射到的schema中的属性。

No

edge_mappings

N/A

定义如何将原始数据映射到图模式中的边

edge_mappings.type_triplet

N/A

唯一标识一条边的三元组

edge_mappings.type_triplet.edge

N/A

边的类型

edge_mappings.type_triplet.source_vertex

N/A

边的源顶点类型

edge_mappings.type_triplet.destination_vertex

N/A

边的目标顶点类型

Yes

edge_mappings.inputs

不适用

包含边类型原始数据的文件列表,相对于 loading_config.data_source.location

edge_mappings.source_vertex_mappings

边数据的第一列

定义边数据的哪些列映射到其源顶点

edge_mappings.source_vertex_mappings.column.index

N/A

表示源顶点在原始数据中的列索引。

No

edge_mappings.source_vertex_mappings.column.name

N/A

表示源顶点的原始数据列名称。

No

edge_mappings.destination_vertex_mappings

边数据的第二列

定义边数据的哪些列映射到其源顶点

edge_mappings.destination_vertex_mappings.column.index

N/A

表示目标顶点的原始数据列索引。

No

edge_mappings.destination_vertex_mappings.column.name

N/A

表示目标顶点的原始数据列名称。

No

edge_mappings.column_mappings

原始数据中的列将按给定顺序自动映射到属性

定义边数据的哪一列映射到指定属性

edge_mappings.column_mappings.column.index

N/A

原始数据中列的索引

edge_mappings.column_mappings.column.name

N/A

原始数据中的列名

No

edge_mappings.column_mappings.property

N/A

列映射到的schema中的属性。

否(但如果列存在则必须存在)

加载配置

loading_config 部分定义了数据加载的主要设置。这包括指定数据源、其格式和其他元数据。

  • loading_config.data_source.location: 这是容器内数据源的路径。在初始化服务时,必须确保该路径已从主机映射。

  • loading_config.format.metadata: 主要用于配置读取CSV的选项

    • loading_config.format.metadata.delimiter: 定义用于分割数据行的分隔符。

    • loading_config.format.metadata.header_row: 指示是否应将第一行用作表头。

    • loading_config.format.metadata.quoting: 是否使用引号

    • loading_config.format.metadata.quote_char: 引用字符(如果 quoting 为 true)

    • loading_config.format.metadata.double_quote: 值内的引号是否为双引号

    • loading_config.format.metadata.escaping: 是否使用转义

    • loading_config.format.metadata.escape_char: 转义字符(如果 escaping 为 true)

    • loading_config.format.metadata.block_size: 从文件读取时的批次大小

    • loading_config.format.metadata.null_values: 识别空值的拼写形式

顶点映射

vertex_mappings 部分概述了原始数据如何根据定义的模式映射到图顶点。

  • vertex_mappings.type_name: 指定顶点类型的名称。

  • vertex_mappings.inputs: 列出包含该顶点类型原始数据的文件。这些路径是相对于loading_config.data_source.location的。

  • vertex_mappings.column_mappings.column**: 定义顶点数据的哪一列映射到给定属性。如果未指定,原始数据中的列将按给定顺序自动映射到属性。

    • vertex_mappings.column_mappings.column.index: 原始数据中列的索引。

    • vertex_mappings.column_mappings.column.name: 原始数据中的列名。

  • vertex_mappings.column_mappings.property: 列映射到的schema中的属性。

边映射关系

edge_mappings 部分详细说明了原始数据如何根据模式映射到图的边。

  • edge_mappings.type_triplet: 唯一标识一条边的三元组。

    • edge_mappings.type_triplet.edge: 表示边的类型。

    • edge_mappings.type_triplet.source_vertex: 表示源顶点的类型。

    • edge_mappings.type_triplet.destination_vertex: 表示目标顶点的类型。

  • edge_mappings.inputs: 列出包含该边类型原始数据的文件。这些路径是相对于loading_config.data_source.location的。

  • edge_mappings.source_vertex_mappings: 定义边映射中哪些列(可以是多列)对应其源顶点。如果未指定,默认将使用边数据的第一列。

    • edge_mappings.source_vertex_mappings.column.index: 原始数据中表示源顶点列的索引。

    • edge_mappings.source_vertex_mappings.column.name: 原始数据中表示源顶点的列名。

  • edge_mappings.destination_vertex_mappings: 定义边映射到目标顶点的列(可以是多列)。如果未指定,默认将使用边数据的第二列。

    • edge_mappings.destination_vertex_mappings.column.index: 原始数据中表示目标顶点的列索引。

    • edge_mappings.destination_vertex_mappings.column.name: 原始数据中表示目标顶点的列名。

  • edge_mappings.column_mappings.column: 定义边数据的哪一列映射到给定属性。如果未指定,原始数据中的列将按给定顺序自动映射到属性。

    • edge_mappings.column_mappings.column.index: 原始数据中列的索引。

    • edge_mappings.column_mappings.column.name: 原始数据中的列名。

  • edge_mappings.column_mappings.property: 列映射到的模式中的属性。

通过理解并适当设置这些配置,用户可以将他们的图数据无缝导入GraphScope,确保数据完整性和最佳性能。