Search in sources :

Example 1 with IValueParser

use of org.apache.hyracks.dataflow.common.data.parsers.IValueParser in project asterixdb by apache.

the class DelimitedDataTupleParserFactory method createTupleParser.

@Override
public ITupleParser createTupleParser(final IHyracksTaskContext ctx) {
    return new ITupleParser() {

        @Override
        public void parse(InputStream in, IFrameWriter writer) throws HyracksDataException {
            try {
                IValueParser[] valueParsers = new IValueParser[valueParserFactories.length];
                for (int i = 0; i < valueParserFactories.length; ++i) {
                    valueParsers[i] = valueParserFactories[i].createValueParser();
                }
                IFrame frame = new VSizeFrame(ctx);
                FrameTupleAppender appender = new FrameTupleAppender();
                appender.reset(frame, true);
                ArrayTupleBuilder tb = new ArrayTupleBuilder(valueParsers.length);
                DataOutput dos = tb.getDataOutput();
                FieldCursorForDelimitedDataParser cursor = new FieldCursorForDelimitedDataParser(new InputStreamReader(in), fieldDelimiter, quote);
                while (cursor.nextRecord()) {
                    tb.reset();
                    for (int i = 0; i < valueParsers.length; ++i) {
                        if (!cursor.nextField()) {
                            break;
                        }
                        // Eliminate double quotes in the field that we are going to parse
                        if (cursor.isDoubleQuoteIncludedInThisField) {
                            cursor.eliminateDoubleQuote(cursor.buffer, cursor.fStart, cursor.fEnd - cursor.fStart);
                            cursor.fEnd -= cursor.doubleQuoteCount;
                            cursor.isDoubleQuoteIncludedInThisField = false;
                        }
                        valueParsers[i].parse(cursor.buffer, cursor.fStart, cursor.fEnd - cursor.fStart, dos);
                        tb.addFieldEndOffset();
                    }
                    FrameUtils.appendToWriter(writer, appender, tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize());
                }
                appender.write(writer, true);
            } catch (IOException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : IFrameWriter(org.apache.hyracks.api.comm.IFrameWriter) DataOutput(java.io.DataOutput) InputStreamReader(java.io.InputStreamReader) IFrame(org.apache.hyracks.api.comm.IFrame) InputStream(java.io.InputStream) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) IOException(java.io.IOException) VSizeFrame(org.apache.hyracks.api.comm.VSizeFrame) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) FrameTupleAppender(org.apache.hyracks.dataflow.common.comm.io.FrameTupleAppender) IValueParser(org.apache.hyracks.dataflow.common.data.parsers.IValueParser)

Example 2 with IValueParser

use of org.apache.hyracks.dataflow.common.data.parsers.IValueParser in project asterixdb by apache.

the class ADurationParserFactory method createValueParser.

@Override
public IValueParser createValueParser() {
    final AMutableDuration aMutableDuration = new AMutableDuration(0, 0);
    return new IValueParser() {

        @Override
        public void parse(char[] buffer, int start, int length, DataOutput out) throws HyracksDataException {
            parseDuration(buffer, start, length, aMutableDuration, ADurationParseOption.All);
            try {
                out.writeInt(aMutableDuration.getMonths());
                out.writeLong(aMutableDuration.getMilliseconds());
            } catch (IOException ex) {
                throw new HyracksDataException(ex);
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AMutableDuration(org.apache.asterix.om.base.AMutableDuration) IValueParser(org.apache.hyracks.dataflow.common.data.parsers.IValueParser) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 3 with IValueParser

use of org.apache.hyracks.dataflow.common.data.parsers.IValueParser in project asterixdb by apache.

the class ADateTimeParserFactory method createValueParser.

@Override
public IValueParser createValueParser() {
    return new IValueParser() {

        @Override
        public void parse(char[] buffer, int start, int length, DataOutput out) throws HyracksDataException {
            long chrononTimeInMs = 0;
            short timeOffset = (short) ((buffer[start] == '-') ? 1 : 0);
            timeOffset += 8;
            if (buffer[start + timeOffset] != 'T') {
                timeOffset += 2;
                if (buffer[start + timeOffset] != 'T') {
                    throw new HyracksDataException(dateTimeErrorMessage + ": missing T");
                }
            }
            chrononTimeInMs = ADateParserFactory.parseDatePart(buffer, start, timeOffset);
            chrononTimeInMs += ATimeParserFactory.parseTimePart(buffer, start + timeOffset + 1, length - timeOffset - 1);
            try {
                out.writeLong(chrononTimeInMs);
            } catch (IOException ex) {
                throw new HyracksDataException(ex);
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) IValueParser(org.apache.hyracks.dataflow.common.data.parsers.IValueParser) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 4 with IValueParser

use of org.apache.hyracks.dataflow.common.data.parsers.IValueParser in project asterixdb by apache.

the class WordTupleParserFactory method createTupleParser.

@Override
public ITupleParser createTupleParser(final IHyracksTaskContext ctx) {
    return new ITupleParser() {

        @Override
        public void parse(InputStream in, IFrameWriter writer) throws HyracksDataException {
            try {
                FrameTupleAppender appender = new FrameTupleAppender(new VSizeFrame(ctx));
                ArrayTupleBuilder tb = new ArrayTupleBuilder(1);
                DataOutput dos = tb.getDataOutput();
                IValueParser utf8StringParser = UTF8StringParserFactory.INSTANCE.createValueParser();
                WordCursor cursor = new WordCursor(new InputStreamReader(in));
                while (cursor.nextWord()) {
                    tb.reset();
                    utf8StringParser.parse(cursor.buffer, cursor.fStart, cursor.fEnd - cursor.fStart, dos);
                    tb.addFieldEndOffset();
                    FrameUtils.appendToWriter(writer, appender, tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize());
                }
                appender.write(writer, true);
            } catch (IOException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : IFrameWriter(org.apache.hyracks.api.comm.IFrameWriter) DataOutput(java.io.DataOutput) InputStreamReader(java.io.InputStreamReader) ITupleParser(org.apache.hyracks.dataflow.std.file.ITupleParser) InputStream(java.io.InputStream) FrameTupleAppender(org.apache.hyracks.dataflow.common.comm.io.FrameTupleAppender) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) IValueParser(org.apache.hyracks.dataflow.common.data.parsers.IValueParser) IOException(java.io.IOException) VSizeFrame(org.apache.hyracks.api.comm.VSizeFrame) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Aggregations

DataOutput (java.io.DataOutput)4 IOException (java.io.IOException)4 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)4 IValueParser (org.apache.hyracks.dataflow.common.data.parsers.IValueParser)4 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 IFrameWriter (org.apache.hyracks.api.comm.IFrameWriter)2 VSizeFrame (org.apache.hyracks.api.comm.VSizeFrame)2 ArrayTupleBuilder (org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder)2 FrameTupleAppender (org.apache.hyracks.dataflow.common.comm.io.FrameTupleAppender)2 AMutableDuration (org.apache.asterix.om.base.AMutableDuration)1 IFrame (org.apache.hyracks.api.comm.IFrame)1 ITupleParser (org.apache.hyracks.dataflow.std.file.ITupleParser)1