Search in sources :

Example 6 with Factory

use of com.questdb.store.factory.Factory 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 7 with Factory

use of com.questdb.store.factory.Factory in project questdb by bluestreak01.

the class SQLExportToTextFile method main.

public static void main(String[] args) throws JournalException, IOException, ParserException {
    if (args.length < 1) {
        System.out.println("Usage: SQLExportToTextFile <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, "select year, count() count from (select title, match('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc)")) {
            ExportManager.export(rs, factory, new File(args[0], "export.csv"), ',');
        }
    }
}
Also used : RecordSource(com.questdb.ql.RecordSource) Factory(com.questdb.store.factory.Factory) QueryCompiler(com.questdb.parser.sql.QueryCompiler) File(java.io.File)

Example 8 with Factory

use of com.questdb.store.factory.Factory 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)

Example 9 with Factory

use of com.questdb.store.factory.Factory in project questdb by bluestreak01.

the class AuthReplicationClientMain method main.

public static void main(String[] args) throws Exception {
    JournalConfiguration configuration = new JournalConfigurationBuilder().build(args[0]);
    Factory factory = new Factory(configuration);
    final JournalClient client = new JournalClient(factory, () -> "MY SECRET".getBytes("UTF8"));
    final Journal<Price> reader = factory.reader(Price.class, "price-copy");
    reader.setSequentialAccess(true);
    client.subscribe(Price.class, null, "price-copy", new JournalListener() {

        @Override
        public void onCommit() {
            int count = 0;
            long t = 0;
            for (Price p : JournalIterators.incrementBufferedIterator(reader)) {
                if (count == 0) {
                    t = p.getNanos();
                }
                count++;
            }
            System.out.println("took: " + (System.nanoTime() - t) + ", count=" + count);
        }

        @Override
        public void onEvent(int event) {
            System.out.println("There was an error");
        }
    });
    client.start();
    System.out.println("Client started");
}
Also used : JournalClient(com.questdb.net.ha.JournalClient) JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) Price(org.questdb.examples.support.Price) JournalListener(com.questdb.store.JournalListener) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder)

Example 10 with Factory

use of com.questdb.store.factory.Factory in project questdb by bluestreak01.

the class ClusteredProducerMain method main.

public static void main(String[] args) throws JournalException, IOException, JournalNetworkException, NumericException {
    final String pathToDatabase = args[0];
    final int instance = Numbers.parseInt(args[1]);
    final JournalConfiguration configuration = new JournalConfigurationBuilder() {

        {
            $(Price.class).$ts();
        }
    }.build(pathToDatabase);
    final Factory factory = new Factory(configuration, 1000, 1, 0);
    final JournalWriter<Price> writer = factory.writer(new JournalKey<>(Price.class, null, PartitionBy.DEFAULT, 1000000000));
    final WorkerController wc = new WorkerController(writer);
    final ClusterController cc = new ClusterController(new ServerConfig() {

        {
            addNode(new ServerNode(1, "127.0.0.1:7080"));
            addNode(new ServerNode(2, "127.0.0.1:7090"));
        }
    }, new ClientConfig(), factory, instance, new ArrayList<JournalWriter>() {

        {
            add(writer);
        }
    }, wc);
    cc.start();
    Runtime.getRuntime().addShutdownHook(new Thread(cc::halt));
}
Also used : JournalWriter(com.questdb.store.JournalWriter) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) ServerConfig(com.questdb.net.ha.config.ServerConfig) JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) ClusterController(com.questdb.net.ha.ClusterController) Price(org.questdb.examples.support.Price) ServerNode(com.questdb.net.ha.config.ServerNode) ClientConfig(com.questdb.net.ha.config.ClientConfig)

Aggregations

Factory (com.questdb.store.factory.Factory)27 JournalConfigurationBuilder (com.questdb.store.factory.configuration.JournalConfigurationBuilder)13 JournalConfiguration (com.questdb.store.factory.configuration.JournalConfiguration)11 Price (org.questdb.examples.support.Price)8 File (java.io.File)6 QueryCompiler (com.questdb.parser.sql.QueryCompiler)5 JournalWriter (com.questdb.store.JournalWriter)5 Quote (org.questdb.examples.support.Quote)5 LogFactory (com.questdb.log.LogFactory)4 JournalClient (com.questdb.net.ha.JournalClient)4 RecordSource (com.questdb.ql.RecordSource)4 Rnd (com.questdb.std.Rnd)4 JournalListener (com.questdb.store.JournalListener)4 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)4 AbstractTest (com.questdb.test.tools.AbstractTest)4 Test (org.junit.Test)4 JournalServer (com.questdb.net.ha.JournalServer)3 ClientConfig (com.questdb.net.ha.config.ClientConfig)3 JournalException (com.questdb.std.ex.JournalException)3 JournalEntryWriter (com.questdb.store.JournalEntryWriter)3