univocity-parsers

repository·master·Indexed 21 days ago

https://github.com/univocity/univocity-parsers

A 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.

Tokens
2.2K
Snippets
6
Records
9
Agent score
74%

What's inside univocity-parsers

  1. Overview of univocity-parsers

    master
    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.
  2. Convert Record data to Maps

    master

    The Record interface allows you to export row data into various Map formats, 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 String representation.
      • 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);
  3. Convert fields using RecordMetaData

    master

    RecordMetaData provides methods to create FieldSet objects for bulk conversion of fields based on provided Conversion objects.

    • 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.
  4. Access and convert record values using RecordMetaData

    master

    The RecordMetaData interface (implemented by RecordMetaDataImpl) allows you to access and convert data from individual records using several different identifiers: column names (String), column indexes (int), or Enum constants.

    Key Capabilities

    • Value Retrieval: Retrieve raw String values or converted objects from a record.
    • Type Conversion: Automatically convert string data into specific types (e.g., Integer, Date, Boolean) using getObjectValue.
    • 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 columnIndex
    • String headerName
    • Enum<?> 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);
  5. Access parsed data using the Record interface

    master

    The Record interface 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 specific Conversion logic 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 custom trueString and falseString mapping)
    • 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");
  6. Set column types in RecordMetaData

    master

    You 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");
  7. Configure default values for columns in RecordMetaData

    master

    You can set a default value for specific columns in RecordMetaData. This value will be returned by getObjectValue if 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);
  8. Retrieve multiple values as an array

    master

    If you need to extract a subset of columns as a raw array of strings, use the getValues methods. 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);