public class Csv
extends java.lang.Object
Basically, per IETF RFC-4180:
// Create a stream of "car" JsonObjects from a "cars.csv" file.
String fieldNames = new String[]{"year", "make", "model"});
TStream<String> pathnames = topology.strings("cars.csv");
TStream<JsonObject> cars = FileStreams.textFileReader(topology, pathnames)
.map(csv -> toJson(parseCsv(csv), fieldNames);
cars.print();
Modifier and Type | Method and Description |
---|---|
static java.util.List<java.lang.String> |
parseCsv(java.lang.String csv)
Parse a CSV string into its fields using comma for the field separator.
|
static java.util.List<java.lang.String> |
parseCsv(java.lang.String csv,
char separator)
Parse a CSV string into its fields using the specified field separator.
|
static com.google.gson.JsonObject |
toJson(java.util.List<java.lang.String> fields,
java.lang.String... fieldNames)
Create a
JsonObject containing the specified fieldNames
properties each with its corresponding value from fields . |
public static java.util.List<java.lang.String> parseCsv(java.lang.String csv)
csv
- the csv stringjava.lang.IllegalArgumentException
- if the csv is malformedparseCsv(String, char)
public static java.util.List<java.lang.String> parseCsv(java.lang.String csv, char separator)
csv
- the csv stringseparator
- the separator to usejava.lang.IllegalArgumentException
- if the csv is malformedparseCsv(String)
public static com.google.gson.JsonObject toJson(java.util.List<java.lang.String> fields, java.lang.String... fieldNames)
JsonObject
containing the specified fieldNames
properties each with its corresponding value from fields
.
Each property is set as a string value.
The JsonObject.getAsJsonPrimitive().getAs*()
methods allowing
accessing the property as the requested type.
A field is omitted from the JsonObject if its corresponding field name is null or the empty string.
fields
- the field valuesfieldNames
- the corresponding field value namesjava.lang.IllegalArgumentException
- if the number of fields and the number
of fieldNames don't matchCopyright © 2016 The Apache Software Foundation. All Rights Reserved - bbe71fa-20161201-1641