|
| 1 | +package org.apache.drill.exec.store.fixedwidth; |
| 2 | + |
| 3 | +import org.apache.drill.common.AutoCloseables; |
| 4 | +import org.apache.drill.common.exceptions.CustomErrorContext; |
| 5 | +import org.apache.drill.common.exceptions.UserException; |
| 6 | +import org.apache.drill.exec.ops.OperatorContext; |
| 7 | +import org.apache.drill.exec.physical.impl.scan.v3.ManagedReader; |
| 8 | +import org.apache.drill.exec.physical.impl.scan.v3.SchemaNegotiator; |
| 9 | +import org.apache.drill.exec.physical.impl.scan.v3.file.FileScanLifecycle; |
| 10 | +import org.apache.drill.exec.physical.impl.scan.v3.file.FileSchemaNegotiator; |
| 11 | +import org.apache.drill.exec.physical.impl.scan.v3.lifecycle.ReaderLifecycle; |
| 12 | +import org.apache.drill.exec.physical.impl.scan.v3.lifecycle.SchemaNegotiatorImpl; |
| 13 | +import org.apache.drill.exec.physical.impl.scan.v3.schema.ProjectedColumn; |
| 14 | +import org.apache.drill.exec.physical.resultSet.ResultSetLoader; |
| 15 | +import org.apache.drill.exec.record.metadata.TupleMetadata; |
| 16 | +import org.apache.drill.shaded.guava.com.google.common.base.Charsets; |
| 17 | +import org.apache.hadoop.mapred.FileSplit; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import java.io.BufferedReader; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.InputStreamReader; |
| 25 | + |
| 26 | +public class FixedwidthBatchReaderImpl implements ManagedReader { |
| 27 | + |
| 28 | + private final int maxRecords; |
| 29 | + private final FixedwidthFormatConfig config; |
| 30 | + private InputStream fsStream; |
| 31 | + private ResultSetLoader loader; |
| 32 | + private FileSplit split; |
| 33 | + private CustomErrorContext errorContext; |
| 34 | + private static final Logger logger = LoggerFactory.getLogger(FixedwidthBatchReader.class); |
| 35 | + |
| 36 | + public FixedwidthBatchReaderImpl (SchemaNegotiator negotiator, FixedwidthFormatConfig config, int maxRecords) { |
| 37 | + this.loader = open(negotiator); |
| 38 | + this.config = config; |
| 39 | + this.maxRecords = maxRecords; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean next() { |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void close() { |
| 49 | + if (fsStream != null){ |
| 50 | + AutoCloseables.closeSilently(fsStream); |
| 51 | + fsStream = null; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private ResultSetLoader open(SchemaNegotiator negotiator) { |
| 56 | + split = (FileSplit) negotiator.split(); |
| 57 | + errorContext = negotiator.parentErrorContext(); |
| 58 | + openFile(negotiator); |
| 59 | + |
| 60 | + try { |
| 61 | + negotiator.tableSchema(buildSchema(), true); |
| 62 | + loader = negotiator.build(); |
| 63 | + } catch (Exception e) { |
| 64 | + throw UserException |
| 65 | + .dataReadError(e) |
| 66 | + .message("Failed to open input file: {}", split.getPath().toString()) |
| 67 | + .addContext(errorContext) |
| 68 | + .addContext(e.getMessage()) |
| 69 | + .build(logger); |
| 70 | + } |
| 71 | + reader = new BufferedReader(new InputStreamReader(fsStream, Charsets.UTF_8)); |
| 72 | + return loader; |
| 73 | + } |
| 74 | + |
| 75 | + private void openFile(FileSchemaNegotiator negotiator) { |
| 76 | + try { |
| 77 | + fsStream = negotiator.file().fileSystem().openPossiblyCompressedStream(split.getPath()); |
| 78 | + sasFileReader = new SasFileReaderImpl(fsStream); |
| 79 | + firstRow = sasFileReader.readNext(); |
| 80 | + } catch (IOException e) { |
| 81 | + throw UserException |
| 82 | + .dataReadError(e) |
| 83 | + .message("Unable to open Fixed Width File %s", split.getPath()) |
| 84 | + .addContext(e.getMessage()) |
| 85 | + .addContext(errorContext) |
| 86 | + .build(logger); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments