Search in sources :

Example 1 with StdoutSink

use of com.questdb.std.str.StdoutSink in project questdb by bluestreak01.

the class SQLExamples method main.

public static void main(String[] args) throws JournalException, ParserException, IOException {
    if (args.length < 1) {
        System.out.println("Usage: SQLExamples <path>");
        System.exit(1);
    }
    try (Factory factory = new Factory(args[0], 1000, 1, 0)) {
        // import movies data to query
        ImportManager.importFile(factory, SQLExamples.class.getResource("/movies.csv").getFile(), ',', null, false);
        // Create SQL engine instance.
        QueryCompiler compiler = new QueryCompiler();
        try (RecordSource rs = compiler.compile(factory, "'movies.csv'")) {
            // Execute query and fetch results
            RecordCursor cursor = rs.prepareCursor(factory);
            try {
                while (cursor.hasNext()) {
                    Record record = cursor.next();
                }
            } finally {
                cursor.releaseCursor();
            }
        }
        // to simplify query demonstration we have generic record source printer
        RecordSourcePrinter printer = new RecordSourcePrinter(new StdoutSink());
        printer.print(compiler.compile(factory, "'movies.csv'"), factory);
        System.out.println("---------");
        // find movie by ID
        printer.print(compiler.compile(factory, "'movies.csv' where movieId = 62198"), factory);
        System.out.println("---------");
        // extract year from movie title
        printer.print(compiler.compile(factory, "select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' where movieId = 62198"), factory);
        System.out.println("---------");
        // order by movie year descending
        printer.print(compiler.compile(factory, "select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc"), factory);
        System.out.println("---------");
        // count titles by year
        printer.print(compiler.compile(factory, "select year, count() from (select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc)"), factory);
    }
}
Also used : RecordSource(com.questdb.ql.RecordSource) StdoutSink(com.questdb.std.str.StdoutSink) RecordCursor(com.questdb.common.RecordCursor) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) Factory(com.questdb.store.factory.Factory) Record(com.questdb.common.Record) QueryCompiler(com.questdb.parser.sql.QueryCompiler)

Example 2 with StdoutSink

use of com.questdb.std.str.StdoutSink in project questdb by bluestreak01.

the class SQLParameters method main.

public static void main(String[] args) throws JournalException, ParserException, IOException {
    if (args.length < 1) {
        System.out.println("Usage: SQLParameters <path>");
        System.exit(1);
    }
    JournalConfiguration configuration = new JournalConfigurationBuilder().build(args[0]);
    try (Factory factory = new Factory(configuration, 1000, 1, 0)) {
        // import movies data to query
        ImportManager.importFile(factory, SQLParameters.class.getResource("/movies.csv").getFile(), ',', null, false);
        // Create SQL engine instance.
        QueryCompiler compiler = new QueryCompiler();
        try (RecordSource rs = compiler.compile(factory, "'movies.csv' where movieId = :id")) {
            RecordSourcePrinter printer = new RecordSourcePrinter(new StdoutSink());
            // use of parameters avoids expensive query compilation
            rs.getParam(":id").set(62198);
            printer.print(rs, factory);
            System.out.println("----------------");
            rs.getParam(":id").set(125);
            printer.print(rs, factory);
        }
    }
}
Also used : JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) RecordSource(com.questdb.ql.RecordSource) StdoutSink(com.questdb.std.str.StdoutSink) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) QueryCompiler(com.questdb.parser.sql.QueryCompiler)

Aggregations

QueryCompiler (com.questdb.parser.sql.QueryCompiler)2 RecordSource (com.questdb.ql.RecordSource)2 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)2 StdoutSink (com.questdb.std.str.StdoutSink)2 Factory (com.questdb.store.factory.Factory)2 Record (com.questdb.common.Record)1 RecordCursor (com.questdb.common.RecordCursor)1 JournalConfiguration (com.questdb.store.factory.configuration.JournalConfiguration)1 JournalConfigurationBuilder (com.questdb.store.factory.configuration.JournalConfigurationBuilder)1