Search in sources :

Example 21 with Factory

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

the class AppendObjectTimeSeries method main.

/**
 * Appends 1 million quotes to journal. Timestamp values are in chronological order.
 */
public static void main(String[] args) throws JournalException {
    if (args.length != 1) {
        System.out.println("Usage: " + AppendObjectTimeSeries.class.getName() + " <path>");
        System.exit(1);
    }
    String journalLocation = args[0];
    JournalConfiguration configuration = new JournalConfigurationBuilder() {

        {
            $(Quote.class, "quote").$ts();
        }
    }.build(journalLocation);
    try (Factory factory = new Factory(configuration, 1000, 1, 0)) {
        // delete existing quote journal
        Files.delete(new File(configuration.getJournalBase(), "quote"));
        try (JournalWriter<Quote> writer = factory.writer(Quote.class)) {
            final int count = 1000000;
            final String[] symbols = { "AGK.L", "BP.L", "TLW.L", "ABF.L", "LLOY.L", "BT-A.L", "WTB.L", "RRS.L", "ADM.L", "GKN.L", "HSBA.L" };
            final Random r = new Random(System.currentTimeMillis());
            // reuse same same instance of Quote class to keep GC under control
            final Quote q = new Quote();
            long t = System.nanoTime();
            for (int i = 0; i < count; i++) {
                // prepare object for new set of data
                q.clear();
                // generate some data
                q.setSym(symbols[Math.abs(r.nextInt() % (symbols.length - 1))]);
                q.setAsk(Math.abs(r.nextDouble()));
                q.setBid(Math.abs(r.nextDouble()));
                q.setAskSize(Math.abs(r.nextInt() % 10000));
                q.setBidSize(Math.abs(r.nextInt() % 10000));
                q.setEx("LXE");
                q.setMode("Fast trading");
                q.setTimestamp(System.currentTimeMillis());
                writer.append(q);
            }
            // commit is necessary
            writer.commit();
            System.out.println("Journal size: " + writer.size());
            System.out.println("Generated " + count + " objects in " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t) + "ms.");
        }
    }
}
Also used : Quote(org.questdb.examples.support.Quote) JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) Random(java.util.Random) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) File(java.io.File)

Example 22 with Factory

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

the class AppendObjectUnordered method main.

/**
 * Appends 1 million quotes with random timestamp values. Journal doesn't enforce order.
 */
public static void main(String[] args) throws JournalException {
    if (args.length != 1) {
        System.out.println("Usage: " + AppendObjectTimeSeries.class.getName() + " <path>");
        System.exit(1);
    }
    String journalLocation = args[0];
    try (Factory factory = new Factory(journalLocation, 1000, 1, 0)) {
        // delete existing quote journal
        Files.delete(new File(factory.getConfiguration().getJournalBase(), "quote-unordered"));
        try (JournalWriter<Quote> writer = factory.writer(Quote.class)) {
            final int count = 1000000;
            final String[] symbols = { "AGK.L", "BP.L", "TLW.L", "ABF.L", "LLOY.L", "BT-A.L", "WTB.L", "RRS.L", "ADM.L", "GKN.L", "HSBA.L" };
            final Random r = new Random(System.currentTimeMillis());
            // reuse same same instance of Quote class to keep GC under control
            final Quote q = new Quote();
            long t = System.nanoTime();
            for (int i = 0; i < count; i++) {
                // prepare object for new set of data
                q.clear();
                // generate some data
                q.setSym(symbols[Math.abs(r.nextInt() % (symbols.length - 1))]);
                q.setAsk(Math.abs(r.nextDouble()));
                q.setBid(Math.abs(r.nextDouble()));
                q.setAskSize(Math.abs(r.nextInt() % 10000));
                q.setBidSize(Math.abs(r.nextInt() % 10000));
                q.setEx("LXE");
                q.setMode("Fast trading");
                q.setTimestamp(r.nextLong());
                // 
                writer.append(q);
            }
            // commit is necessary
            writer.commit();
            System.out.println("Journal size: " + writer.size());
            System.out.println("Generated " + count + " objects in " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t) + "ms.");
        }
    }
}
Also used : Quote(org.questdb.examples.support.Quote) Random(java.util.Random) Factory(com.questdb.store.factory.Factory) File(java.io.File)

Example 23 with Factory

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

the class AppendRawPartitioned method main.

public static void main(String[] args) throws JournalException, ParserException {
    if (args.length < 1) {
        System.out.println("Usage: AppendRawPartitioned <path>");
        System.exit(1);
    }
    final String location = args[0];
    // factory can be reused in application and must be explicitly closed when no longer needed.
    try (Factory factory = new Factory(location, 1000, 1, 0)) {
        // to populate it.
        try (JournalWriter writer = factory.writer(new JournalStructure("customers").$int("id").$str("name").$ts("updateDate").partitionBy(PartitionBy.DAY).$())) {
            Rnd rnd = new Rnd();
            long timestamp = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) {
                // enforce timestamp order
                JournalEntryWriter ew = writer.entryWriter(timestamp);
                // columns accessed by index
                ew.putInt(0, rnd.nextPositiveInt());
                ew.putStr(1, rnd.nextChars(25));
                // increment timestamp by 30 seconds
                timestamp += 30000;
                // append record to journal
                ew.append();
            }
            // commit all records at once
            // there is no limit on how many records can be in the same transaction
            writer.commit();
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Factory(com.questdb.store.factory.Factory) Rnd(com.questdb.std.Rnd) JournalEntryWriter(com.questdb.store.JournalEntryWriter)

Example 24 with Factory

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

the class AppendRawUnordered method main.

public static void main(String[] args) throws JournalException, ParserException {
    if (args.length < 1) {
        System.out.println("Usage: AppendRawUnordered <path>");
        System.exit(1);
    }
    final String location = args[0];
    // factory can be reused in application and must be explicitly closed when no longer needed.
    try (Factory factory = new Factory(location, 1000, 1, 0)) {
        // to populate it.
        try (JournalWriter writer = factory.writer(new JournalStructure("customers").$int("id").$str("name").$ts("updateDate").$())) {
            Rnd rnd = new Rnd();
            int updateDateIndex = writer.getMetadata().getColumnIndex("updateDate");
            long timestamp = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) {
                // timestamp order is enforced by passing value to entryWriter() call
                // in this example we don't pass timestamp and ordering is not enforced
                JournalEntryWriter ew = writer.entryWriter();
                // columns accessed by index
                ew.putInt(0, rnd.nextPositiveInt());
                ew.putStr(1, rnd.nextChars(25));
                // you can use column index we looked up earlier
                ew.putDate(updateDateIndex, timestamp);
                // increment timestamp by 30 seconds
                timestamp += 30000;
                // append record to journal
                ew.append();
            }
            // commit all records at once
            // there is no limit on how many records can be in the same transaction
            writer.commit();
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Factory(com.questdb.store.factory.Factory) Rnd(com.questdb.std.Rnd) JournalEntryWriter(com.questdb.store.JournalEntryWriter)

Example 25 with Factory

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

the class SQLErrorHandling method main.

public static void main(String[] args) throws JournalException, ParserException, IOException {
    if (args.length < 1) {
        System.out.println("Usage: SQLErrorHandling <path>");
        System.exit(1);
    }
    try (Factory factory = new Factory(args[0], 1000, 1, 0)) {
        // import movies data to query
        ImportManager.importFile(factory, SQLErrorHandling.class.getResource("/movies.csv").getFile(), ',', null, false);
        // Create SQL engine instance.
        QueryCompiler compiler = new QueryCompiler();
        try {
            // in case of exception all allocated resources are freed automatically
            compiler.compile(factory, "'movies.csv' where movieIds = :id");
        } catch (ParserException e) {
            LOG.error().$("At (").$(QueryError.getPosition()).$(") : ").$(QueryError.getMessage()).$();
        }
    }
}
Also used : ParserException(com.questdb.ex.ParserException) Factory(com.questdb.store.factory.Factory) LogFactory(com.questdb.log.LogFactory) QueryCompiler(com.questdb.parser.sql.QueryCompiler)

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