
eu.stratosphere.api.java.io.CsvInputFormat Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* Copyright (C) 2010-2013 by the Stratosphere project (http://stratosphere.eu)
*
* Licensed 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.
*
**********************************************************************************************************************/
package eu.stratosphere.api.java.io;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import com.google.common.base.Preconditions;
import eu.stratosphere.api.common.io.GenericCsvInputFormat;
import eu.stratosphere.api.java.tuple.Tuple;
import eu.stratosphere.core.fs.FileInputSplit;
import eu.stratosphere.core.fs.Path;
import eu.stratosphere.types.parser.FieldParser;
import eu.stratosphere.util.StringUtils;
public class CsvInputFormat extends GenericCsvInputFormat {
private static final long serialVersionUID = 1L;
public static final String DEFAULT_LINE_DELIMITER = "\n";
public static final char DEFAULT_FIELD_DELIMITER = ',';
private transient Object[] parsedValues;
public CsvInputFormat(Path filePath) {
super(filePath);
}
public CsvInputFormat(Path filePath, Class> ... types) {
this(filePath, DEFAULT_LINE_DELIMITER, DEFAULT_FIELD_DELIMITER, types);
}
public CsvInputFormat(Path filePath, String lineDelimiter, char fieldDelimiter, Class>... types) {
super(filePath);
setDelimiter(lineDelimiter);
setFieldDelimiter(fieldDelimiter);
setFieldTypes(types);
}
public void setFieldTypes(Class> ... fieldTypes) {
if (fieldTypes == null || fieldTypes.length == 0) {
throw new IllegalArgumentException("Field types must not be null or empty.");
}
setFieldTypesGeneric(fieldTypes);
}
public void setFields(int[] sourceFieldIndices, Class>[] fieldTypes) {
Preconditions.checkNotNull(sourceFieldIndices);
Preconditions.checkNotNull(fieldTypes);
checkForMonotonousOrder(sourceFieldIndices, fieldTypes);
setFieldsGeneric(sourceFieldIndices, fieldTypes);
}
public void setFields(boolean[] sourceFieldMask, Class>[] fieldTypes) {
Preconditions.checkNotNull(sourceFieldMask);
Preconditions.checkNotNull(fieldTypes);
setFieldsGeneric(sourceFieldMask, fieldTypes);
}
public Class>[] getFieldTypes() {
return super.getGenericFieldTypes();
}
@Override
public void open(FileInputSplit split) throws IOException {
super.open(split);
@SuppressWarnings("unchecked")
FieldParser
© 2015 - 2025 Weber Informatics LLC | Privacy Policy