

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立 Neptune GraphMappingConfig
<a name="dms-neptune-graph-mapping"></a>

您建立的 `GraphMappingConfig` 指定應如何將擷取自來源資料存放區的資料載入至 Neptune 資料庫叢集。它的格式取決於它是用於載入 RDF 資料還是用於載入屬性圖資料。

若是 RDF 資料，您可以使用 W3 [R2RML](https://www.w3.org/TR/r2rml/) 語言將關聯式資料對應到 RDF。

如果您載入的是即將使用 Gremlin 查詢的屬性圖形資料，請為 `GraphMappingConfig` 建立 JSON 物件。

## RDF/SPARQL 資料的 GraphMappingConfig 配置
<a name="dms-neptune-graph-mapping-sparql"></a>

如果您是載入要使用 SPARQL 查詢的 RDF 資料，則可以使用 [R2RML](https://www.w3.org/TR/r2rml/) 撰寫 `GraphMappingConfig`。`R2RML` 是用於將關聯式資料映射到 RDF 的標準 W3 語言。以下是一個範例：

```
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ex: <http://example.com/ns#> .

<#TriplesMap1>
    rr:logicalTable [ rr:tableName "nodes" ];
    rr:subjectMap [
        rr:template "http://data.example.com/employee/{id}";
        rr:class ex:Employee;
    ];
    rr:predicateObjectMap [
        rr:predicate ex:name;
        rr:objectMap [ rr:column "label" ];
    ] .
```

以下是另一個範例：

```
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ex: <http://example.com/#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<#TriplesMap2>
    rr:logicalTable [ rr:tableName "Student" ];
    rr:subjectMap   [ rr:template "http://example.com/{ID}{Name}";
                      rr:class foaf:Person ];
    rr:predicateObjectMap [
        rr:predicate ex:id ;
        rr:objectMap  [ rr:column "ID";
                        rr:datatype xsd:integer ]
    ];
    rr:predicateObjectMap [
        rr:predicate foaf:name ;
        rr:objectMap  [ rr:column "Name" ]
    ] .
```

[R2RML: RDB to RDF Mapping Language](https://www.w3.org/TR/r2rml/) 的 W3 建議中提供了語言的詳細資訊。

## 屬性圖形/Gremlin 資料的 GraphMappingConfig 配置
<a name="dms-neptune-graph-mapping-gremlin"></a>

適用於屬性圖形資料的比較 `GraphMappingConfig` 是一個 JSON 物件，為即將根據來源資料產生的每個圖形實體提供對應規則。下列範本顯示此物件中每個規則的外觀：

```
{
  "rules": [
    {
      "rule_id": "(an identifier for this rule)",
      "rule_name": "(a name for this rule)",
      "table_name": "(the name of the table or view being loaded)",
      "vertex_definitions": [
        {
          "vertex_id_template": "{col1}",
          "vertex_label": "(the vertex to create)",
          "vertex_definition_id": "(an identifier for this vertex)",
          "vertex_properties": [
            {
              "property_name": "(name of the property)",
              "property_value_template": "{col2} or text",
              "property_value_type": "(data type of the property)"
            }
          ]
        }
      ]
    },
    {
      "rule_id": "(an identifier for this rule)",
      "rule_name": "(a name for this rule)",
      "table_name": "(the name of the table or view being loaded)",
      "edge_definitions": [
        {
          "from_vertex": {
            "vertex_id_template": "{col1}",
            "vertex_definition_id": "(an identifier for the vertex referenced above)"
          },
          "to_vertex": {
            "vertex_id_template": "{col3}",
            "vertex_definition_id": "(an identifier for the vertex referenced above)"
          },
          "edge_id_template": {
            "label": "(the edge label to add)",
            "template": "{col1}_{col3}"
          },
          "edge_properties":[
            {
              "property_name": "(the property to add)",
              "property_value_template": "{col4} or text",
              "property_value_type": "(data type like String, int, double)"
            }
          ]
        }
      ]
    }
  ]
}
```

請注意，出現頂點標籤表示頂點是在此處建立，沒有頂點標籤則表示頂點是由不同來源建立的，而這項定義只是新增頂點屬性。

以下是員工記錄的規則範例：

```
{
  "rules": [
    {
      "rule_id": "1",
      "rule_name": "vertex_mapping_rule_from_nodes",
      "table_name": "nodes",
      "vertex_definitions": [
        {
          "vertex_id_template": "{emp_id}",
          "vertex_label": "employee",
          "vertex_definition_id": "1",
          "vertex_properties": [
            {
              "property_name": "name",
              "property_value_template": "{emp_name}",
              "property_value_type": "String"
            }
          ]
        }
      ]
    },
    {
      "rule_id": "2",
      "rule_name": "edge_mapping_rule_from_emp",
      "table_name": "nodes",
      "edge_definitions": [
        {
          "from_vertex": {
            "vertex_id_template": "{emp_id}",
            "vertex_definition_id": "1"
          },
          "to_vertex": {
            "vertex_id_template": "{mgr_id}",
            "vertex_definition_id": "1"
          },
          "edge_id_template": {
            "label": "reportsTo",
            "template": "{emp_id}_{mgr_id}"
          },
          "edge_properties":[
            {
              "property_name": "team",
              "property_value_template": "{team}",
              "property_value_type": "String"
            }
          ]
        }
      ]
    }
  ]
}
```