Created
April 18, 2016 19:15
-
-
Save gastaldi/a099a6afdd92abf9e0135eb8aa976d0b to your computer and use it in GitHub Desktop.
Split from a huge YAML to several files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.example; | |
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.FileWriter; | |
| import java.io.IOException; | |
| import java.util.Map; | |
| import org.yaml.snakeyaml.DumperOptions; | |
| import org.yaml.snakeyaml.Yaml; | |
| public class YAMLSplitter { | |
| public static void main(String[] args) throws Exception { | |
| DumperOptions dop = new DumperOptions(); | |
| dop.setPrettyFlow(true); | |
| dop.setSplitLines(true); | |
| dop.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); | |
| Yaml yaml = new Yaml(dop); | |
| try (FileInputStream fis= new FileInputStream("path-to-huge.yaml")) { | |
| yaml.loadAll(fis).forEach((item) -> { | |
| if (item == null) return; | |
| String id = ((Map<String,String>)item).get("id"); | |
| //System.out.println(yaml.dump(item)); | |
| try { | |
| File f = new File("target-dir", id); | |
| try (FileWriter fw = new FileWriter(f)) { | |
| yaml.dump(item,fw); | |
| } | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment