Search in sources :

Example 1 with TableSchemaBuilder

use of org.apache.carbondata.core.metadata.schema.table.TableSchemaBuilder in project carbondata by apache.

the class HiveCarbonUtil method getTableInfo.

private static TableInfo getTableInfo(String tableName, String databaseName, String location, String sortColumnsString, String[] columns, String[] columnTypes, List<String> partitionColumns) throws SQLException {
    TableInfo tableInfo = new TableInfo();
    TableSchemaBuilder builder = new TableSchemaBuilder();
    builder.tableName(tableName);
    List<String> sortColumns = new ArrayList<>();
    if (sortColumnsString != null) {
        sortColumns = Arrays.asList(sortColumnsString.toLowerCase().split("\\,"));
    }
    PartitionInfo partitionInfo = null;
    AtomicInteger integer = new AtomicInteger();
    List<StructField> partitionStructFields = new ArrayList<>();
    for (int i = 0; i < columns.length; i++) {
        DataType dataType = DataTypeUtil.convertHiveTypeToCarbon(columnTypes[i]);
        Field field = new Field(columns[i].toLowerCase(), dataType);
        if (partitionColumns.contains(columns[i])) {
            partitionStructFields.add(new StructField(columns[i].toLowerCase(), dataType, field.getChildren()));
        } else {
            builder.addColumn(new StructField(columns[i].toLowerCase(), dataType, field.getChildren()), integer, sortColumns.contains(columns[i]), false);
        }
    }
    if (!partitionStructFields.isEmpty()) {
        List<ColumnSchema> partitionColumnSchemas = new ArrayList<>();
        for (StructField partitionStructField : partitionStructFields) {
            partitionColumnSchemas.add(builder.addColumn(partitionStructField, integer, sortColumns.contains(partitionStructField.getFieldName()), false));
        }
        partitionInfo = new PartitionInfo(partitionColumnSchemas, PartitionType.NATIVE_HIVE);
    }
    TableSchema tableSchema = builder.build();
    SchemaEvolution schemaEvol = new SchemaEvolution();
    List<SchemaEvolutionEntry> schemaEvolutionEntry = new ArrayList<>();
    schemaEvolutionEntry.add(new SchemaEvolutionEntry());
    schemaEvol.setSchemaEvolutionEntryList(schemaEvolutionEntry);
    tableSchema.setSchemaEvolution(schemaEvol);
    tableSchema.setPartitionInfo(partitionInfo);
    tableInfo.setDatabaseName(databaseName);
    tableInfo.setTablePath(location);
    tableInfo.setFactTable(tableSchema);
    tableInfo.setTableUniqueName(databaseName + "_" + tableName);
    return tableInfo;
}
Also used : TableSchema(org.apache.carbondata.core.metadata.schema.table.TableSchema) ArrayList(java.util.ArrayList) TableSchemaBuilder(org.apache.carbondata.core.metadata.schema.table.TableSchemaBuilder) ColumnSchema(org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema) SchemaEvolution(org.apache.carbondata.core.metadata.schema.SchemaEvolution) Field(org.apache.carbondata.core.metadata.datatype.Field) StructField(org.apache.carbondata.core.metadata.datatype.StructField) SchemaEvolutionEntry(org.apache.carbondata.core.metadata.schema.SchemaEvolutionEntry) StructField(org.apache.carbondata.core.metadata.datatype.StructField) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataType(org.apache.carbondata.core.metadata.datatype.DataType) TableInfo(org.apache.carbondata.core.metadata.schema.table.TableInfo) PartitionInfo(org.apache.carbondata.core.metadata.schema.PartitionInfo)

Example 2 with TableSchemaBuilder

use of org.apache.carbondata.core.metadata.schema.table.TableSchemaBuilder in project carbondata by apache.

the class CarbonWriterBuilder method buildCarbonTable.

/**
 * Build a {@link CarbonTable}
 */
private CarbonTable buildCarbonTable() {
    TableSchemaBuilder tableSchemaBuilder = TableSchema.builder();
    if (blockSize > 0) {
        tableSchemaBuilder = tableSchemaBuilder.blockSize(blockSize);
    }
    if (blockletSize > 0) {
        tableSchemaBuilder = tableSchemaBuilder.blockletSize(blockletSize);
    }
    if (pageSizeInMb > 0) {
        tableSchemaBuilder = tableSchemaBuilder.pageSizeInMb(pageSizeInMb);
    }
    tableSchemaBuilder.enableLocalDictionary(isLocalDictionaryEnabled);
    tableSchemaBuilder.localDictionaryThreshold(localDictionaryThreshold);
    List<String> sortColumnsList = new ArrayList<>();
    if (sortColumns == null) {
        // user passed size 4 but supplied only 2 fileds
        for (Field field : schema.getFields()) {
            if (null != field) {
                if (field.getDataType() == DataTypes.STRING || field.getDataType() == DataTypes.DATE || field.getDataType() == DataTypes.TIMESTAMP) {
                    sortColumnsList.add(field.getFieldName());
                }
            }
        }
        sortColumns = new String[sortColumnsList.size()];
        sortColumns = sortColumnsList.toArray(sortColumns);
    } else {
        sortColumnsList = Arrays.asList(sortColumns);
    }
    ColumnSchema[] sortColumnsSchemaList = new ColumnSchema[sortColumnsList.size()];
    List<String> invertedIdxColumnsList = new ArrayList<>();
    if (null != invertedIndexColumns) {
        invertedIdxColumnsList = Arrays.asList(invertedIndexColumns);
    }
    Field[] fields = schema.getFields();
    buildTableSchema(fields, tableSchemaBuilder, sortColumnsList, sortColumnsSchemaList, invertedIdxColumnsList);
    tableSchemaBuilder.setSortColumns(Arrays.asList(sortColumnsSchemaList));
    String tableName;
    String dbName;
    dbName = "";
    tableName = "_tempTable-" + UUID.randomUUID().toString() + "_" + timestamp;
    TableSchema schema = tableSchemaBuilder.build();
    schema.setTableName(tableName);
    CarbonTable table = CarbonTable.builder().tableName(schema.getTableName()).databaseName(dbName).tablePath(path).tableSchema(schema).isTransactionalTable(false).build();
    return table;
}
Also used : CarbonTable(org.apache.carbondata.core.metadata.schema.table.CarbonTable) Field(org.apache.carbondata.core.metadata.datatype.Field) StructField(org.apache.carbondata.core.metadata.datatype.StructField) TableSchema(org.apache.carbondata.core.metadata.schema.table.TableSchema) ArrayList(java.util.ArrayList) TableSchemaBuilder(org.apache.carbondata.core.metadata.schema.table.TableSchemaBuilder) ColumnSchema(org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema)

Aggregations

ArrayList (java.util.ArrayList)2 Field (org.apache.carbondata.core.metadata.datatype.Field)2 StructField (org.apache.carbondata.core.metadata.datatype.StructField)2 TableSchema (org.apache.carbondata.core.metadata.schema.table.TableSchema)2 TableSchemaBuilder (org.apache.carbondata.core.metadata.schema.table.TableSchemaBuilder)2 ColumnSchema (org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DataType (org.apache.carbondata.core.metadata.datatype.DataType)1 PartitionInfo (org.apache.carbondata.core.metadata.schema.PartitionInfo)1 SchemaEvolution (org.apache.carbondata.core.metadata.schema.SchemaEvolution)1 SchemaEvolutionEntry (org.apache.carbondata.core.metadata.schema.SchemaEvolutionEntry)1 CarbonTable (org.apache.carbondata.core.metadata.schema.table.CarbonTable)1 TableInfo (org.apache.carbondata.core.metadata.schema.table.TableInfo)1