Search in sources :

Example 1 with CompositeMapFn

use of org.apache.crunch.fn.CompositeMapFn in project crunch by cloudera.

the class TextFileReaderFactory method read.

@Override
public Iterator<T> read(FileSystem fs, Path path) {
    MapFn mapFn = null;
    if (String.class.equals(ptype.getTypeClass())) {
        mapFn = IdentityFn.getInstance();
    } else {
        // Check for a composite MapFn for the PType.
        // Note that this won't work for Avro-- need to solve that.
        MapFn input = ptype.getInputMapFn();
        if (input instanceof CompositeMapFn) {
            mapFn = ((CompositeMapFn) input).getSecond();
        }
    }
    mapFn.setConfigurationForTest(conf);
    mapFn.initialize();
    FSDataInputStream is = null;
    try {
        is = fs.open(path);
    } catch (IOException e) {
        LOG.info("Could not read path: " + path, e);
        return Iterators.emptyIterator();
    }
    final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    final MapFn<String, T> iterMapFn = mapFn;
    return new UnmodifiableIterator<T>() {

        private String nextLine;

        @Override
        public boolean hasNext() {
            try {
                return (nextLine = reader.readLine()) != null;
            } catch (IOException e) {
                LOG.info("Exception reading text file stream", e);
                return false;
            }
        }

        @Override
        public T next() {
            return iterMapFn.map(nextLine);
        }
    };
}
Also used : UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) InputStreamReader(java.io.InputStreamReader) CompositeMapFn(org.apache.crunch.fn.CompositeMapFn) BufferedReader(java.io.BufferedReader) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException) CompositeMapFn(org.apache.crunch.fn.CompositeMapFn) MapFn(org.apache.crunch.MapFn)

Example 2 with CompositeMapFn

use of org.apache.crunch.fn.CompositeMapFn in project crunch by cloudera.

the class Writables method derived.

public static <S, T> PType<T> derived(Class<T> clazz, MapFn<S, T> inputFn, MapFn<T, S> outputFn, PType<S> base) {
    WritableType<S, ?> wt = (WritableType<S, ?>) base;
    MapFn input = new CompositeMapFn(wt.getInputMapFn(), inputFn);
    MapFn output = new CompositeMapFn(outputFn, wt.getOutputMapFn());
    return new WritableType(clazz, wt.getSerializationClass(), input, output, base.getSubTypes().toArray(new PType[0]));
}
Also used : PType(org.apache.crunch.types.PType) CompositeMapFn(org.apache.crunch.fn.CompositeMapFn) CompositeMapFn(org.apache.crunch.fn.CompositeMapFn) MapFn(org.apache.crunch.MapFn)

Aggregations

MapFn (org.apache.crunch.MapFn)2 CompositeMapFn (org.apache.crunch.fn.CompositeMapFn)2 UnmodifiableIterator (com.google.common.collect.UnmodifiableIterator)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PType (org.apache.crunch.types.PType)1 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)1