What is YML or YAML ?
- Data serialization language.
- It is used to store information about different things, commonly used for configuration files.
- Can use to define key-value pairs.
- Very similar to JSON. It does all things we can do with JSON, besides giving more emphasis on readability and user friendliness.
- Can write YAML files with two different extensions. .YML or .YAML.
Basic Structure of YML.
- The basic structure of a YAML file is a map. You might call this a dictionary, hash or object.
- Very generally, it’s keys and values all the way down: key: value
Compare XML and JSON with YAML
- XML is “eXtensible Markup Language” whereas YML is not a markup language.
- XML uses a tag to define the structure just like HTML.
- YAML uses indentation to define the structured data. So the each block in the YAML is differentiated by the number of white spaces.
JSON and YAML considered light weight cousins, lets discuss about XML.
XML has a schema support, that will be useful when inter-operate with 3rd party systems.
The XML schema makes implementation of communication between different systems easier and can be standardised.
But in situations where there is less complexity involved, and more human friendly configuration files are desired JSON and YAML are the preferred choice.
In other words the select of these data serialization languages is based on the purpose of the application.
Images below shows how an employee record can be represented in XML, Json and YAML.
Sample YAML Files.
# This is an example for the docker-compose.yml file.
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
# Here’s the docker-compse.yml for a Vagrant environment with two peer nodes - vp0 and vp1:
vp0:
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_VM_ENDPOINT=http://172.17.0.1:2375
- CORE_LOGGING_LEVEL=DEBUG
command: peer node start
vp1:
extends:
service: vp0
environment:
- CORE_PEER_ID=vp1
- CORE_PEER_DISCOVERY_ROOTNODE=vp0:30303
links:
- vp0
Basic Rules.
- # this is how we add a comment. Comments must be separated from other tokens by whitespaces.
- Use of tabs not supported. Indentation of whitespace is used to denote structure.
- YAML is case sensitive.
Data Types Supported
- Scalars − strings or numbers.
- Sequences − arrays or lists.
- Mappings − hashes or dictionaries.
language: go # example of a scalar.Mappings, It is a way of defining keys along with the values.
go: # sequence.
- 1.7
sudo: required
services:
- docker
env: # sequence with each item in the list placed on its own line. They are equivalent to lists or arrays.
- TEST_TARGET=unit-test
- TEST_TARGET=behave
- TEST_TARGET=node-sdk-unit-tests
Snippet shows a Nested map, that uses indentation. Two space indent is preferred (but not required).
BatchSize:Multiple-line strings, can be written either as a 'literal block' (using |),
MaxMessageCount: 10
AbsoluteMaxBytes: 99 MB
PreferredMaxBytes: 512 KB
or a 'folded block' (using '>').
If we use | ('literal_block') key, the line breaks will be preserved.
If we use > ('folded_block') key, all newlines will be replaced with a single space.
Snippet below shows a literal block.
before_install:
- echo "Starting Docker Daemon "
- |
export TR_PULL_REQUEST="$TRAVIS_PULL_REQUEST" && export"
USER_NAME="$(echo $GIT_USER | cut -d '/' -f 1)" && REPO_NAME"
ip="$(ifconfig docker0 | grep "inet addr:" | awk '{print $2}' | cut -d ':' -f 2)"
port="$(ps -ef | grep docker | awk '{print $10}' | cut -d ':' -f 3 | cut -d '' -f 1)"
sudo stop docker
sudo docker daemon -H tcp://0.0.0.0:$port -H unix:///var/run/docker.sock >>
YAML also has a feature called 'anchors', which let you easily duplicate
content across your document.
Notice the OrdererDefaults, in the code snippet below.
Both of these keys will have the same value:
Profiles:
TwoOrgsOrdererGenesis:
Capabilities:
<<: *ChannelCapabilities
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- blockchain-orderer:31010
BatchTimeout: 2s
No comments:
Post a Comment