Search in sources :

Example 6 with RecordSourcePrinter

use of com.questdb.ql.RecordSourcePrinter in project questdb by bluestreak01.

the class HttpServerTest method testImportWrongType.

@Test
public void testImportWrongType() throws Exception {
    BootstrapEnv env = new BootstrapEnv();
    env.configuration = new ServerConfiguration();
    env.factory = getFactory();
    env.typeProbeCollection = TYPE_PROBE_COLLECTION;
    env.dateLocaleFactory = DateLocaleFactory.INSTANCE;
    env.dateFormatFactory = DATE_FORMAT_FACTORY;
    env.matcher = new SimpleUrlMatcher() {

        {
            put("/imp", new ImportHandler(env));
        }
    };
    HttpServer server = new HttpServer(env);
    server.start();
    try {
        Assert.assertEquals(200, HttpTestUtils.upload("/csv/test-import.csv", "http://localhost:9000/imp?fmt=json", "[{\"name\":\"IsoDate\", \"type\":\"DATE\"}, {\"name\":\"IntCol\", \"type\":\"DOUBLE\"}]", null));
        Assert.assertEquals(200, HttpTestUtils.upload("/csv/test-import.csv", "http://localhost:9000/imp", "[{\"name\":\"IsoDate\", \"type\":\"DATE\"}]", null));
        StringSink sink = new StringSink();
        RecordSourcePrinter printer = new RecordSourcePrinter(sink);
        QueryCompiler qc = new QueryCompiler(env);
        RecordSource src1 = qc.compile(env.factory, "select count(StrSym), count(IntSym), count(IntCol), count(long), count() from 'test-import.csv'");
        try {
            printer.print(src1, env.factory);
            TestUtils.assertEquals("252\t252\t256\t258\t258\n", sink);
        } finally {
            Misc.free(src1);
        }
        RecordSource src2 = qc.compile(env.factory, "'test-import.csv'");
        try {
            Assert.assertEquals(ColumnType.DOUBLE, src2.getMetadata().getColumn("IntCol").getType());
        } finally {
            Misc.free(src2);
        }
    } finally {
        server.halt();
    }
}
Also used : BootstrapEnv(com.questdb.BootstrapEnv) RecordSource(com.questdb.ql.RecordSource) ServerConfiguration(com.questdb.ServerConfiguration) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) ImportHandler(com.questdb.net.http.handlers.ImportHandler) StringSink(com.questdb.std.str.StringSink) QueryCompiler(com.questdb.parser.sql.QueryCompiler) AbstractJournalTest(com.questdb.net.ha.AbstractJournalTest) Test(org.junit.Test)

Example 7 with RecordSourcePrinter

use of com.questdb.ql.RecordSourcePrinter in project questdb by bluestreak01.

the class HttpServerTest method testImportOverwrite.

@Test
public void testImportOverwrite() throws Exception {
    final ServerConfiguration configuration = new ServerConfiguration();
    BootstrapEnv env = new BootstrapEnv();
    env.configuration = configuration;
    env.factory = getFactory();
    env.typeProbeCollection = TYPE_PROBE_COLLECTION;
    env.matcher = new SimpleUrlMatcher() {

        {
            put("/imp", new ImportHandler(env));
        }
    };
    HttpServer server = new HttpServer(env);
    server.start();
    try {
        StringSink sink = new StringSink();
        RecordSourcePrinter printer = new RecordSourcePrinter(sink);
        QueryCompiler qc = new QueryCompiler(env);
        Assert.assertEquals(200, HttpTestUtils.upload("/csv/test-import.csv", "http://localhost:9000/imp", null, null));
        printer.print(qc.compile(getFactory(), "select count() from 'test-import.csv'"), getFactory());
        TestUtils.assertEquals("129\n", sink);
        sink.clear();
        Assert.assertEquals(200, HttpTestUtils.upload("/csv/test-headers.csv", "http://localhost:9000/imp?name=test-import.csv&overwrite=true&durable=true", null, null));
        printer.print(qc.compile(getFactory(), "select count() from 'test-import.csv'"), getFactory());
        TestUtils.assertEquals("5\n", sink);
    } finally {
        server.halt();
    }
}
Also used : BootstrapEnv(com.questdb.BootstrapEnv) ServerConfiguration(com.questdb.ServerConfiguration) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) ImportHandler(com.questdb.net.http.handlers.ImportHandler) StringSink(com.questdb.std.str.StringSink) QueryCompiler(com.questdb.parser.sql.QueryCompiler) AbstractJournalTest(com.questdb.net.ha.AbstractJournalTest) Test(org.junit.Test)

Example 8 with RecordSourcePrinter

use of com.questdb.ql.RecordSourcePrinter in project questdb by bluestreak01.

the class HttpServerTest method testImportForcedHeader.

@Test
public void testImportForcedHeader() throws Exception {
    BootstrapEnv env = new BootstrapEnv();
    env.configuration = new ServerConfiguration();
    env.factory = getFactory();
    env.typeProbeCollection = TYPE_PROBE_COLLECTION;
    env.matcher = new SimpleUrlMatcher() {

        {
            put("/imp", new ImportHandler(env));
        }
    };
    HttpServer server = new HttpServer(env);
    server.start();
    try {
        StringSink sink = new StringSink();
        RecordSourcePrinter printer = new RecordSourcePrinter(sink);
        QueryCompiler qc = new QueryCompiler(env);
        Assert.assertEquals(200, HttpTestUtils.upload("/csv/test-explicit-headers.csv", "http://localhost:9000/imp?name=test-import.csv&overwrite=true&durable=true&forceHeader=true", null, null));
        printer.print(qc.compile(getFactory(), "select count() from 'test-import.csv'"), getFactory());
        // expect first line to be treated as header
        TestUtils.assertEquals("2\n", sink);
    } finally {
        server.halt();
    }
}
Also used : BootstrapEnv(com.questdb.BootstrapEnv) ServerConfiguration(com.questdb.ServerConfiguration) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) ImportHandler(com.questdb.net.http.handlers.ImportHandler) StringSink(com.questdb.std.str.StringSink) QueryCompiler(com.questdb.parser.sql.QueryCompiler) AbstractJournalTest(com.questdb.net.ha.AbstractJournalTest) Test(org.junit.Test)

Example 9 with RecordSourcePrinter

use of com.questdb.ql.RecordSourcePrinter 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 10 with RecordSourcePrinter

use of com.questdb.ql.RecordSourcePrinter 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

RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)17 StringSink (com.questdb.std.str.StringSink)14 Test (org.junit.Test)13 QueryCompiler (com.questdb.parser.sql.QueryCompiler)11 BootstrapEnv (com.questdb.BootstrapEnv)9 ServerConfiguration (com.questdb.ServerConfiguration)9 AbstractJournalTest (com.questdb.net.ha.AbstractJournalTest)9 ImportHandler (com.questdb.net.http.handlers.ImportHandler)9 RecordSource (com.questdb.ql.RecordSource)7 CountDownLatch (java.util.concurrent.CountDownLatch)5 RecordCursor (com.questdb.common.RecordCursor)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 EntryLockedException (com.questdb.cairo.pool.ex.EntryLockedException)2 EntryUnavailableException (com.questdb.cairo.pool.ex.EntryUnavailableException)2 PoolClosedException (com.questdb.cairo.pool.ex.PoolClosedException)2 ClientConfig (com.questdb.net.ha.config.ClientConfig)2 ServerConfig (com.questdb.net.ha.config.ServerConfig)2 ServerNode (com.questdb.net.ha.config.ServerNode)2 StdoutSink (com.questdb.std.str.StdoutSink)2 JournalKey (com.questdb.store.JournalKey)2