Java example: JSON parser
The JSON Parser consumes a stream of JSON objects. Each object must be well formed and on a single line in the input. Use line breaks to delimit the objects. The parser uses the field names as keys in a map, which become column names in the table. You can find the code for this example in /opt/vertica/packages/flextable/examples. This directory also contains an example data file.
This example uses the setRowFromMap()
method to write data.
Loading and using the example
Load the library and define the JSON parser, using the third-party library (gson-2.2.4.jar
) as follows. See the comments in JsonParser.java for a download URL:
You can now define a table and then use the JSON parser to load data into it, as follows:
The data file contains a value (hike_safety) that was not loaded because the table definition did not include that column. The data file follows:
Implementation
The following code shows the process()
method from JsonParser.java. The parser attempts to read the input into a Map.
If the read is successful, the JSON Parser calls setRowFromMap()
:
The factory, JsonParserFactory.java, instantiates and returns a parser in the prepare()
method. No additional setup is required.