Hyppää sisältöön

Welcome to our weekly research support coffee hour on Zoom! Click here for more information.

Warning!

Puhti scratch disk is becoming very full (80+ % ) resulting in performance degradation. Everybody is advised to only keep actively processed data on scratch, all other data should be deleted, transferred to host institute or stored in Lumi-O. No new quota will be granted. Click here for a tool for examining your disk usage.

Perustaso

Tarvitset vain perustason tietoteknistä osaamista. On hyödyllistä tietää, miten XML tai JSON toimii.

Lyhyt johdanto YAMLiin

YAMLia käytetään avain-arvo-kuvausten ja taulukoiden kuvaamiseen. YAML-tiedostot tunnistetaan tiedostopäätteestä .yml tai .yaml.

YAML-aineisto voi olla

  • arvo
value
  • taulukko
- value 1
- value 2
- value 3

tai

[value 1, value 2, value 3]
  • sanakirja
key: value
another_key: another value

tai

key:
  value
another_key:
  another value
  • YAML-aineisto
key:
  - value 1
  - another key:
      yet another key: value 2
    another key 2:
      - more values
    this keys value is also an array:
    - but indentation is not necessary here

Arvoja voidaan syöttää usealle riville käyttäen merkkiä >:

key: >
  Here's a value that is written over multiple lines
  but is actually still considered a single line until now.

  Placing double newline here will result in newline in the actual data.

Sanatarkka tyyli on tuettu merkillä |:

key: |
  Now each
  newline is
  treated as such

YAML vs JSON

YAML on JSONin (JavaScript Object Notation) yläsarja. Siten

{
  "key": [
    {
      "value 1": {
        "another key": {
          "yet another key": "value 2"
        },
        "another key 2": [
          "more values"
        ],
        "this keys value is also an array": ["but indentation is not necessary here"]
      }
    }
  ]
}

on myös kelvollista YAMLia. Yleisesti ottaen YAML on tiiviimpää kuin JSON:

key:
  - value 1:
      another key:
        yet another key: value 2
      another key 2:
        - more values
      this keys value is also an array:
        - but indentation is not necessary here

yq-komentorivityökalu

yq on hyödyllinen työkalu yamlin käsittelyyn. Sen voi asentaa käyttäen pip:

$ pip show yq  
Name: yq
Version: 3.0.2
Summary: Command-line YAML/XML processor - jq wrapper for YAML/XML documents
Home-page: https://github.com/kislyuk/yq
Author: Andrey Kislyuk
Author-email: kislyuk@gmail.com
License: Apache Software License
Location: /home/jtahir/Documents/csc-stuff/osclient/lib/python3.6/site-packages
Requires: argcomplete, PyYAML, toml, xmltodict
Required-by: 

yq on jq-wrapperi, mikä tarkoittaa, että se muuntaa yaml-syötteen jsoniksi ja välittää sitten käsittelyn jq:lle. Tästä syystä sen syntaksi on sama kuin jq:n. Alla olevassa esimerkissä .data.WebHookSecretKey:n arvo haetaan raakamuodossa (ilman lainausmerkkejä):

$ echo 'apiVersion: v1
kind: Secret
data:
  WebHookSecretKey: dGhpc19pc19hX2JhZF90b2tlbgo=
metadata:
  name: webhooksecret
  namespace: mynamespace     # set this to your project namespace
' | yq ' .data.WebHookSecretKey ' -r
dGhpc19pc19hX2JhZF90b2tlbgo=

Lisätietoja on sivustoilla yaml.org ja json.org.

Suomenkielinen tekoälykäännös

Sisällössä voi esiintyä virheellistä tietoa tekoälykäännöksestä johtuen.

Klikkaa tästä antaaksesi palautetta