Modify the metamodel's schema to change/rename column names

210 views Asked by At

I am using Apache MetaModel to get the schema information. There is one use case, where I need to create CsvDataContext object for csv file with no header. I have column names in a separate data structure (List<String> colNames).

The context object gives column names as "A", "B", "C", etc. I guess metamodel assigns some default column names to the tables with no headers.

Is there any way to modify the schema which is held by the CsvDataContext object?

I believe UpdateableDataContext should work, but the documentation doesn't expose any method that allows modifying the metadata like column name.

How is it possible to achieve this scenario?

1

There are 1 answers

0
Kasper Sørensen On BEST ANSWER

When you create your CsvDataContext, you specify a CsvConfiguration. One of the options in the CsvConfiguration is to provide a ColumnNamingStrategy. The default strategy is in deed to use alphabetic characters, A, B, C etc. But you can use a custom naming strategy, like this:

ColumnNamingStrategy columnNamingStrategy =
    ColumnNamingStrategies.customNames("id", "foo", "bar", "baz");
CsvConfiguration configuration = new CsvConfiguration(
    0, columnNamingStrategy, "UTF-8", ',', '"', '\\', true, false);
return new CsvDataContext(file, configuration);