Search in sources :

Example 1 with PlainTextLexer

use of com.questdb.parser.plaintext.PlainTextLexer in project questdb by bluestreak01.

the class ImportManager method importFile.

public static void importFile(Factory factory, String fileName, char delimiter, CharSequence schema, int sampleSize, boolean forceHeader) throws IOException {
    BootstrapEnv env = new BootstrapEnv();
    env.typeProbeCollection = new TypeProbeCollection();
    env.configuration = new ServerConfiguration();
    env.factory = factory;
    try (PlainTextLexer parser = new PlainTextLexer(env).of(delimiter)) {
        File file = new File(fileName);
        String location = file.getName();
        switch(factory.getConfiguration().exists(location)) {
            case JournalConfiguration.EXISTS_FOREIGN:
                throw new JournalRuntimeException("A foreign file/directory already exists: " + (new File(factory.getConfiguration().getJournalBase(), location)));
            default:
                try (PlainTextStoringParser l = new PlainTextStoringParser(env).of(location, false, false, PlainTextStoringParser.ATOMICITY_RELAXED)) {
                    analyzeAndParse(file, parser, l, schema, sampleSize, forceHeader);
                }
                break;
        }
    }
}
Also used : BootstrapEnv(com.questdb.BootstrapEnv) PlainTextStoringParser(com.questdb.parser.plaintext.PlainTextStoringParser) TypeProbeCollection(com.questdb.parser.typeprobe.TypeProbeCollection) ServerConfiguration(com.questdb.ServerConfiguration) JournalRuntimeException(com.questdb.common.JournalRuntimeException) PlainTextLexer(com.questdb.parser.plaintext.PlainTextLexer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with PlainTextLexer

use of com.questdb.parser.plaintext.PlainTextLexer in project questdb by bluestreak01.

the class CsvTest method testHeaders.

@Test
public void testHeaders() throws Exception {
    final List<String> names = new ArrayList<>();
    final List<String> expected = new ArrayList<String>() {

        {
            add("type");
            add("value");
            add("active");
            add("desc");
            add("grp");
        }
    };
    ImportManager.parse(new File(this.getClass().getResource("/csv/test-headers.csv").getFile()), new PlainTextLexer(env).of(','), 1024 * 1024, true, new PlainTextParser() {

        @Override
        public void onError(int line) {
        }

        @Override
        public void onFieldCount(int count) {
        }

        @Override
        public void onFields(int line, ObjList<DirectByteCharSequence> values, int hi) {
        }

        @Override
        public void onHeader(ObjList<DirectByteCharSequence> values, int hi) {
            for (int i = 0; i < hi; i++) {
                names.add(values.getQuick(i).toString());
            }
        }

        @Override
        public void onLineCount(int count) {
        }
    });
    TestUtils.assertEquals(expected.iterator(), names.iterator());
}
Also used : PlainTextParser(com.questdb.parser.plaintext.PlainTextParser) DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) ArrayList(java.util.ArrayList) PlainTextLexer(com.questdb.parser.plaintext.PlainTextLexer) File(java.io.File) Test(org.junit.Test)

Aggregations

PlainTextLexer (com.questdb.parser.plaintext.PlainTextLexer)2 File (java.io.File)2 BootstrapEnv (com.questdb.BootstrapEnv)1 ServerConfiguration (com.questdb.ServerConfiguration)1 JournalRuntimeException (com.questdb.common.JournalRuntimeException)1 PlainTextParser (com.questdb.parser.plaintext.PlainTextParser)1 PlainTextStoringParser (com.questdb.parser.plaintext.PlainTextStoringParser)1 TypeProbeCollection (com.questdb.parser.typeprobe.TypeProbeCollection)1 DirectByteCharSequence (com.questdb.std.str.DirectByteCharSequence)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1