Search in sources :

Example 1 with Factory

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

the class AppendObjectBlobs method main.

public static void main(String[] args) throws JournalException, IOException {
    final String dirToIndex = args[1];
    JournalConfiguration configuration = new JournalConfigurationBuilder().build(args[0]);
    try (Factory factory = new Factory(configuration, 1000, 1, 0)) {
        try (JournalWriter writer = factory.writer(new JournalStructure("files") {

            {
                $sym("name").index();
                $bin("data");
                $ts();
            }
        })) {
            long t = System.currentTimeMillis();
            int count = processDir(writer, new File(dirToIndex));
            System.out.println("Added " + count + " files in " + (System.currentTimeMillis() - t) + " ms.");
        }
    }
}
Also used : JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) JournalWriter(com.questdb.store.JournalWriter) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder)

Example 2 with Factory

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

the class AppendObjectConcurrent method main.

/**
 * Multiple threads append to same journal concurrently.
 * <p>
 * Single append thread receives data from multiple publishers via circular queue using QuestDB's thread messaging mechanism.
 * Please refer to my blog post: http://blog.questdb.org/2016/08/the-art-of-thread-messaging.html for more details.
 */
public static void main(String[] args) throws JournalException, InterruptedException {
    if (args.length != 1) {
        System.out.println("Usage: " + AppendObjectConcurrent.class.getName() + " <path>");
        System.exit(1);
    }
    // 
    RingQueue<Quote> queue = new RingQueue<>(Quote::new, 4096);
    // publisher sequence
    final MPSequence pubSequence = new MPSequence(queue.getCapacity());
    final SCSequence subSequence = new SCSequence();
    // create circular dependency between sequences
    pubSequence.then(subSequence).then(pubSequence);
    // run configuration
    int nThreads = 2;
    int nMessages = 1000000;
    JournalConfiguration configuration = ModelConfiguration.CONFIG.build(args[0]);
    try (Factory factory = new Factory(configuration, 1000, 1, 0)) {
        // start publishing threads
        for (int i = 0; i < nThreads; i++) {
            new Thread(new Publisher(queue, pubSequence, nMessages)).start();
        }
        // consume messages in main thread
        int count = 0;
        int deadline = nMessages * nThreads;
        try (JournalWriter<Quote> writer = factory.writer(Quote.class)) {
            while (count < deadline) {
                long cursor = subSequence.next();
                if (cursor < 0) {
                    LockSupport.parkNanos(1);
                    continue;
                }
                long available = subSequence.available();
                while (cursor < available) {
                    Quote q = queue.get(cursor++);
                    q.setTimestamp(System.currentTimeMillis());
                    try {
                        writer.append(q);
                    } catch (JournalException e) {
                        // append may fail, log and continue
                        // N.B. this logging uses builder pattern to construct message. Building finishes with $() call.
                        LOG.error().$("Sequence failed: ").$(cursor - 1).$(e).$();
                    }
                    count++;
                }
                subSequence.done(available - 1);
                try {
                    writer.commit();
                } catch (JournalException e) {
                    // something serious, attempt to rollback
                    LOG.error().$("Batch commit() failed [").$(available - 1).$(']').$(e).$();
                }
            }
        }
        LOG.info().$("Writer done").$();
    }
}
Also used : MPSequence(com.questdb.mp.MPSequence) RingQueue(com.questdb.mp.RingQueue) JournalException(com.questdb.std.ex.JournalException) Factory(com.questdb.store.factory.Factory) LogFactory(com.questdb.log.LogFactory) Quote(org.questdb.examples.support.Quote) SCSequence(com.questdb.mp.SCSequence) JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration)

Example 3 with Factory

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

the class AppendObjectSortMerge method main.

/**
 * For cases where incoming data feed is not in chronological order but you would like your journal to be in chronological order.
 * This is a lossy way to append data as journal would only be merging a slice of data as specified by "lag" attribute.
 */
public static void main(String[] args) throws JournalException {
    if (args.length != 1) {
        System.out.println("Usage: " + AppendObjectSortMerge.class.getName() + " <path>");
        System.exit(1);
    }
    String journalLocation = args[0];
    try (Factory writerFactory = new Factory(new JournalConfigurationBuilder() {

        {
            $(Quote.class, "quote-lag").lag(24, // enable lag
            TimeUnit.HOURS).$ts();
        }
    }.build(journalLocation), 1000, 1, 0)) {
        // delete existing quote journal
        Files.delete(new File(writerFactory.getConfiguration().getJournalBase(), "quote-lag"));
        try (JournalWriter<Quote> writer = writerFactory.writer(Quote.class)) {
            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());
            // 20 batches of 50,000 quotes, total 1,000,000
            final int batchCount = 20;
            final int batchSize = 50000;
            final ArrayList<Quote> batch = new ArrayList<>(batchSize);
            // have pre-initialized array to reduce GC overhead
            for (int i = 0; i < batchSize; i++) {
                batch.add(new Quote());
            }
            long utc = System.currentTimeMillis();
            long t = System.nanoTime();
            for (int i = 0; i < batchCount; i++) {
                // populate batch in-memory
                for (int k = 0; k < batchSize; k++) {
                    Quote q = batch.get(k);
                    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");
                    long timestamp = utc + (i * batchSize + (batchSize - k)) * 1000L;
                    // make batches overlap (subtract 10 seconds)
                    timestamp -= 100000000L;
                    q.setTimestamp(timestamp);
                }
                // batch must be sorted before being presented to writer
                batch.sort(writer.getTimestampComparator());
                // append batch and have journal merge data
                writer.mergeAppend(batch);
            }
            // commit is necessary
            writer.commit();
            System.out.println("Journal size: " + writer.size());
            System.out.println("Generated " + batchCount * batchSize + " objects in " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t) + "ms.");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) Quote(org.questdb.examples.support.Quote) Random(java.util.Random) File(java.io.File)

Example 4 with Factory

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

the class AppendRawTimeSeries method main.

public static void main(String[] args) throws JournalException, ParserException {
    if (args.length < 1) {
        System.out.println("Usage: AppendRawTimeSeries <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();
            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 5 with Factory

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

the class AppendTextFile method main.

public static void main(String[] args) throws JournalException, ParserException, IOException {
    if (args.length < 1) {
        System.out.println("Usage: AppendRawUnordered <path>");
        System.exit(1);
    }
    try (Factory factory = new Factory(args[0], 1000, 1, 0)) {
        // import manager will determine file structure automatically
        ImportManager.importFile(factory, AppendTextFile.class.getResource("/movies.csv").getFile(), ',', null, false);
    }
}
Also used : Factory(com.questdb.store.factory.Factory)

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