Q1. What is YAML?
A1. YAML Ain’t Markup Language. It is a human friendly data serialization standard for all programming languages. A YAML file has an extension of “.yml“.
Q2. What are the 3 rules of YAML?
A2.
Rule 1: Indentation YAML uses a fixed indentation scheme to represent relationships between data layers (i.e. 2 spaces). Never use a tab.
---
YAML: YAML Ain't Markup Language
What It Is: YAML is a human friendly data serialization
standard for all programming languages.
Projects:
- PyYaml # YAML 1.1, pure python and libyaml binding
- PySyck # YAML 1.0, syck binding
Java:
- JvYaml # Java port of RbYaml
- SnakeYAML # Java 5 / YAML 1.1
- YamlBeans # To/from JavaBeans
- JYaml # Original Java Implementation
Rule 2: Colons & case sensitive All keys/properties are case-sensitive. (“Hello”, is not the same as “hello”) and key value pairs are separated by “:” followed by a space.
YAML: YAML is case sensitive
Keys can be nested.
first_level_dict_key:
second_level_dict_key: value_in_second_level_dict
Rule 3: Dashes To represent lists of items, a single dash followed by a space is used. Multiple items are a part of the same list as a function of their having the same level of indentation.
Java:
- JvYaml # Java port of RbYaml
- SnakeYAML # Java 5 / YAML 1.1
- YamlBeans # To/from JavaBeans
- JYaml # Original Java Implementation
Q3. Where will you use YAML in Java application?
A3.
1) Configuring applications: Java developers mostly deal with property (.properties), XML, and JSON files for configuring applications. YAML file can be used to configure Java applications without being too verbose as XML and being more expressive than .properties and JSON formats.
All you need is the YAML library
<dependency>
<groupid>org.yaml</groupid>
<artifactid>snakeyaml</artifactid>
<version>1.11</version>
</dependency>
Java application.yml file example
#Configure a DataSource
spring:
datasource:
driver-class-name: com.sybase.jdbc4.jdbc.SybDataSource
url: jdbc:sybase:Tds:mydb-host:5700/mydb
username: user
password: password
The properties can be accessed via Spring configs as
@Configuration
@EnableTransactionManagement
public class AppConfig {
@Autowired
private Environment environment;
@Bean
public DriverManagerDataSource datasource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(environment.getRequiredProperty("spring.datasource.driver-class-name"));
ds.setUrl(environment.getRequiredProperty("spring.datasource.url"));
ds.setUsername(environment.getRequiredProperty("spring.datasource.username"));
ds.setPassword(environment.getRequiredProperty("spring.datasource.password"));
return ds;
}
}
Can you spot the expressive nature of the YAML file?
2) Serialize & Deserialize Java objects: YamlBeans makes it easy to serialize and deserialize Java object graphs. YAML is a human-friendly data format. Replace XML and properties files with YAML for more expressive power.
3) Spring boot supports YAML E.g.application.yml file.
More application configuration examples
Logging
logging:
path: ./logs/
file: myapp
level.com.mytutorial: DEBUG
level.: INFO
Messaging
env:
mq:
hostname: my-mq-host
channel: mychannel
port: 3500
queue.manager: JKGHJK
environment: EN7