You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// TODO: incorporate fields later for projectable table scans
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to you under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */packageorg.apache.wayang.api.sql.sources.fs;
importorg.apache.calcite.rel.type.RelDataType;
importorg.apache.commons.io.IOUtils;
importorg.apache.wayang.basic.channels.FileChannel;
importorg.apache.wayang.basic.data.Record;
importorg.apache.wayang.core.api.exception.WayangException;
importorg.apache.wayang.core.optimizer.OptimizationContext;
importorg.apache.wayang.core.plan.wayangplan.ExecutionOperator;
importorg.apache.wayang.core.plan.wayangplan.UnarySource;
importorg.apache.wayang.core.platform.ChannelDescriptor;
importorg.apache.wayang.core.platform.ChannelInstance;
importorg.apache.wayang.core.platform.lineage.ExecutionLineageNode;
importorg.apache.wayang.core.types.DataSetType;
importorg.apache.wayang.core.util.Tuple;
importorg.apache.wayang.core.util.fs.FileSystem;
importorg.apache.wayang.core.util.fs.FileSystems;
importorg.apache.wayang.core.util.fs.FileUtils;
importorg.apache.wayang.java.channels.StreamChannel;
importorg.apache.wayang.java.execution.JavaExecutor;
importorg.apache.wayang.java.operators.JavaExecutionOperator;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.UncheckedIOException;
importjava.util.*;
importjava.util.function.Function;
importjava.util.stream.Stream;
importjava.util.stream.StreamSupport;
publicclassJavaCSVTableSource<T> extendsUnarySource<T> implementsJavaExecutionOperator {
privatefinalStringsourcePath;
privatefinalList<RelDataType> fieldTypes;
// TODO: incorporate fields later for projectable table scans// private final ImmutableIntList fields;publicJavaCSVTableSource(StringsourcePath, DataSetTypetype, List<RelDataType> fieldTypes) {
super(type);
this.sourcePath = sourcePath;
this.fieldTypes = fieldTypes;
}
/*public JavaCSVTableSource(DataSetType<T> type) { this(null, type); }*/@OverridepublicTuple<Collection<ExecutionLineageNode>, Collection<ChannelInstance>> evaluate(
ChannelInstance[] inputs,
ChannelInstance[] outputs,
JavaExecutorjavaExecutor,
OptimizationContext.OperatorContextoperatorContext) {
assertoutputs.length == this.getNumOutputs();
finalStringpath;
if (this.sourcePath == null) {
finalFileChannel.Instanceinput = (FileChannel.Instance) inputs[0];
path = input.getSinglePath();
} else {
assertinputs.length == 0;
path = this.sourcePath;
}
finalStringactualInputPath = FileSystems.findActualSingleInputPath(path);
Stream<T> stream = this.createStream(actualInputPath);
((StreamChannel.Instance) outputs[0]).accept(stream);
returnExecutionOperator.modelLazyExecution(inputs, outputs, operatorContext);
}
privateStream<T> createStream(StringactualInputPath) {
Function<String, T> parser = this::parseLine;
returnstreamLines(actualInputPath).map(parser);
}
privateTparseLine(Strings) {
ClasstypeClass = this.getType().getDataUnitType().getTypeClass();
asserttypeClass == Record.class;
try {
String[] tokens = CsvRowConverter.parseLine(s);
if (tokens.length != fieldTypes.size()) {
thrownewIllegalStateException(String.format("Error while parsing CSV file %s at line %s", sourcePath, s));
}
finalObject[] objects = newObject[tokens.length];
for (inti = 0; i < tokens.length; i++) {
objects[i] = CsvRowConverter.convert(fieldTypes.get(i), tokens[i]);
}
return (T) newRecord(objects);
} catch (IOExceptione) {
System.out.println(e.getMessage());
}
thrownewIllegalStateException(String.format("Error while parsing CSV file %s at line %s", sourcePath, s));
}
@OverridepublicList<ChannelDescriptor> getSupportedInputChannels(intindex) {
returnCollections.singletonList(FileChannel.HDFS_TSV_DESCRIPTOR);
}
@OverridepublicList<ChannelDescriptor> getSupportedOutputChannels(intindex) {
assertindex <= this.getNumOutputs() || (index == 0 && this.getNumOutputs() == 0);
returnCollections.singletonList(StreamChannel.DESCRIPTOR);
}
/** * Copied from {@link FileUtils} as a quick work around to read CSV file after skipping * header row. * * Creates a {@link Stream} of a lines of the file. * * @param path of the file * @return the {@link Stream} */privatestaticStream<String> streamLines(Stringpath) {
finalFileSystemfileSystem = FileSystems.getFileSystem(path).orElseThrow(
() -> newIllegalStateException(String.format("No file system found for %s", path))
);
try {
Iterator<String> lineIterator = createLineIterator(fileSystem, path);
lineIterator.next(); // skip header rowreturnStreamSupport.stream(Spliterators.spliteratorUnknownSize(lineIterator, 0), false);
} catch (IOExceptione) {
thrownewWayangException(String.format("%s failed to read %s.", FileUtils.class, path), e);
}
}
/** * Creates an {@link Iterator} over the lines of a given {@code path} (that resides in the given {@code fileSystem}). */privatestaticIterator<String> createLineIterator(FileSystemfileSystem, Stringpath) throwsIOException {
finalBufferedReaderreader = newBufferedReader(newInputStreamReader(fileSystem.open(path), "UTF-8"));
returnnewIterator<String>() {
Stringnext;
{
this.advance();
}
privatevoidadvance() {
try {
this.next = reader.readLine();
} catch (IOExceptione) {
this.next = null;
thrownewUncheckedIOException(e);
} finally {
if (this.next == null) {
IOUtils.closeQuietly(reader);
}
}
}
@OverridepublicbooleanhasNext() {
returnthis.next != null;
}
@OverridepublicStringnext() {
assertthis.hasNext();
finalStringreturnValue = this.next;
this.advance();
returnreturnValue;
}
};
}
}
2b1aef4e9a7503c7e7f9987af1737a27fe565f78
The text was updated successfully, but these errors were encountered:
incorporate fields later for projectable table scans
private final ImmutableIntList fields;
incubator-wayang/wayang-api/wayang-api-sql/src/main/java/org/apache/wayang/api/sql/sources/fs/JavaCSVTableSource.java
Line 56 in d46f3c3
2b1aef4e9a7503c7e7f9987af1737a27fe565f78
The text was updated successfully, but these errors were encountered: