use of co.cask.cdap.api.data.format.RecordFormat in project cdap by caskdata.
the class RecordFormats method createInitializedFormat.
/**
* Create an initialized record format for the given format specification. The name in the specification is
* first checked against standard names like "CSV" or "TSV". If it is a standard name, the corresponding
* format will be created, with specification settings applied on top of default settings.
* For example, "CSV" will map to the {@link DelimitedStringsRecordFormat}, with a comma as the delimiter,
* whereas "TSV" will map to the {@link DelimitedStringsRecordFormat}, with a tab as the delimiter.
* If the name is not a standard name, it is interpreted as a class name.
*
* @param spec the specification for the format to create and initialize
* @param <FROM> Type of underlying object the format reads
* @param <TO> Type of object the format reads the underlying object into
* @return Initialized {@link RecordFormat} based on the given name
* @throws IllegalAccessException if there was an illegal access when instantiating the record format
* @throws InstantiationException if there was an exception instantiating the record format
* @throws ClassNotFoundException if the record format class could not be found
* @throws UnsupportedTypeException if the specification is not supported by the format
*/
public static <FROM, TO> RecordFormat<FROM, TO> createInitializedFormat(FormatSpecification spec) throws IllegalAccessException, InstantiationException, ClassNotFoundException, UnsupportedTypeException {
String name = spec.getName();
// check if it's a standard class
Class<? extends RecordFormat> formatClass = NAME_CLASS_MAP.get(name.toLowerCase());
@SuppressWarnings("unchecked") RecordFormat<FROM, TO> format = (RecordFormat<FROM, TO>) (formatClass == null ? Class.forName(name).newInstance() : formatClass.newInstance());
// compute actual settings: use default settings if present
Map<String, String> settings;
Map<String, String> defaultSettings = NAME_SETTINGS_MAP.get(name.toLowerCase());
if (defaultSettings != null) {
settings = Maps.newHashMap(defaultSettings);
if (spec.getSettings() != null) {
settings.putAll(spec.getSettings());
}
} else {
settings = spec.getSettings();
}
// compute actual schema
Schema schema;
Schema defaultSchema = DEFAULT_SCHEMA_MAP.get(name.toLowerCase());
if (defaultSchema != null && spec.getSchema() == null) {
schema = defaultSchema;
} else {
schema = spec.getSchema();
}
FormatSpecification actualSpec = new FormatSpecification(name, schema, settings);
format.initialize(actualSpec);
return format;
}
Aggregations