Search in sources :

Example 6 with Source

use of org.apache.calcite.util.Source in project calcite by apache.

the class CsvSchema method createTableMap.

private Map<String, Table> createTableMap() {
    // Look for files in the directory ending in ".csv", ".csv.gz", ".json",
    // ".json.gz".
    final Source baseSource = Sources.of(directoryFile);
    File[] files = directoryFile.listFiles(new FilenameFilter() {

        public boolean accept(File dir, String name) {
            final String nameSansGz = trim(name, ".gz");
            return nameSansGz.endsWith(".csv") || nameSansGz.endsWith(".json");
        }
    });
    if (files == null) {
        System.out.println("directory " + directoryFile + " not found");
        files = new File[0];
    }
    // Build a map from table name to table; each file becomes a table.
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();
    for (File file : files) {
        Source source = Sources.of(file);
        Source sourceSansGz = source.trim(".gz");
        final Source sourceSansJson = sourceSansGz.trimOrNull(".json");
        if (sourceSansJson != null) {
            JsonTable table = new JsonTable(source);
            builder.put(sourceSansJson.relative(baseSource).path(), table);
            continue;
        }
        final Source sourceSansCsv = sourceSansGz.trim(".csv");
        final Table table = createTable(source);
        builder.put(sourceSansCsv.relative(baseSource).path(), table);
    }
    return builder.build();
}
Also used : FilenameFilter(java.io.FilenameFilter) Table(org.apache.calcite.schema.Table) File(java.io.File) Source(org.apache.calcite.util.Source) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with Source

use of org.apache.calcite.util.Source in project calcite by apache.

the class CsvStreamTableFactory method create.

public CsvTable create(SchemaPlus schema, String name, Map<String, Object> operand, RelDataType rowType) {
    String fileName = (String) operand.get("file");
    File file = new File(fileName);
    final File base = (File) operand.get(ModelHandler.ExtraOperand.BASE_DIRECTORY.camelName);
    if (base != null && !file.isAbsolute()) {
        file = new File(base, fileName);
    }
    final Source source = Sources.of(file);
    final RelProtoDataType protoRowType = rowType != null ? RelDataTypeImpl.proto(rowType) : null;
    return new CsvStreamScannableTable(source, protoRowType);
}
Also used : RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) File(java.io.File) Source(org.apache.calcite.util.Source)

Example 8 with Source

use of org.apache.calcite.util.Source in project calcite by apache.

the class CsvTableFactory method create.

public CsvTable create(SchemaPlus schema, String name, Map<String, Object> operand, RelDataType rowType) {
    String fileName = (String) operand.get("file");
    final File base = (File) operand.get(ModelHandler.ExtraOperand.BASE_DIRECTORY.camelName);
    final Source source = Sources.file(base, fileName);
    final RelProtoDataType protoRowType = rowType != null ? RelDataTypeImpl.proto(rowType) : null;
    return new CsvScannableTable(source, protoRowType);
}
Also used : RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) File(java.io.File) Source(org.apache.calcite.util.Source)

Example 9 with Source

use of org.apache.calcite.util.Source in project calcite by apache.

the class FileSchema method addTable.

private boolean addTable(ImmutableMap.Builder<String, Table> builder, Source source, String tableName, Map<String, Object> tableDef) {
    final Source sourceSansGz = source.trim(".gz");
    final Source sourceSansJson = sourceSansGz.trimOrNull(".json");
    if (sourceSansJson != null) {
        JsonTable table = new JsonTable(source);
        builder.put(Util.first(tableName, sourceSansJson.path()), table);
        return true;
    }
    final Source sourceSansCsv = sourceSansGz.trimOrNull(".csv");
    if (sourceSansCsv != null) {
        final Table table = new CsvFilterableTable(source, null);
        builder.put(Util.first(tableName, sourceSansCsv.path()), table);
        return true;
    }
    if (tableDef != null) {
        try {
            FileTable table = FileTable.create(source, tableDef);
            builder.put(Util.first(tableName, source.path()), table);
            return true;
        } catch (Exception e) {
            throw new RuntimeException("Unable to instantiate table for: " + tableName);
        }
    }
    return false;
}
Also used : JsonTable(org.apache.calcite.adapter.csv.JsonTable) Table(org.apache.calcite.schema.Table) CsvFilterableTable(org.apache.calcite.adapter.csv.CsvFilterableTable) JsonTable(org.apache.calcite.adapter.csv.JsonTable) CsvFilterableTable(org.apache.calcite.adapter.csv.CsvFilterableTable) Source(org.apache.calcite.util.Source) MalformedURLException(java.net.MalformedURLException)

Example 10 with Source

use of org.apache.calcite.util.Source in project calcite by apache.

the class FileSchema method getTableMap.

@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();
    for (Map<String, Object> tableDef : this.tables) {
        String tableName = (String) tableDef.get("name");
        try {
            addTable(builder, tableDef);
        } catch (MalformedURLException e) {
            throw new RuntimeException("Unable to instantiate table for: " + tableName);
        }
    }
    // Look for files in the directory ending in ".csv", ".csv.gz", ".json",
    // ".json.gz".
    final Source baseSource = Sources.of(baseDirectory);
    File[] files = baseDirectory.listFiles(new FilenameFilter() {

        public boolean accept(File dir, String name) {
            final String nameSansGz = trim(name, ".gz");
            return nameSansGz.endsWith(".csv") || nameSansGz.endsWith(".json");
        }
    });
    if (files == null) {
        System.out.println("directory " + baseDirectory + " not found");
        files = new File[0];
    }
    // Build a map from table name to table; each file becomes a table.
    for (File file : files) {
        Source source = Sources.of(file);
        Source sourceSansGz = source.trim(".gz");
        final Source sourceSansJson = sourceSansGz.trimOrNull(".json");
        if (sourceSansJson != null) {
            JsonTable table = new JsonTable(source);
            builder.put(sourceSansJson.relative(baseSource).path(), table);
            continue;
        }
        final Source sourceSansCsv = sourceSansGz.trimOrNull(".csv");
        if (sourceSansCsv != null) {
            addTable(builder, source, sourceSansCsv.relative(baseSource).path(), null);
        }
    }
    return builder.build();
}
Also used : MalformedURLException(java.net.MalformedURLException) Table(org.apache.calcite.schema.Table) CsvFilterableTable(org.apache.calcite.adapter.csv.CsvFilterableTable) JsonTable(org.apache.calcite.adapter.csv.JsonTable) JsonTable(org.apache.calcite.adapter.csv.JsonTable) ImmutableMap(com.google.common.collect.ImmutableMap) Source(org.apache.calcite.util.Source) FilenameFilter(java.io.FilenameFilter) File(java.io.File)

Aggregations

Source (org.apache.calcite.util.Source)14 Test (org.junit.Test)8 Elements (org.jsoup.select.Elements)6 File (java.io.File)4 Table (org.apache.calcite.schema.Table)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 FilenameFilter (java.io.FilenameFilter)2 MalformedURLException (java.net.MalformedURLException)2 CsvFilterableTable (org.apache.calcite.adapter.csv.CsvFilterableTable)2 JsonTable (org.apache.calcite.adapter.csv.JsonTable)2 RelProtoDataType (org.apache.calcite.rel.type.RelProtoDataType)2