|
1 <stránka |
|
2 xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana" |
|
3 xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"> |
|
4 |
|
5 <nadpis>Reading and writing YAML</nadpis> |
|
6 <perex>convert relations to YAML or YAML/JSON to relations</perex> |
|
7 <m:pořadí-příkladu>05200</m:pořadí-příkladu> |
|
8 |
|
9 <text xmlns="http://www.w3.org/1999/xhtml"> |
|
10 |
|
11 <p> |
|
12 YAML is a text format for serializing tree structures into text (like XML). |
|
13 It can be sometimes tricky, but basic scenarios are intuitive and straightforward. |
|
14 We can use YAML to describe relational data and load them using the <code>relpipe-in-yaml</code> input filter: |
|
15 </p> |
|
16 |
|
17 <m:pre src="examples/film-1-no-types.yaml" jazyk="yaml"/> |
|
18 <p>This YAML notation is almost as simple as <m:a href="examples-recfile">Recfiles</m:a> and like Recfiles it can be easily written by hand.</p> |
|
19 |
|
20 <p> |
|
21 We can also use the JSON notation (it can be read by the same tool – <code>relpipe-in-json</code> is currently just a symlink to <code>relpipe-in-yaml</code>). |
|
22 JSON is inferior to YAML and XML, but the JSON input filter might be useful when interacting with software that can produce only JSON. |
|
23 The <code>film</code> relation would look like this in JSON format (shortened): |
|
24 </p> |
|
25 <m:pre jazyk="json"><![CDATA[{ |
|
26 "film": [ |
|
27 { |
|
28 "title": "Beetlejuice", |
|
29 "year": "1988", |
|
30 "director": "Tim Burton" |
|
31 }, |
|
32 { |
|
33 "title": "…", |
|
34 "year": "…", |
|
35 "director": "…" |
|
36 }, |
|
37 |
|
38 ] |
|
39 }]]></m:pre> |
|
40 |
|
41 <p> |
|
42 When we want to declare data types |
|
43 – which is usually a good idea – |
|
44 we add the <code>attribute-metadata</code> header and move the data under the <code>record</code> node: |
|
45 </p> |
|
46 <m:pre src="examples/film-1.yaml" jazyk="yaml"/> |
|
47 <p> |
|
48 This form (with data types and quotes) is also produced by the <code>relpipe-out-yaml</code> tool. |
|
49 </p> |
|
50 |
|
51 |
|
52 <p>When we load such YAML, we get this relational data:</p> |
|
53 <m:pre src="examples/film-1.tabular" jazyk="text"/> |
|
54 <p>Of course, there might be multiple relations, not only one.</p> |
|
55 |
|
56 |
|
57 <p> |
|
58 Once we convert the YAML to relational format, we can run any transformations and conversions as usual. |
|
59 For example we can use SQL to sort the recordsy by year: |
|
60 </p> |
|
61 |
|
62 <m:pre jazyk="shell"><![CDATA[cat film-1.yaml \ |
|
63 | relpipe-in-yaml \ |
|
64 | relpipe-tr-sql --relation 'film' "SELECT * FROM film ORDER BY year" \ |
|
65 | relpipe-out-recfile > relpipe-data/examples/film-1.rec]]></m:pre> |
|
66 |
|
67 <p>and Recfile output filter to get:</p> |
|
68 <m:pre src="examples/film-1.rec" jazyk="text"/> |
|
69 |
|
70 <p>Or we may use XPath filter:</p> |
|
71 <m:pre jazyk="shell"><![CDATA[relpipe-tr-xpath --relation '.*' --where 'year >= 1980 and year < 1990']]></m:pre> |
|
72 <p>to get the 80s films:</p> |
|
73 <m:pre src="examples/film-1.filtered-2.tabular" jazyk="text"/> |
|
74 |
|
75 <p>YAML comes into play when we are looking for a friendly format for user-supplied data.</p> |
|
76 |
|
77 </text> |
|
78 |
|
79 </stránka> |