use of org.embulk.spi.time.TimestampFormatter in project embulk by embulk.
the class Timestamps method newTimestampColumnFormatters.
public static TimestampFormatter[] newTimestampColumnFormatters(TimestampFormatter.Task formatterTask, Schema schema, Map<String, ? extends TimestampFormatter.TimestampColumnOption> columnOptions) {
TimestampFormatter[] formatters = new TimestampFormatter[schema.getColumnCount()];
int i = 0;
for (Column column : schema.getColumns()) {
if (column.getType() instanceof TimestampType) {
Optional<TimestampFormatter.TimestampColumnOption> option = Optional.fromNullable(columnOptions.get(column.getName()));
formatters[i] = TimestampFormatter.of(formatterTask, option);
}
i++;
}
return formatters;
}
use of org.embulk.spi.time.TimestampFormatter in project embulk by embulk.
the class CsvFormatterPlugin method open.
@Override
public PageOutput open(TaskSource taskSource, final Schema schema, FileOutput output) {
final PluginTask task = taskSource.loadTask(PluginTask.class);
final LineEncoder encoder = new LineEncoder(output, task);
final TimestampFormatter[] timestampFormatters = Timestamps.newTimestampColumnFormatters(task, schema, task.getColumnOptions());
final char delimiter = task.getDelimiterChar();
final QuotePolicy quotePolicy = task.getQuotePolicy();
final char quote = task.getQuoteChar() != '\0' ? task.getQuoteChar() : '"';
final char escape = task.getEscapeChar().or(quotePolicy == QuotePolicy.NONE ? '\\' : quote);
final String newlineInField = task.getNewlineInField().getString();
final String nullString = task.getNullString();
// create a file
encoder.nextFile();
// write header
if (task.getHeaderLine()) {
writeHeader(schema, encoder, delimiter, quotePolicy, quote, escape, newlineInField, nullString);
}
return new PageOutput() {
private final PageReader pageReader = new PageReader(schema);
private final String delimiterString = String.valueOf(delimiter);
public void add(Page page) {
pageReader.setPage(page);
while (pageReader.nextRecord()) {
schema.visitColumns(new ColumnVisitor() {
public void booleanColumn(Column column) {
addDelimiter(column);
if (!pageReader.isNull(column)) {
addValue(Boolean.toString(pageReader.getBoolean(column)));
} else {
addNullString();
}
}
public void longColumn(Column column) {
addDelimiter(column);
if (!pageReader.isNull(column)) {
addValue(Long.toString(pageReader.getLong(column)));
} else {
addNullString();
}
}
public void doubleColumn(Column column) {
addDelimiter(column);
if (!pageReader.isNull(column)) {
addValue(Double.toString(pageReader.getDouble(column)));
} else {
addNullString();
}
}
public void stringColumn(Column column) {
addDelimiter(column);
if (!pageReader.isNull(column)) {
addValue(pageReader.getString(column));
} else {
addNullString();
}
}
public void timestampColumn(Column column) {
addDelimiter(column);
if (!pageReader.isNull(column)) {
Timestamp value = pageReader.getTimestamp(column);
addValue(timestampFormatters[column.getIndex()].format(value));
} else {
addNullString();
}
}
public void jsonColumn(Column column) {
addDelimiter(column);
if (!pageReader.isNull(column)) {
Value value = pageReader.getJson(column);
addValue(value.toJson());
} else {
addNullString();
}
}
private void addDelimiter(Column column) {
if (column.getIndex() != 0) {
encoder.addText(delimiterString);
}
}
private void addValue(String v) {
encoder.addText(setEscapeAndQuoteValue(v, delimiter, quotePolicy, quote, escape, newlineInField, nullString));
}
private void addNullString() {
encoder.addText(nullString);
}
});
encoder.addNewLine();
}
}
public void finish() {
encoder.finish();
}
public void close() {
encoder.close();
}
};
}
use of org.embulk.spi.time.TimestampFormatter in project embulk by embulk.
the class DynamicColumnSetterFactory method newColumnSetter.
public DynamicColumnSetter newColumnSetter(PageBuilder pageBuilder, Column column) {
Type type = column.getType();
if (type instanceof BooleanType) {
return new BooleanColumnSetter(pageBuilder, column, defaultValue);
} else if (type instanceof LongType) {
return new LongColumnSetter(pageBuilder, column, defaultValue);
} else if (type instanceof DoubleType) {
return new DoubleColumnSetter(pageBuilder, column, defaultValue);
} else if (type instanceof StringType) {
TimestampFormatter formatter = TimestampFormatter.of(getTimestampFormatForFormatter(column), getTimeZoneId(column));
return new StringColumnSetter(pageBuilder, column, defaultValue, formatter);
} else if (type instanceof TimestampType) {
// TODO use flexible time format like Ruby's Time.parse
final TimestampParser parser;
if (this.useColumnForTimestampMetadata) {
final TimestampType timestampType = (TimestampType) type;
// https://github.com/embulk/embulk/issues/935
parser = TimestampParser.of(getFormatFromTimestampTypeWithDepracationSuppressed(timestampType), getTimeZoneId(column));
} else {
parser = TimestampParser.of(getTimestampFormatForParser(column), getTimeZoneId(column));
}
return new TimestampColumnSetter(pageBuilder, column, defaultValue, parser);
} else if (type instanceof JsonType) {
TimestampFormatter formatter = TimestampFormatter.of(getTimestampFormatForFormatter(column), getTimeZoneId(column));
return new JsonColumnSetter(pageBuilder, column, defaultValue, formatter);
}
throw new ConfigException("Unknown column type: " + type);
}
Aggregations