org.apache.kylin.metadata.model.DataModelDesc Java Examples

The following examples show how to use org.apache.kylin.metadata.model.DataModelDesc. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelController.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "", method = { RequestMethod.PUT }, produces = { "application/json" })
@ResponseBody
public ModelRequest updateModelDesc(@RequestBody ModelRequest modelRequest) throws JsonProcessingException {
    DataModelDesc modelDesc = deserializeDataModelDesc(modelRequest);
    if (modelDesc == null) {
        return modelRequest;
    }
    try {
        modelDesc = modelService.updateModelAndDesc(modelRequest.getProject(), modelDesc);
    } catch (AccessDeniedException accessDeniedException) {
        throw new ForbiddenException("You don't have right to update this model.");
    } catch (Exception e) {
        logger.error("Failed to deal with the request:" + e.getLocalizedMessage(), e);
        throw new InternalErrorException("Failed to deal with the request: " + e.getLocalizedMessage(), e);
    }

    if (modelDesc.getError().isEmpty()) {
        modelRequest.setSuccessful(true);
    } else {
        logger.warn("Model " + modelDesc.getName() + " fail to update because " + modelDesc.getError());
        updateRequest(modelRequest, false, omitMessage(modelDesc.getError()));
    }
    String descData = JsonUtil.writeValueAsIndentString(modelDesc);
    modelRequest.setModelDescData(descData);
    return modelRequest;
}
 
Example #2
Source File: TableSchemaUpdateChecker.java    From kylin with Apache License 2.0 6 votes vote down vote up
private List<String> checkAllColumnsInLookupTable(DataModelDesc usedModel, TableDesc newTableDesc) {
    List<String> violateColumns = Lists.newArrayList();

    Set<String> newColumns = Sets.newHashSet(newTableDesc.getColumns()).stream()
            .map(c -> c.getName().toUpperCase(Locale.ROOT)).collect(Collectors.toSet());
    for (ModelDimensionDesc dim : usedModel.getDimensions()) {
        if (dim.getTable().equalsIgnoreCase(newTableDesc.getIdentity())) {
            Set<String> oldColumns = Sets.newHashSet(dim.getColumns()).stream().map(c -> c.toUpperCase(Locale.ROOT))
                    .collect(Collectors.toSet());
            oldColumns.removeAll(newColumns);
            violateColumns.addAll(oldColumns);
        }
    }

    return violateColumns;
}
 
Example #3
Source File: CubeSegment.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
public static String makeSegmentName(TSRange tsRange, SegmentRange segRange, DataModelDesc modelDesc) {
    if (tsRange == null && segRange == null) {
        return "FULL_BUILD";
    }

    if (segRange != null) {
        return segRange.start.v + "_" + segRange.end.v;
    }

    if (!modelDesc.isStandardPartitionedDateColumn()) {
        return tsRange.start.v + "_" + tsRange.end.v;
    }

    // using time
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ROOT);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    return dateFormat.format(tsRange.start.v) + "_" + dateFormat.format(tsRange.end.v);
}
 
Example #4
Source File: TableSchemaUpdateChecker.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
public CheckResult allowReload(TableDesc newTableDesc, String prj) {
    final String fullTableName = newTableDesc.getIdentity();

    TableDesc existing = metadataManager.getTableDesc(fullTableName, prj);
    if (existing == null) {
        return CheckResult.validOnFirstLoad(fullTableName);
    }
    List<String> issues = Lists.newArrayList();

    for (DataModelDesc usedModel : findModelByTable(newTableDesc, prj)){
        checkValidationInModel(newTableDesc, issues, usedModel);
    }

    for (CubeInstance cube : findCubeByTable(newTableDesc)) {
        checkValidationInCube(newTableDesc, issues, cube);
    }

    if (issues.isEmpty()) {
        return CheckResult.validOnCompatibleSchema(fullTableName);
    }
    return CheckResult.invalidOnIncompatibleSchema(fullTableName, issues);
}
 
Example #5
Source File: ModelServiceTest.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Test
public void testRevisableModelInCaseOfDeleteMeasure() throws IOException {
    List<DataModelDesc> dataModelDescs = modelService.listAllModels("ci_left_join_model", "default", true);
    Assert.assertTrue(dataModelDescs.size() == 1);

    DataModelDesc revisableModel = dataModelDescs.get(0);

    String[] originM = revisableModel.getMetrics();
    String[] reviseM = cutItems(originM, 1);

    revisableModel.setMetrics(reviseM);

    expectedEx.expect(org.apache.kylin.rest.exception.BadRequestException.class);
    expectedEx.expectMessage(
            "Measure: TEST_KYLIN_FACT.ITEM_COUNT can't be removed, It is referred in Cubes: [ci_left_join_cube]");
    modelService.updateModelToResourceStore(revisableModel, "default");
}
 
Example #6
Source File: CubeMigrationCLI.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private static void listCubeRelatedResources(CubeInstance cube, List<String> metaResource,
        Set<String> dictAndSnapshot) throws IOException {

    CubeDesc cubeDesc = cube.getDescriptor();
    metaResource.add(cube.getResourcePath());
    metaResource.add(cubeDesc.getResourcePath());
    metaResource.add(DataModelDesc.concatResourcePath(cubeDesc.getModelName()));

    for (TableRef table : cubeDesc.getModel().getAllTables()) {
        metaResource.add(table.getTableDesc().getResourcePath());
    }

    for (CubeSegment segment : cube.getSegments()) {
        metaResource.add(segment.getStatisticsResourcePath());
        dictAndSnapshot.addAll(segment.getSnapshotPaths());
        dictAndSnapshot.addAll(segment.getDictionaryPaths());
    }
}
 
Example #7
Source File: ModelService.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
public void primaryCheck(DataModelDesc modelDesc) {
    Message msg = MsgPicker.getMsg();

    if (modelDesc == null) {
        throw new BadRequestException(msg.getINVALID_MODEL_DEFINITION());
    }

    String modelName = modelDesc.getName();

    if (StringUtils.isEmpty(modelName)) {
        logger.info("Model name should not be empty.");
        throw new BadRequestException(msg.getEMPTY_MODEL_NAME());
    }
    if (!ValidateUtil.isAlphanumericUnderscore(modelName)) {
        logger.info("Invalid model name {}, only letters, numbers and underscore supported.", modelDesc.getName());
        throw new BadRequestException(String.format(Locale.ROOT, msg.getINVALID_MODEL_NAME(), modelName));
    }
}
 
Example #8
Source File: CubeMigrationCLI.java    From kylin with Apache License 2.0 6 votes vote down vote up
private static void listCubeRelatedResources(CubeInstance cube, List<String> metaResource,
        Set<String> dictAndSnapshot) throws IOException {

    CubeDesc cubeDesc = cube.getDescriptor();
    metaResource.add(cube.getResourcePath());
    metaResource.add(cubeDesc.getResourcePath());
    metaResource.add(DataModelDesc.concatResourcePath(cubeDesc.getModelName()));

    for (TableRef table : cubeDesc.getModel().getAllTables()) {
        metaResource.add(table.getTableDesc().getResourcePath());
    }

    for (CubeSegment segment : cube.getSegments()) {
        metaResource.add(segment.getStatisticsResourcePath());
        dictAndSnapshot.addAll(segment.getSnapshotPaths());
        dictAndSnapshot.addAll(segment.getDictionaryPaths());
    }
}
 
Example #9
Source File: FactTableRealizationSetCalculator.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private String getRootFactTableForRealization(IRealization realization) {
    if (realization == null) {
        logger.warn("Cannot find realization %s", realization);
        return null;
    }
    DataModelDesc model = realization.getModel();
    if (model == null) {
        logger.warn("The model for realization %s is null", realization.getName());
        return null;
    }
    TableRef rootFactTable = model.getRootFactTable();
    if (rootFactTable == null) {
        logger.warn("The root table for model %s is null", model.getName());
        return null;
    }
    return rootFactTable.getTableIdentity();
}
 
Example #10
Source File: TableSchemaUpdateChecker.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private void checkValidationInModel(TableDesc newTableDesc, List<String> issues, DataModelDesc usedModel){
    final String fullTableName = newTableDesc.getIdentity();
    // if user reloads a fact table used by model, then all used columns must match current schema
    if (usedModel.isFactTable(fullTableName)) {
        TableDesc factTable = usedModel.findFirstTable(fullTableName).getTableDesc();
        List<String> violateColumns = checkAllColumnsInFactTable(usedModel, factTable, newTableDesc);
        if (!violateColumns.isEmpty()) {
            issues.add(format(Locale.ROOT, "Column %s used in model[%s], but changed " + "in hive",
                    violateColumns, usedModel.getName()));
        }
    }

    // if user reloads a lookup table used by cube, only append column(s) are allowed, all existing columns
    // must be the same (except compatible type changes)
    if (usedModel.isLookupTable(fullTableName)) {
        TableDesc lookupTable = usedModel.findFirstTable(fullTableName).getTableDesc();
        if (!checkAllColumnsInTableDesc(lookupTable, newTableDesc)) {
            issues.add(format(Locale.ROOT, "Table '%s' is used as Lookup Table in model[%s], but "
                            + "changed in " + "hive, only append operation are supported on hive table as lookup table",
                    lookupTable.getIdentity(), usedModel.getName()));
        }
    }
}
 
Example #11
Source File: CubeInstance.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public int getCost() {
    if (getDescriptor() == null) {
        //in case not initialized
        return 0;
    }
    int countedDimensionNum = getRowKeyColumnCount();
    int c = countedDimensionNum * COST_WEIGHT_DIMENSION + getMeasures().size() * COST_WEIGHT_MEASURE;
    DataModelDesc model = getModel();
    if (model == null) {
        //in case broken cube
        return 0;
    }
    for (JoinTableDesc join : model.getJoinTables()) {
        if (join.getJoin().isInnerJoin())
            c += CubeInstance.COST_WEIGHT_INNER_JOIN;
    }
    return c;
}
 
Example #12
Source File: ModelCreator.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
public static DataModelDesc generateKylinModel(String owner, String tableName, List<String> dimensions,
        List<String> measures, PartitionDesc partitionDesc) {
    ModelDimensionDesc modelDimensionDesc = new ModelDimensionDesc();
    modelDimensionDesc.setTable(tableName);
    modelDimensionDesc.setColumns(dimensions.toArray(new String[dimensions.size()]));

    DataModelDesc kylinModel = new DataModelDesc();
    kylinModel.setName(tableName.replace('.', '_'));
    kylinModel.setOwner(owner);
    kylinModel.setDescription("");
    kylinModel.setLastModified(0L);
    kylinModel.setRootFactTableName(tableName);
    kylinModel.setJoinTables(new JoinTableDesc[0]);
    kylinModel.setDimensions(Lists.newArrayList(modelDimensionDesc));
    kylinModel.setMetrics(measures.toArray(new String[measures.size()]));
    kylinModel.setFilterCondition("");
    kylinModel.setPartitionDesc(partitionDesc);
    kylinModel.setCapacity(DataModelDesc.RealizationCapacity.SMALL);
    kylinModel.updateRandomUuid();

    return kylinModel;
}
 
Example #13
Source File: JoinedFormatter.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private void setDateEnv(IJoinedFlatTableDesc flatDesc) {
    DataModelDesc model = flatDesc.getDataModel();
    PartitionDesc partDesc = model.getPartitionDesc();
    SegmentRange segRange = flatDesc.getSegRange();
    long startInclusive = (Long) segRange.start.v;
    long endExclusive = (Long) segRange.end.v;
    //
    String startDate = "";
    String endDate = "";
    String partitionColumnDateFormat = partDesc.getPartitionDateFormat();
    if (partDesc.getPartitionTimeColumn() == null && partDesc.getPartitionDateColumn() == null) {
        startDate = String.valueOf(startInclusive);
        endDate = String.valueOf(endExclusive);
    } else {
        startDate = DateFormat.formatToDateStr(startInclusive, partitionColumnDateFormat);
        endDate = DateFormat.formatToDateStr(endExclusive, partitionColumnDateFormat);
    }
    setKeyValue(START_DATE, startDate);
    setKeyValue(END_DATE, endDate);
}
 
Example #14
Source File: TableSchemaUpdateChecker.java    From kylin with Apache License 2.0 6 votes vote down vote up
public CheckResult allowReload(TableDesc newTableDesc, String prj) {
    final String fullTableName = newTableDesc.getIdentity();

    TableDesc existing = metadataManager.getTableDesc(fullTableName, prj);
    if (existing == null) {
        return CheckResult.validOnFirstLoad(fullTableName);
    }
    List<String> issues = Lists.newArrayList();

    for (DataModelDesc usedModel : findModelByTable(newTableDesc, prj)){
        checkValidationInModel(newTableDesc, issues, usedModel);
    }

    for (CubeInstance cube : findCubeByTable(newTableDesc)) {
        checkValidationInCube(newTableDesc, issues, cube);
    }

    if (issues.isEmpty()) {
        return CheckResult.validOnCompatibleSchema(fullTableName);
    }
    return CheckResult.invalidOnIncompatibleSchema(fullTableName, issues);
}
 
Example #15
Source File: ModelController.java    From kylin with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "", method = { RequestMethod.PUT }, produces = { "application/json" })
@ResponseBody
public ModelRequest updateModelDesc(@RequestBody ModelRequest modelRequest) throws JsonProcessingException {
    DataModelDesc modelDesc = deserializeDataModelDesc(modelRequest);
    if (modelDesc == null) {
        return modelRequest;
    }
    try {
        modelDesc = modelService.updateModelAndDesc(modelRequest.getProject(), modelDesc);
    } catch (AccessDeniedException accessDeniedException) {
        throw new ForbiddenException("You don't have right to update this model.");
    } catch (Exception e) {
        logger.error("Failed to deal with the request:" + e.getLocalizedMessage(), e);
        throw new InternalErrorException("Failed to deal with the request: " + e.getLocalizedMessage(), e);
    }

    if (modelDesc.getError().isEmpty()) {
        modelRequest.setSuccessful(true);
    } else {
        logger.warn("Model " + modelDesc.getName() + " fail to update because " + modelDesc.getError());
        updateRequest(modelRequest, false, omitMessage(modelDesc.getError()));
    }
    String descData = JsonUtil.writeValueAsIndentString(modelDesc);
    modelRequest.setModelDescData(descData);
    return modelRequest;
}
 
Example #16
Source File: ModelServiceTest.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessModelUpdate() throws IOException, JobException {
    Serializer<DataModelDesc> serializer = modelService.getDataModelManager().getDataModelSerializer();
    
    List<DataModelDesc> dataModelDescs = modelService.listAllModels("ci_inner_join_model", "default", true);
    Assert.assertTrue(dataModelDescs.size() == 1);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serializer.serialize(dataModelDescs.get(0), new DataOutputStream(baos));
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataModelDesc deserialize = serializer.deserialize(new DataInputStream(bais));

    deserialize.setOwner("somebody");
    DataModelDesc dataModelDesc = modelService.updateModelAndDesc("default", deserialize);
    Assert.assertTrue(dataModelDesc.getOwner().equals("somebody"));
}
 
Example #17
Source File: TableSchemaUpdateChecker.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private Set<ColumnDesc> findUsedColumnsInFactTable(DataModelDesc usedModel, TableDesc factTable) {
    Set<ColumnDesc> usedColumns = Sets.newHashSet();
    // column in dimension
    for (ModelDimensionDesc dim : usedModel.getDimensions()) {
        if (dim.getTable().equalsIgnoreCase(factTable.getName())) {
            for (String col : dim.getColumns()) {
                usedColumns.add(mustGetColumnDesc(factTable, col));
            }
        }
    }

    // column in measure
    for (String columnInMeasure : usedModel.getMetrics()) {
        if (factTable.getName().equalsIgnoreCase(getTableName(columnInMeasure))) {
            usedColumns.add(mustGetColumnDesc(factTable, columnInMeasure));
        }
    }

    return usedColumns;
}
 
Example #18
Source File: LookupTableToHFileMapper.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void doSetup(Context context) throws IOException {
    super.bindCurrentConfiguration(context.getConfiguration());
    cubeName = context.getConfiguration().get(BatchConstants.CFG_CUBE_NAME);
    tableName = context.getConfiguration().get(BatchConstants.CFG_TABLE_NAME);
    shardNum = Integer.parseInt(context.getConfiguration().get(BatchConstants.CFG_SHARD_NUM));
    KylinConfig config = AbstractHadoopJob.loadKylinPropsAndMetadata();

    CubeManager cubeMgr = CubeManager.getInstance(config);
    cubeDesc = cubeMgr.getCube(cubeName).getDescriptor();
    DataModelDesc modelDesc = cubeDesc.getModel();
    TableDesc tableDesc = TableMetadataManager.getInstance(KylinConfig.getInstanceFromEnv()).getTableDesc(
            tableName, cubeDesc.getProject());
    TableRef lookupTableRef = null;
    for (TableRef tableRef : modelDesc.getLookupTables()) {
        if (tableRef.getTableIdentity().equalsIgnoreCase(tableName)) {
            lookupTableRef = tableRef;
            break;
        }
    }
    JoinDesc joinDesc = modelDesc.getJoinByPKSide(lookupTableRef);
    TblColRef[] keyColRefs = joinDesc.getPrimaryKeyColumns();
    String[] keyColumns = new String[keyColRefs.length];
    for (int i = 0; i < keyColRefs.length; i++) {
        keyColumns[i] = keyColRefs[i].getName();
    }
    encoder = new HBaseLookupRowEncoder(tableDesc, keyColumns, shardNum);
    Configuration conf = context.getConfiguration();
    lookupTableInputFormat = MRUtil.getTableInputFormat(tableDesc, conf.get(BatchConstants.ARG_CUBING_JOB_ID));
}
 
Example #19
Source File: DataGenTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Test
public void testCIConfigured() throws IOException {
    DataModelDesc model = getModel("ci_inner_join_model");
    ModelDataGenerator gen = new ModelDataGenerator(model, 100);
    gen.outprint = false;
    
    gen.generate();
}
 
Example #20
Source File: ModelDataGenerator.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    String modelName = args[0];
    int nRows = Integer.parseInt(args[1]);
    String outputDir = args.length > 2 ? args[2] : null;

    KylinConfig conf = KylinConfig.getInstanceFromEnv();
    DataModelDesc model = DataModelManager.getInstance(conf).getDataModelDesc(modelName);
    ResourceStore store = outputDir == null ? ResourceStore.getStore(conf)
            : ResourceStore.getStore(mockup(outputDir));

    ModelDataGenerator gen = new ModelDataGenerator(model, nRows, store);
    gen.generate();
}
 
Example #21
Source File: ModelService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public void dropModel(DataModelDesc desc) throws IOException {
    aclEvaluate.checkProjectWritePermission(desc.getProjectInstance().getName());
    Message msg = MsgPicker.getMsg();
    //check cube desc exist
    List<CubeDesc> cubeDescs = getCubeDescManager().listAllDesc();
    for (CubeDesc cubeDesc : cubeDescs) {
        if (cubeDesc.getModelName().equals(desc.getName())) {
            throw new BadRequestException(
                    String.format(Locale.ROOT, msg.getDROP_REFERENCED_MODEL(), cubeDesc.getName()));
        }
    }

    getDataModelManager().dropModel(desc);
}
 
Example #22
Source File: CubeService.java    From kylin with Apache License 2.0 5 votes vote down vote up
public void validateCubeDesc(CubeDesc desc, boolean isDraft) {
    Message msg = MsgPicker.getMsg();

    if (desc == null) {
        throw new BadRequestException(msg.getINVALID_CUBE_DEFINITION());
    }

    String cubeName = desc.getName();
    if (StringUtils.isEmpty(cubeName)) {
        logger.info("Cube name should not be empty.");
        throw new BadRequestException(msg.getEMPTY_CUBE_NAME());
    }
    if (!ValidateUtil.isAlphanumericUnderscore(cubeName)) {
        logger.info("Invalid Cube name {}, only letters, numbers and underscore supported.", cubeName);
        throw new BadRequestException(String.format(Locale.ROOT, msg.getINVALID_CUBE_NAME(), cubeName));
    }

    if (!isDraft) {
        DataModelDesc modelDesc = modelService.getDataModelManager().getDataModelDesc(desc.getModelName());
        if (modelDesc == null) {
            throw new BadRequestException(
                    String.format(Locale.ROOT, msg.getMODEL_NOT_FOUND(), desc.getModelName()));
        }

        if (modelDesc.isDraft()) {
            logger.info("Cannot use draft model.");
            throw new BadRequestException(
                    String.format(Locale.ROOT, msg.getUSE_DRAFT_MODEL(), desc.getModelName()));
        }
    }
}
 
Example #23
Source File: LookupTableToHFileMapper.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
protected void doSetup(Context context) throws IOException {
    super.bindCurrentConfiguration(context.getConfiguration());
    cubeName = context.getConfiguration().get(BatchConstants.CFG_CUBE_NAME);
    tableName = context.getConfiguration().get(BatchConstants.CFG_TABLE_NAME);
    shardNum = Integer.parseInt(context.getConfiguration().get(BatchConstants.CFG_SHARD_NUM));
    KylinConfig config = AbstractHadoopJob.loadKylinPropsAndMetadata();

    CubeManager cubeMgr = CubeManager.getInstance(config);
    cubeDesc = cubeMgr.getCube(cubeName).getDescriptor();
    DataModelDesc modelDesc = cubeDesc.getModel();
    TableDesc tableDesc = TableMetadataManager.getInstance(KylinConfig.getInstanceFromEnv()).getTableDesc(
            tableName, cubeDesc.getProject());
    TableRef lookupTableRef = null;
    for (TableRef tableRef : modelDesc.getLookupTables()) {
        if (tableRef.getTableIdentity().equalsIgnoreCase(tableName)) {
            lookupTableRef = tableRef;
            break;
        }
    }
    JoinDesc joinDesc = modelDesc.getJoinByPKSide(lookupTableRef);
    TblColRef[] keyColRefs = joinDesc.getPrimaryKeyColumns();
    String[] keyColumns = new String[keyColRefs.length];
    for (int i = 0; i < keyColRefs.length; i++) {
        keyColumns[i] = keyColRefs[i].getName();
    }
    encoder = new HBaseLookupRowEncoder(tableDesc, keyColumns, shardNum);
    Configuration conf = context.getConfiguration();
    lookupTableInputFormat = MRUtil.getTableInputFormat(tableDesc, conf.get(BatchConstants.ARG_CUBING_JOB_ID));
}
 
Example #24
Source File: DimensionDesc.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public void init(CubeDesc cubeDesc) {
    DataModelDesc model = cubeDesc.getModel();

    if (name != null)
        name = name.toUpperCase(Locale.ROOT);

    tableRef = model.findTable(table);
    table = tableRef.getAlias();

    join = null;
    for (JoinTableDesc joinTable : model.getJoinTables()) {
        if (joinTable.getTableRef().equals(this.tableRef)) {
            join = joinTable.getJoin();
            break;
        }
    }

    if (column != null && !"{FK}".equals(column)) {
        column = model.findColumn(table, column).getName();
    }
    if (derived != null && derived.length == 0) {
        derived = null;
    }
    if (derived != null) {
        for (int i = 0; i < derived.length; i++) {
            derived[i] = model.findColumn(table, derived[i]).getName();
        }
    }
    if (derived != null && join == null) {
        throw new IllegalStateException(
                "Derived can only be defined on lookup table, cube " + cubeDesc + ", " + this);
    }

}
 
Example #25
Source File: ModelService.java    From kylin with Apache License 2.0 5 votes vote down vote up
public void dropModel(DataModelDesc desc) throws IOException {
    aclEvaluate.checkProjectWritePermission(desc.getProjectInstance().getName());
    Message msg = MsgPicker.getMsg();
    //check cube desc exist
    List<CubeDesc> cubeDescs = getCubeDescManager().listAllDesc();
    for (CubeDesc cubeDesc : cubeDescs) {
        if (cubeDesc.getModelName().equals(desc.getName())) {
            throw new BadRequestException(
                    String.format(Locale.ROOT, msg.getDROP_REFERENCED_MODEL(), cubeDesc.getName()));
        }
    }

    getDataModelManager().dropModel(desc);
}
 
Example #26
Source File: CubeMetaExtractor.java    From kylin with Apache License 2.0 5 votes vote down vote up
private void addTables(DataModelDesc modelDesc) {
    if (modelDesc != null) {
        //fixme should get all tbls in prj not only in cubes when back up by prj.
        for (TableRef tableRef : modelDesc.getAllTables()) {
            addRequired(tableRef.getTableDesc().getResourcePath());
            addOptional(TableMetadataManager.getInstance(KylinConfig.getInstanceFromEnv())
                    .getTableExt(tableRef.getTableDesc())
                    .getResourcePath());
        }
        addRequired(DataModelDesc.concatResourcePath(modelDesc.getName()));
    }
}
 
Example #27
Source File: CsvSourceTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFlatTable() throws IOException {
    System.out.println(getTestConfig().getMetadataUrl());
    CubeManager cubeMgr = CubeManager.getInstance(getTestConfig());
    CubeInstance cube = cubeMgr.getCube(CUBE_NAME);
    cleanupSegments(CUBE_NAME);
    DataModelDesc model = cube.getModel();
    CubeSegment segment = cubeMgr.appendSegment(cube, new SegmentRange.TSRange(dateToLong("2010-01-01"), dateToLong("2013-01-01")));
    Dataset<Row> ds = initFlatTable(segment);
    ds.show(10);
    StructType schema = ds.schema();

    SegmentInfo segmentInfo = MetadataConverter.getSegmentInfo(segment.getCubeInstance(), segment.getUuid(),
            segment.getName(), segment.getStorageLocationIdentifier());
    scala.collection.immutable.Map<String, String> map = BuildUtils.getColumnIndexMap(segmentInfo);
    for (StructField field : schema.fields()) {
        Assert.assertNotNull(model.findColumn(map.apply(field.name())));
    }

    for (LayoutEntity layoutEntity : MetadataConverter.extractEntityList2JavaList(cube)) {
        Set<Integer> dims = layoutEntity.getOrderedDimensions().keySet();
        Column[] modelCols = new Column[dims.size()];
        int index = 0;
        for (int id : dims) {
            modelCols[index] = new Column(String.valueOf(id));
            index++;
        }
        ds.select(modelCols).show(10);
    }
}
 
Example #28
Source File: ModelService.java    From kylin with Apache License 2.0 5 votes vote down vote up
public void checkModelCompatibility(String project, DataModelDesc dataModalDesc) {
    ProjectInstance prjInstance = getProjectManager().getProject(project);
    if (prjInstance == null) {
        throw new BadRequestException("Project " + project + " does not exist");
    }
    if (!prjInstance.getConfig().isModelSchemaUpdaterCheckerEnabled()) {
        logger.info("Skip the check for model schema update");
        return;
    }
    ModelSchemaUpdateChecker checker = new ModelSchemaUpdateChecker(getTableManager(), getCubeManager(),
            getDataModelManager());
    ModelSchemaUpdateChecker.CheckResult result = checker.allowEdit(dataModalDesc, project);
    result.raiseExceptionWhenInvalid();
}
 
Example #29
Source File: JoinedFlatTable.java    From Kylin with Apache License 2.0 5 votes vote down vote up
private static void appendJoinStatement(IJoinedFlatTableDesc intermediateTableDesc, StringBuilder sql, Map<String, String> tableAliasMap) {
    Set<String> dimTableCache = new HashSet<String>();

    DataModelDesc dataModelDesc = intermediateTableDesc.getDataModel();
    String factTableName = dataModelDesc.getFactTable();
    String factTableAlias = tableAliasMap.get(factTableName);
    sql.append("FROM " + factTableName + " as " + factTableAlias + " \n");

    for (LookupDesc lookupDesc : dataModelDesc.getLookups()) {
        JoinDesc join = lookupDesc.getJoin();
        if (join != null && join.getType().equals("") == false) {
            String joinType = join.getType().toUpperCase();
            String dimTableName = lookupDesc.getTable();
            if (!dimTableCache.contains(dimTableName)) {
                TblColRef[] pk = join.getPrimaryKeyColumns();
                TblColRef[] fk = join.getForeignKeyColumns();
                if (pk.length != fk.length) {
                    throw new RuntimeException("Invalid join condition of lookup table:" + lookupDesc);
                }
                sql.append(joinType + " JOIN " + dimTableName + " as " + tableAliasMap.get(dimTableName) + "\n");
                sql.append("ON ");
                for (int i = 0; i < pk.length; i++) {
                    if (i > 0) {
                        sql.append(" AND ");
                    }
                    sql.append(factTableAlias + "." + fk[i].getName() + " = " + tableAliasMap.get(dimTableName) + "." + pk[i].getName());
                }
                sql.append("\n");

                dimTableCache.add(dimTableName);
            }
        }
    }
}
 
Example #30
Source File: DimensionRangeInfo.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static Map<String, DimensionRangeInfo> mergeRangeMap(DataModelDesc model, Map<String, DimensionRangeInfo> m1,
        Map<String, DimensionRangeInfo> m2) {

    if (!m1.keySet().equals(m2.keySet())) {
        logger.warn("Merging incompatible maps of DimensionRangeInfo, keys in m1 " + m1.keySet() + ", keys in m2 "
                + m2.keySet());
    }

    Map<String, DimensionRangeInfo> result = Maps.newHashMap();
    
    for (String colId : m1.keySet()) {
        if (!m2.containsKey(colId))
            continue;

        DimensionRangeInfo r1 = m1.get(colId);
        DimensionRangeInfo r2 = m2.get(colId);
        
        DimensionRangeInfo newR;
        if (r1.getMin() == null && r1.getMax() == null) {
            newR = r2; // when r1 is all null or has 0 records
        } else if (r2.getMin() == null && r2.getMax() == null) {
            newR = r1; // when r2 is all null or has 0 records
        } else {
            DataTypeOrder order = model.findColumn(colId).getType().getOrder();
            String newMin = order.min(r1.getMin(), r2.getMin());
            String newMax = order.max(r1.getMax(), r2.getMax());
            newR = new DimensionRangeInfo(newMin, newMax);
        }
        
        result.put(colId, newR);
    }
    
    return result;
}