Search in sources :

Example 11 with IPrinter

use of org.apache.hyracks.algebricks.data.IPrinter in project asterixdb by apache.

the class ARecordPrinterFactory method createPrinter.

@Override
public IPrinter createPrinter() {
    final PointableAllocator allocator = new PointableAllocator();
    final IAType inputType = recType == null ? DefaultOpenFieldType.getDefaultOpenFieldType(ATypeTag.OBJECT) : recType;
    final IVisitablePointable recAccessor = allocator.allocateRecordValue(inputType);
    final APrintVisitor printVisitor = new APrintVisitor();
    final Pair<PrintStream, ATypeTag> arg = new Pair<>(null, null);
    return new IPrinter() {

        @Override
        public void init() {
            arg.second = inputType.getTypeTag();
        }

        @Override
        public void print(byte[] b, int start, int l, PrintStream ps) throws HyracksDataException {
            recAccessor.set(b, start, l);
            arg.first = ps;
            recAccessor.accept(printVisitor, arg);
        }
    };
}
Also used : PrintStream(java.io.PrintStream) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) APrintVisitor(org.apache.asterix.om.pointables.printer.adm.APrintVisitor) IPrinter(org.apache.hyracks.algebricks.data.IPrinter) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) IAType(org.apache.asterix.om.types.IAType) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 12 with IPrinter

use of org.apache.hyracks.algebricks.data.IPrinter in project asterixdb by apache.

the class AUnionPrinterFactory method createPrinter.

@Override
public IPrinter createPrinter() {
    return new IPrinter() {

        private IPrinter[] printers;

        private List<IAType> unionList;

        @Override
        public void init() throws HyracksDataException {
            unionList = unionType.getUnionList();
            printers = new IPrinter[unionType.getUnionList().size()];
            for (int i = 0; i < printers.length; i++) {
                printers[i] = (CSVPrinterFactoryProvider.INSTANCE.getPrinterFactory(unionType.getUnionList().get(i))).createPrinter();
                printers[i].init();
            }
        }

        @Override
        public void print(byte[] b, int s, int l, PrintStream ps) throws HyracksDataException {
            ATypeTag tag = unionList.get(b[s + 1]).getTypeTag();
            if (tag == ATypeTag.UNION) {
                printers[b[s + 1]].print(b, s + 1, l, ps);
            } else {
                if (tag == ATypeTag.ANY) {
                    printers[b[s + 1]].print(b, s + 2, l, ps);
                } else {
                    printers[b[s + 1]].print(b, s + 1, l, ps);
                }
            }
        }
    };
}
Also used : PrintStream(java.io.PrintStream) ATypeTag(org.apache.asterix.om.types.ATypeTag) List(java.util.List) IPrinter(org.apache.hyracks.algebricks.data.IPrinter)

Example 13 with IPrinter

use of org.apache.hyracks.algebricks.data.IPrinter in project asterixdb by apache.

the class ARecordPrinterFactory method createPrinter.

@Override
public IPrinter createPrinter() {
    final PointableAllocator allocator = new PointableAllocator();
    final IAType inputType = recType == null ? DefaultOpenFieldType.getDefaultOpenFieldType(ATypeTag.OBJECT) : recType;
    final IVisitablePointable recAccessor = allocator.allocateRecordValue(inputType);
    final APrintVisitor printVisitor = new APrintVisitor();
    final Pair<PrintStream, ATypeTag> arg = new Pair<>(null, null);
    return new IPrinter() {

        @Override
        public void init() {
            arg.second = inputType.getTypeTag();
        }

        @Override
        public void print(byte[] b, int start, int l, PrintStream ps) throws HyracksDataException {
            recAccessor.set(b, start, l);
            arg.first = ps;
            recAccessor.accept(printVisitor, arg);
        }
    };
}
Also used : PrintStream(java.io.PrintStream) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) APrintVisitor(org.apache.asterix.om.pointables.printer.json.lossless.APrintVisitor) IPrinter(org.apache.hyracks.algebricks.data.IPrinter) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) IAType(org.apache.asterix.om.types.IAType) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 14 with IPrinter

use of org.apache.hyracks.algebricks.data.IPrinter in project asterixdb by apache.

the class AUnorderedlistPrinterFactory method createPrinter.

@Override
public IPrinter createPrinter() {
    PointableAllocator allocator = new PointableAllocator();
    final IAType inputType = unorderedlistType == null ? DefaultOpenFieldType.getDefaultOpenFieldType(ATypeTag.MULTISET) : unorderedlistType;
    final IVisitablePointable listAccessor = allocator.allocateListValue(inputType);
    final APrintVisitor printVisitor = new APrintVisitor();
    final Pair<PrintStream, ATypeTag> arg = new Pair<>(null, null);
    return new IPrinter() {

        @Override
        public void init() {
            arg.second = inputType.getTypeTag();
        }

        @Override
        public void print(byte[] b, int start, int l, PrintStream ps) throws HyracksDataException {
            try {
                listAccessor.set(b, start, l);
                arg.first = ps;
                listAccessor.accept(printVisitor, arg);
            } catch (Exception e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : PrintStream(java.io.PrintStream) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) APrintVisitor(org.apache.asterix.om.pointables.printer.json.lossless.APrintVisitor) IPrinter(org.apache.hyracks.algebricks.data.IPrinter) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IAType(org.apache.asterix.om.types.IAType) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 15 with IPrinter

use of org.apache.hyracks.algebricks.data.IPrinter in project asterixdb by apache.

the class ClassAdToADMTest method testSchemaful.

@SuppressWarnings("rawtypes")
public void testSchemaful() {
    try {
        File file = new File("target/classad-wtih-temporals.adm");
        File expected = new File(getClass().getResource("/classad/results/classad-with-temporals.adm").toURI().getPath());
        FileUtils.deleteQuietly(file);
        PrintStream printStream = new PrintStream(Files.newOutputStream(Paths.get(file.toURI())));
        String[] recordFieldNames = { "GlobalJobId", "Owner", "ClusterId", "ProcId", "RemoteWallClockTime", "CompletionDate", "QDate", "JobCurrentStartDate", "JobStartDate", "JobCurrentStartExecutingDate" };
        IAType[] recordFieldTypes = { BuiltinType.ASTRING, BuiltinType.ASTRING, BuiltinType.AINT32, BuiltinType.AINT32, BuiltinType.ADURATION, BuiltinType.ADATETIME, BuiltinType.ADATETIME, BuiltinType.ADATETIME, BuiltinType.ADATETIME, BuiltinType.ADATETIME };
        ARecordType recordType = new ARecordType("value", recordFieldNames, recordFieldTypes, true);
        int numOfTupleFields = 1;
        ISerializerDeserializer[] serdes = new ISerializerDeserializer[1];
        serdes[0] = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(recordType);
        IPrinterFactory[] printerFactories = new IPrinterFactory[1];
        printerFactories[0] = ADMPrinterFactoryProvider.INSTANCE.getPrinterFactory(recordType);
        // create output descriptor
        IPrinter[] printers = new IPrinter[printerFactories.length];
        for (int i = 0; i < printerFactories.length; i++) {
            printers[i] = printerFactories[i].createPrinter();
        }
        ClassAdObjectPool objectPool = new ClassAdObjectPool();
        String[] files = new String[] { "/classad/classad-with-temporals.classads" };
        ClassAdParser parser = new ClassAdParser(recordType, false, false, false, null, null, null, objectPool);
        ArrayTupleBuilder tb = new ArrayTupleBuilder(numOfTupleFields);
        for (String path : files) {
            List<Path> paths = new ArrayList<>();
            Map<String, String> config = new HashMap<>();
            config.put(ExternalDataConstants.KEY_RECORD_START, "[");
            config.put(ExternalDataConstants.KEY_RECORD_END, "]");
            paths.add(Paths.get(getClass().getResource(path).toURI()));
            FileSystemWatcher watcher = new FileSystemWatcher(paths, null, false);
            LocalFSInputStream in = new LocalFSInputStream(watcher);
            SemiStructuredRecordReader recordReader = new SemiStructuredRecordReader();
            recordReader.configure(in, config);
            while (recordReader.hasNext()) {
                tb.reset();
                IRawRecord<char[]> record = recordReader.next();
                parser.parse(record, tb.getDataOutput());
                tb.addFieldEndOffset();
                printTuple(tb, printers, printStream);
            }
            recordReader.close();
            printStream.close();
            Assert.assertTrue(FileUtils.contentEquals(file, expected));
        }
    } catch (Throwable th) {
        System.err.println("TEST FAILED");
        th.printStackTrace();
        Assert.assertTrue(false);
    }
    System.err.println("TEST PASSED");
}
Also used : ClassAdObjectPool(org.apache.asterix.external.classad.object.pool.ClassAdObjectPool) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CaseInsensitiveString(org.apache.asterix.external.classad.CaseInsensitiveString) ClassAdParser(org.apache.asterix.external.library.ClassAdParser) FileSystemWatcher(org.apache.asterix.external.util.FileSystemWatcher) LocalFSInputStream(org.apache.asterix.external.input.stream.LocalFSInputStream) Path(java.nio.file.Path) PrintStream(java.io.PrintStream) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IPrinterFactory(org.apache.hyracks.algebricks.data.IPrinterFactory) SemiStructuredRecordReader(org.apache.asterix.external.input.record.reader.stream.SemiStructuredRecordReader) File(java.io.File) ARecordType(org.apache.asterix.om.types.ARecordType) IPrinter(org.apache.hyracks.algebricks.data.IPrinter) IAType(org.apache.asterix.om.types.IAType)

Aggregations

PrintStream (java.io.PrintStream)17 IPrinter (org.apache.hyracks.algebricks.data.IPrinter)17 ATypeTag (org.apache.asterix.om.types.ATypeTag)14 IAType (org.apache.asterix.om.types.IAType)12 PointableAllocator (org.apache.asterix.om.pointables.PointableAllocator)10 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)10 Pair (org.apache.hyracks.algebricks.common.utils.Pair)10 List (java.util.List)4 APrintVisitor (org.apache.asterix.om.pointables.printer.adm.APrintVisitor)3 APrintVisitor (org.apache.asterix.om.pointables.printer.json.clean.APrintVisitor)3 APrintVisitor (org.apache.asterix.om.pointables.printer.json.lossless.APrintVisitor)3 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)3 File (java.io.File)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LocalFSInputStream (org.apache.asterix.external.input.stream.LocalFSInputStream)2 FileSystemWatcher (org.apache.asterix.external.util.FileSystemWatcher)2 ARecordType (org.apache.asterix.om.types.ARecordType)2 IPrinterFactory (org.apache.hyracks.algebricks.data.IPrinterFactory)2