Search in sources :

Example 6 with ClassAdParser

use of org.apache.asterix.external.library.ClassAdParser in project asterixdb by apache.

the class FunctionalTester method get_two_exprs.

/*********************************************************************
     * Function: get_two_exprs
     * Purpose:
     *
     * @throws IOException
     *********************************************************************/
public static void get_two_exprs(AMutableString line, ExprTreeHolder tree1, ExprTreeHolder tree2, State state, Parameters parameters, ClassAdObjectPool objectPool) throws IOException {
    int offset;
    ClassAdParser parser = new ClassAdParser(objectPool);
    StringLexerSource lexer_source = new StringLexerSource(line.getStringValue());
    tree1.setInnerTree(parser.parseExpression(lexer_source, false));
    if (tree1.getInnerTree() == null) {
        print_error_message("Couldn't parse first expression.", state);
        tree2.setInnerTree(null);
        throw new IOException();
    } else {
        if (parameters.debug) {
            System.out.print("# Tree1: ");
            print_expr(tree1, state, parameters, objectPool);
        }
        if (parser.peekToken() != TokenType.LEX_COMMA) {
            print_error_message("Missing comma.\n", state);
            tree1.setInnerTree(null);
            tree2.setInnerTree(null);
            ;
        } else {
            parser.consumeToken();
            tree2.setInnerTree(parser.parseNextExpression());
            offset = lexer_source.getCurrentLocation();
            shorten_line(line, offset);
            if (tree2.getInnerTree() == null) {
                print_error_message("Couldn't parse second expression.", state);
                tree1.setInnerTree(null);
                throw new IOException();
            } else if (parameters.debug) {
                System.out.print("# Tree2: ");
                print_expr(tree2, state, parameters, objectPool);
                System.out.print("# Tree1: ");
                print_expr(tree1, state, parameters, objectPool);
                System.out.println();
            }
        }
    }
    return;
}
Also used : ClassAdParser(org.apache.asterix.external.library.ClassAdParser) StringLexerSource(org.apache.asterix.external.classad.StringLexerSource) IOException(java.io.IOException) PrettyPrint(org.apache.asterix.external.classad.PrettyPrint)

Example 7 with ClassAdParser

use of org.apache.asterix.external.library.ClassAdParser 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)

Example 8 with ClassAdParser

use of org.apache.asterix.external.library.ClassAdParser in project asterixdb by apache.

the class ClassAdToADMTest method testSchemaless.

/**
     *
     */
public void testSchemaless() {
    try {
        ClassAdObjectPool objectPool = new ClassAdObjectPool();
        ClassAd pAd = new ClassAd(objectPool);
        String[] files = new String[] { "/classad/jobads.txt" };
        ClassAdParser parser = new ClassAdParser(objectPool);
        CharArrayLexerSource lexerSource = new CharArrayLexerSource();
        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);
            try {
                Value val = new Value(objectPool);
                while (recordReader.hasNext()) {
                    val.reset();
                    IRawRecord<char[]> record = recordReader.next();
                    lexerSource.setNewSource(record.get());
                    parser.setLexerSource(lexerSource);
                    parser.parseNext(pAd);
                    Map<CaseInsensitiveString, ExprTree> attrs = pAd.getAttrList();
                    for (Entry<CaseInsensitiveString, ExprTree> entry : attrs.entrySet()) {
                        ExprTree tree = entry.getValue();
                        switch(tree.getKind()) {
                            case ATTRREF_NODE:
                            case CLASSAD_NODE:
                            case EXPR_ENVELOPE:
                            case EXPR_LIST_NODE:
                            case FN_CALL_NODE:
                            case OP_NODE:
                                break;
                            case LITERAL_NODE:
                                break;
                            default:
                                System.out.println("Something is wrong");
                                break;
                        }
                    }
                }
            } finally {
                recordReader.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : Path(java.nio.file.Path) ClassAdObjectPool(org.apache.asterix.external.classad.object.pool.ClassAdObjectPool) ClassAd(org.apache.asterix.external.classad.ClassAd) HashMap(java.util.HashMap) CharArrayLexerSource(org.apache.asterix.external.classad.CharArrayLexerSource) ArrayList(java.util.ArrayList) CaseInsensitiveString(org.apache.asterix.external.classad.CaseInsensitiveString) CaseInsensitiveString(org.apache.asterix.external.classad.CaseInsensitiveString) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) ClassAdParser(org.apache.asterix.external.library.ClassAdParser) FileSystemWatcher(org.apache.asterix.external.util.FileSystemWatcher) SemiStructuredRecordReader(org.apache.asterix.external.input.record.reader.stream.SemiStructuredRecordReader) Value(org.apache.asterix.external.classad.Value) ExprTree(org.apache.asterix.external.classad.ExprTree) LocalFSInputStream(org.apache.asterix.external.input.stream.LocalFSInputStream)

Example 9 with ClassAdParser

use of org.apache.asterix.external.library.ClassAdParser in project asterixdb by apache.

the class ClassAd method evaluateExpr.

public boolean evaluateExpr(String buf, Value result) throws HyracksDataException {
    boolean successfully_evaluated;
    ExprTreeHolder tree = objectPool.mutableExprPool.get();
    ClassAdParser parser = objectPool.classAdParserPool.get();
    try {
        if (parser.parseExpression(buf, tree)) {
            successfully_evaluated = evaluateExpr(tree, result);
        } else {
            successfully_evaluated = false;
        }
    } catch (IOException e) {
        throw new HyracksDataException(e);
    }
    return successfully_evaluated;
}
Also used : ClassAdParser(org.apache.asterix.external.library.ClassAdParser) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Aggregations

ClassAdParser (org.apache.asterix.external.library.ClassAdParser)9 ExprTree (org.apache.asterix.external.classad.ExprTree)5 ArrayList (java.util.ArrayList)4 ClassAd (org.apache.asterix.external.classad.ClassAd)4 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 CaseInsensitiveString (org.apache.asterix.external.classad.CaseInsensitiveString)3 Value (org.apache.asterix.external.classad.Value)3 ClassAdObjectPool (org.apache.asterix.external.classad.object.pool.ClassAdObjectPool)3 SemiStructuredRecordReader (org.apache.asterix.external.input.record.reader.stream.SemiStructuredRecordReader)3 LocalFSInputStream (org.apache.asterix.external.input.stream.LocalFSInputStream)3 FileSystemWatcher (org.apache.asterix.external.util.FileSystemWatcher)3 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)3 IOException (java.io.IOException)2 AMutableCharArrayString (org.apache.asterix.external.classad.AMutableCharArrayString)2 CharArrayLexerSource (org.apache.asterix.external.classad.CharArrayLexerSource)2 PrettyPrint (org.apache.asterix.external.classad.PrettyPrint)2 StringLexerSource (org.apache.asterix.external.classad.StringLexerSource)2 AMutableString (org.apache.asterix.om.base.AMutableString)2 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)2