Generate RDF from TSV file
Tags: tsv, rdf, rml, tutorial
Published: 2020-04-07 13:55:54

---
title: Generate RDF from TSV file
published: true
description: Learn how to generate RDF from a TSV file
tags: tsv, rdf, rml, tutorial
canonical_url: https://rml.io/docs/rml/tutorials/tsv/
---

**Table of Contents**

1. [Before we start the tutorial](#before-we-start)
2. [Example](#example)
3. [What rules are needed](#what-rules)
4. [How to start a document with RML rules](#how-to-start)
5. [What data to use](#what-data)
6. [How to generate subjects](#generate-subjects)
7. [How to generate predicates and objects](#generate-pos)
8. [Complete Turtle document with RML rules](#complete-turtle)
9. [Wrapping up](#wrapping-up)
10. [More information](#more-info)

# 1 Before we start the tutorial <a name="before-we-start"></a>

## 1.1 What you learn

At the end of the tutorial you will be able to generate RDF from a TSV file using RML rules.

## What you need 

We assume that you understand 

- [RDF](https://www.w3.org/TR/rdf11-concepts/)
- [TSV](https://www.iana.org/assignments/media-types/text/tab-separated-values)
- vocabularies and ontologies, such as classes, properties, and datatypes

## How you use the tutorial

There are two ways to complete this tutorial: you read the explanations and either

- Read the examples.
- Try out the examples yourself by writing and executing RML rules on your computer.

For the second option you need a tool that executes RML rules. 
Suggestions are the [RMLMapper](https://github.com/RMLio/rmlmapper-java) and the [RMLStreamer](https://github.com/RMLio/RMLStreamer).

# 2 Example <a name="example"></a>

Consider the following TSV file called "[characters.tsv](https://rml.io/docs/rml/tutorials/tsv/characters.tsv)":
 
 ```tsv
id firstname lastname hair
0 Ash Ketchum black
1 Misty  orange
```

It contains the information about two different characters. The id, first name, last name, and hair color are included. The latter two are optional. We want to annotate every character and generate the corresponding RDF triples.

For example, consider the character described by the first row:

```tsv
0 Ash Ketchum black
```

We want to generate the corresponding RDF triples for this row:

```
@prefix schema: <http://schema.org/> .
@prefix dbo: <http://dbpedia.org/ontology/> .
@prefix character: <http://example.org/character/> .

character:0 a schema:Person;
  schema:givenName "Ash";
  schema:lastName "Ketchum";
  dbo:hairColor "black".
```

In the following sections we explain 

1. what rules you need to generate these triples, and 
2. how you write them using RML.

# 3 What rules are needed <a name="what-rules"></a>

Two sets of rules are needed:

- rules that describe the TSV file
- rules that define how the RDF terms are generated from the TSV file, and how these terms are used to generate triples.

In our example we need rules that define that:

- The IRI representing a character is generated by concatenating `http://example.org/character/` 
with the character's id.
- This IRI is used as subject of the triples.
- A character is annotated with the class [`schema:Person`](http://schema.org/Person).
- The first name is annotated with the property [`schema:givenName`](http://schema.org/givenName).
- The last name is annotated with the property [`schema:lastName`](http://schema.org/lastName).
- The hair color is annotated with the property [`dbo:hairColor`](http://schema.org/hairColor).

# 4 How to start a document with RML rules <a name="how-to-start"></a>

We write the RML rules in a [Turtle](https://www.w3.org/TR/turtle/) document. RML rules are RDF themselves.

We add the following prefixes:

| Prefix | Description |
| ------ | ----------- |
| `rml` | RML ontology |
| `rr` | The R2RML ontology, which is extended by RML |
| `ql` | The Query Language vocabulary, which is used together with RML | 
| `csvw` | The CSVW Vocabulary, which is used to describe the TSV file |
| `rdf` | The RDF Concepts Vocabulary |
| _empty_ | The prefix used for our RML rules |
| `schema` | The schema.org vocabulary |
| `dbo` | The DBpedia ontology |
{.table .table-responsive}

The last two are added because they are used for the classes and properties.

The prefixes are added in Turtle like this:

```
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix csvw: <http://www.w3.org/ns/csvw#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <http://example.org/rules/> .
@prefix schema: <http://schema.org/> .
@prefix dbo: <http://dbpedia.org/ontology/> .
```

# 5 What data to use <a name="what-data"></a>

In our example the data of the characters is stored in a TSV file. We add the following RML rules that define what TSV file is used:

```
:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source [
      a csvw:Table;
      csvw:url "characters.tsv";
      csvw:dialect [
        a csvw:Dialect;
        csvw:delimiter "\t"
      ]
    ];
    rml:referenceFormulation ql:CSV
  ].
```

The different rules work as follows:

- `:TriplesMap a rr:TriplesMap;` 
defines the [Triples Map](https://www.w3.org/TR/r2rml/#dfn-triples-map) that groups all rules for the characters.
- The blank node of `:TriplesMap rml:logicalSource [ ... ]` contains all rules about the TSV file and how to access the data in the file. The class of the blank node is implicitly of the class [`rml:LogicalSource`](https://rml.io/specs/rml/#logical-source).
- The blank node in `[rml:source [ ... ]` contains all rules about the TSV file.
- `[csvw:url "characters.tsv"]` says that we access the TSV file `characters.tsv`.
- `[csvw:dialect [csvw:delimiter "\t"]]` says that the delimiter used in this file is a tab (`\t`).
- `[rml:referenceFormulation ql:CSV]` says that we use the column names to access the data in the TSV file.

Note that we access a TSV file as if it is a CSV file because we consider it a CSV file with a different delimiter.

# 6 How to generate subjects <a name="generate-subjects"></a>

We add the following rules that define how the subject IRI of a character is generated:

```
:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{id}"
].
```

The different rules work as follows:

- `:TriplesMap rr:subjectMap [ ... ]` contains all the rules about the subject of a triple. The class of the blank node is implicitly of the class [`rr:SubjectMap`](https://www.w3.org/TR/r2rml/#dfn-subject-map). 
- `[rr:template "http://example.org/character/{id}"]` says that the IRI of the subject is generated by concatenating `http://example.org/character/` with the value in the column `id`.

# 7 How to generate predicates and objects <a name="generate-pos"></a>

## 7.1 How to annotate with a class

In our example we need to annotate every character with the class `schema:Person`. We add the following RML rules:

```
:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:constant schema:Person
].
```

The different rules work as follows:

- `:TriplesMap rr:predicateObjectMap [ ... ]` contains all the rules about a specific predicate of a triple.
The class of the blank node is implicitly of the class [`rr:PredicateObjectMap`](https://www.w3.org/TR/r2rml/#dfn-predicate-object-map). 
- `[rr:predicate rdf:type]` says that we use the predicate `rdf:type`.
- `[rr:objectMap [ ... ]]` contains all the rules about the object of a triple.
The class of the blank node is implicitly of the class [`rr:ObjectMap`](https://www.w3.org/TR/r2rml/#dfn-object-map).
- `[rr:constant schema:Person]` says that the object of the triple is `schema:Person` for every character.

Putting all rules we have so far together results in

```
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix csvw: <http://www.w3.org/ns/csvw#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <http://example.org/rules/> .
@prefix schema: <http://schema.org/> .
@prefix dbo: <http://dbpedia.org/ontology/> .

:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source [
      a csvw:Table;
      csvw:url "characters.tsv";
      csvw:dialect [ 
        a csvw:Dialect;
        csvw:delimiter "\t"
      ]
    ];
    rml:referenceFormulation ql:CSV
  ].

:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{id}"
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:objectMap [
    rr:constant schema:Person
  ]
].
```

You can download the Turtle file [here](https://rml.io/docs/rml/tutorials/tsv/rules-1.rml.ttl). If we execute these rules, the following triples are generated:

```
@prefix schema: <http://schema.org/> .

<http://example.org/character/0> a schema:Person .
<http://example.org/character/1> a schema:Person .
```

Two triples are generated: one for each character. There is a unique subject IRI for each character and each character is annotated with the class `schema:Person`.

## 7.2 How to annotate with a property
 
In our example we need to annotate the values in the column `firstname` with the property `schema:givenName`. We add the following rules:

```
:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:givenName;
  rr:objectMap [
    rml:reference "firstname"
  ]
].
```

The rules are different from when annotating with a class: `rml:reference` is used instead of `rr:constant` because the object is not the same for every character. More specific, `[rml:reference "firstname"]` says that the data in the column `firstname` is used for the object.


Putting all rules we have so far together results in

```
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix csvw: <http://www.w3.org/ns/csvw#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <http://example.org/rules/> .
@prefix schema: <http://schema.org/> .
@prefix dbo: <http://dbpedia.org/ontology/> .

:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source [
      a csvw:Table;
      csvw:url "characters.tsv";
      csvw:dialect [ 
        a csvw:Dialect;
        csvw:delimiter "\t"
      ]
    ];
    rml:referenceFormulation ql:CSV
  ].

:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{id}"
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:objectMap [
   rr:constant schema:Person
 ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:givenName;
  rr:objectMap [
    rml:reference "firstname"
  ]
].
```

You can download the Turtle file [here](https://rml.io/docs/rml/tutorials/tsv/rules-2.rml.ttl). If we execute these rules, the following triples are generated:

```
@prefix schema: <http://schema.org/> .

<http://example.org/character/0> a schema:Person;
  schema:givenName "Ash" .

<http://example.org/character/1> a schema:Person;
  schema:givenName "Misty" .
```

Two triples are added: one for the first name of each character.

We add the following rules to annotate the last name and the hair color in the same way as the first name:

```
:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:lastName;
  rr:objectMap [
    rml:reference "lastname"
  ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate dbo:hairColor;
  rr:objectMap [
    rml:reference "hair"
  ]
].
```

# 8 Complete Turtle document with RML rules <a name="complete-turtle"></a>

The complete Turtle document with RML rules is

```
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix csvw: <http://www.w3.org/ns/csvw#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <http://example.org/rules/> .
@prefix schema: <http://schema.org/> .
@prefix dbo: <http://dbpedia.org/ontology/> .

:TriplesMap a rr:TriplesMap;
  rml:logicalSource [
    rml:source [
      a csvw:Table;
      csvw:url "characters.tsv";
      csvw:dialect [ 
        a csvw:Dialect;
        csvw:delimiter "\t"
      ]
    ];
    rml:referenceFormulation ql:CSV
  ].

:TriplesMap rr:subjectMap [
  rr:template "http://example.org/character/{id}"
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate rdf:type;
  rr:objectMap [
   rr:constant schema:Person
 ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:givenName;
  rr:objectMap [
    rml:reference "firstname"
  ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate schema:lastName;
  rr:objectMap [
    rml:reference "lastname"
  ]
].

:TriplesMap rr:predicateObjectMap [
  rr:predicate dbo:hairColor;
  rr:objectMap [
    rml:reference "hair"
  ]
].
```

You can download the Turtle file [here](https://rml.io/docs/rml/tutorials/tsv/rules-3.rml.ttl). If we execute these rules, the final triples are generated:

```
@prefix dbo: <http://dbpedia.org/ontology/> .
@prefix schema: <http://schema.org/> .

<http://example.org/character/0> a schema:Person;
  dbo:hairColor "black";
  schema:givenName "Ash";
  schema:lastName "Ketchum" .

<http://example.org/character/1> a schema:Person;
  dbo:hairColor "orange";
  schema:givenName "Misty";
  schema:lastName "" .
```

# 9 Wrapping up <a name="wrapping-up"></a>

Congratulations! You have created your own RML rules that generate RDF from data in a TSV file. Nice work! We hope you now feel like you have a decent grasp on how RML rules work.

# 10 More information <a name="more-info"></a>

You can find more information about RML in its [specification](/specs/rml). There is also a human readable text-based representation available for RML rules called [YARRRML](https://w3id.org/yarrrml). It is a subset of [YAML](https://yaml.org/), a widely used data serialization language designed to be human-friendly.

If you have any questions or remarks, don't hesitate to contact me via <a href="mailto:pieter.heyvaert@ugent.be">email</a> or via <a href="https://twitter.com/HeyPieter">Twitter</a>.