readdata
Prototype: readdata(filename, filetype)
Return type: data
Description: Parses CSV, JSON, or YAML data from file filename
and returns the result as a data variable.
When filetype is auto, the file type is guessed from the extension
(ignoring case): .csv means CSV; .json means JSON; .yaml means
YAML. If the file doesn't match any of those names, JSON is used.
When filetype is CSV, this function behaves exactly like
readcsv() and returns the same data structure.
When filetype is JSON, this function behaves exactly like
readjson() and returns the same data structure, except there is no
data size limit (maxbytes is inf).
When filetype is YAML, this function behaves exactly like
readyaml() and returns the same data structure, except there is no
data size limit (maxbytes is inf).
Arguments:
filename:string, in the range:"?(/.*)filetype: one ofCSVYAMLJSONauto
Example:
Prepare:
echo -n 1,2,3 > /tmp/file.csv
echo -n '{ "x": 200 }' > /tmp/file.json
echo '- a' > /tmp/file.yaml
echo '- b' >> /tmp/file.yaml
Run:
bundle agent main
{
vars:
"csv" data => readdata("/tmp/file.csv", "auto"); # or file type "CSV"
"json" data => readdata("/tmp/file.json", "auto"); # or file type "JSON"
"csv_str" string => format("%S", csv);
"json_str" string => format("%S", json);
feature_yaml:: # we can only test YAML data if libyaml is compiled in
"yaml" data => readdata("/tmp/file.yaml", "auto"); # or file type "YAML"
"yaml_str" string => format("%S", yaml);
reports:
"From /tmp/file.csv, got data $(csv_str)";
"From /tmp/file.json, got data $(json_str)";
feature_yaml::
"From /tmp/file.yaml, we would get data $(yaml_str)";
!feature_yaml:: # show the output anyway
'From /tmp/file.yaml, we would get data ["a","b"]';
}
Output:
R: From /tmp/file.csv, got data [["1","2","3"]]
R: From /tmp/file.json, got data {"x":200}
R: From /tmp/file.yaml, we would get data ["a","b"]
See also: readcsv(), readyaml(), readjson(), and data documentation.
History: Was introduced in 3.7.0.
