univocity-parsers
repository·master·Indexed 21 days ago
https://github.com/univocity/univocity-parsersA high-performance Java library providing fast and reliable parsers for various file formats. It features a consistent interface for developing new parsers and provides the Record and RecordMetaData interfaces for type-safe value retrieval, custom conversions, and exporting parsed row data into Map formats.
What's inside univocity-parsers
- univocity-parsers is a high-performance Java library providing extremely fast and reliable parsers for various file formats. It offers a consistent interface across different formats and serves as a framework for developing new parsers. For detailed usage instructions and step-by-step guides, users should refer to the official tutorial on the univocity website.
Access the univocity-parsers tutorial
masterFor comprehensive guides and tutorials on how to use the parsers, visit the official tutorial page at: https://www.univocity.com/pages/parsers-tutorialConvert Record data to Maps
masterThe
Recordinterface allows you to export row data into variousMapformats, which is useful for converting parsed rows into JSON or other object-based structures.Map Types
- String Maps: Maps identifiers (headers, enums, or indices) to their
Stringrepresentation.toFieldMap(String... selectedFields)toIndexMap(int... selectedIndexes)toEnumMap(Class<T> enumType, T... selectedColumns)
- Object Maps: Maps identifiers to the actual parsed object type (e.g.,
Integer,Date, etc.).toFieldObjectMap(String... selectedFields)toIndexObjectMap(int... selectedIndexes)toEnumObjectMap(Class<T> enumType, T... selectedColumns)
If no specific fields are provided to the selection methods, the entire record is typically included.
// Convert specific fields to a Map of Strings Map<String, String> fieldMap = record.toFieldMap("name", "email"); // Convert the whole record to a Map of Objects (parsed types) Map<String, Object> objectMap = record.toFieldObjectMap(); // Convert using Enum columns Map<MyEnum, Object> enumMap = record.toEnumObjectMap(MyEnum.class, MyEnum.COL_A, MyEnum.COL_B);- String Maps: Maps identifiers (headers, enums, or indices) to their
Convert fields using RecordMetaData
masterRecordMetaDataprovides methods to createFieldSetobjects for bulk conversion of fields based on providedConversionobjects.convertFields(Class<T> enumType, Conversion... conversions): Converts fields to an Enum type.convertFields(Conversion... conversions): Converts fields to String types.convertIndexes(Conversion... conversions): Converts field indexes.
Access and convert record values using RecordMetaData
masterThe
RecordMetaDatainterface (implemented byRecordMetaDataImpl) allows you to access and convert data from individual records using several different identifiers: column names (String), column indexes (int), orEnumconstants.Key Capabilities
- Value Retrieval: Retrieve raw
Stringvalues or converted objects from a record. - Type Conversion: Automatically convert string data into specific types (e.g.,
Integer,Date,Boolean) usinggetObjectValue. - Default Values: Define and retrieve default values for specific columns if the input data is null or missing.
- Format Specification: For types like
Date, you can specify a format and options during retrieval. - Column Metadata: Query the type or index of a column.
Supported Identifiers
Most methods accept one of the following to identify a column:
int columnIndexString headerNameEnum<?> column
// Example: Retrieving a value with conversion and a default value Integer age = recordMetaData.getObjectValue(rowData, "age", Integer.class, 0); // Example: Retrieving a Date with a specific format Date date = recordMetaData.getObjectValue(rowData, "created_at", Date.class, "yyyy-MM-dd", "HH:mm:ss"); // Example: Using an Enum for column identification String status = recordMetaData.getValue(rowData, StatusEnum.STATUS_COLUMN);- Value Retrieval: Retrieve raw
Access parsed data using the Record interface
masterThe
Recordinterface provides a high-level API to retrieve data from a single parsed row. You can access values using three different identifiers: a header name (String), an enum column (Enum<?>), or a column index (int).Type-Safe Value Retrieval
You can retrieve values as specific types. The library handles the conversion from the raw string data to the requested type.
- Generic Retrieval: Use
getValue(identifier, expectedType)to get a value of a specific class. - Default Values: Use
getValue(identifier, defaultValue)to return a provided default if the value is missing or null. - Custom Conversions: Use
getValue(identifier, expectedType, Conversion...)to apply specificConversionlogic during retrieval.
Specialized Type Getters
For convenience, the interface provides direct methods for common types:
getString(identifier)getInt(identifier),getLong(identifier),getShort(identifier),getByte(identifier)getFloat(identifier),getDouble(identifier)getBoolean(identifier)(supports customtrueStringandfalseStringmapping)getBigDecimal(identifier),getBigInteger(identifier)getDate(identifier),getCalendar(identifier)getChar(identifier),getBoolean(identifier)
Formatted Retrieval
For numeric and date types, you can pass a format string and optional format options to handle specific parsing requirements:
getInteger(identifier, format, formatOptions...)// Example: Accessing values by header name String name = record.getString("name"); int age = record.getInt("age"); BigDecimal salary = record.getBigDecimal("salary"); // Example: Accessing values with a default value String city = record.getValue("city", "Unknown"); // Example: Accessing values by index String firstCol = record.getString(0); // Example: Boolean with custom true/false strings boolean isActive = record.getBoolean("active", "YES", "NO");- Generic Retrieval: Use
Set column types in RecordMetaData
masterYou can explicitly define the expected type for columns in
RecordMetaData. This is used during conversion processes.Supported methods:
setTypeOfColumns(Class<?> type, Enum... columns)setTypeOfColumns(Class<?> type, String... headerNames)setTypeOfColumns(Class<?> type, int... columnIndexes)
// Set all specified columns to be treated as Double recordMetaData.setTypeOfColumns(Double.class, "price", "tax", "discount");Configure default values for columns in RecordMetaData
masterYou can set a default value for specific columns in
RecordMetaData. This value will be returned bygetObjectValueif the data in that column is null or missing.Supported methods:
setDefaultValueOfColumns(T defaultValue, Enum<?>... columns)setDefaultValueOfColumns(T defaultValue, String... headerNames)setDefaultValueOfColumns(T defaultValue, int... columnIndexes)
// Set default value for a column named "score" to 0 recordMetaData.setDefaultValueOfColumns(0, "score"); // Set default value for multiple columns by index recordMetaData.setDefaultValueOfColumns("N/A", 0, 1, 5);Retrieve multiple values as an array
masterIf you need to extract a subset of columns as a raw array of strings, use the
getValuesmethods. This is more efficient than individual calls if you need multiple specific columns.getValues(String... fieldNames): Returns an array of strings for the specified headers.getValues(int... fieldIndexes): Returns an array of strings for the specified indices.getValues(Enum<?> ... fields): Returns an array of strings for the specified enum columns.
// Get values for specific headers String[] selected = record.getValues("firstName", "lastName"); // Get values for specific indices String[] indexed = record.getValues(0, 2, 5);