1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
| package com.dtyunxi.dacat.metadata.catalog;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Assert; import com.dtyunxi.dacat.core.config.FlinkProperties; import com.dtyunxi.dacat.core.constant.TableTypeEnum; import com.dtyunxi.dacat.core.dto.metadata.*; import com.dtyunxi.dacat.core.exception.BusinessException; import com.dtyunxi.dacat.metadata.service.DatabaseServiceImpl; import com.dtyunxi.dacat.metadata.service.FunctionServiceImpl; import com.dtyunxi.dacat.metadata.service.TableServiceImpl; import com.dtyunxi.dacat.metadata.types.DataTypeMapper; import org.apache.flink.table.api.Schema; import org.apache.flink.table.catalog.*; import org.apache.flink.table.catalog.exceptions.*; import org.apache.flink.table.catalog.stats.CatalogColumnStatistics; import org.apache.flink.table.catalog.stats.CatalogTableStatistics; import org.apache.flink.table.expressions.Expression; import org.apache.flink.table.expressions.SqlCallExpression; import org.apache.flink.table.types.AbstractDataType; import org.apache.flink.table.types.DataType; import org.springframework.stereotype.Component;
import java.util.*;
import static com.dtyunxi.dacat.core.constant.CommonConstant.DATASOURCE_ID_FLINK; import static com.dtyunxi.dacat.core.exception.ExceptionCodeEnum.*;
@Component public class WritableJdbcCatalog extends AbstractCatalog {
private final DatabaseServiceImpl databaseServiceImpl; private final TableServiceImpl tableServiceImpl; private final FunctionServiceImpl functionServiceImpl;
public WritableJdbcCatalog(FlinkProperties flinkProperties, DatabaseServiceImpl databaseServiceImpl, TableServiceImpl tableServiceImpl, FunctionServiceImpl functionServiceImpl) { super(flinkProperties.getCatalogName(), flinkProperties.getDefaultDatabase()); this.databaseServiceImpl = databaseServiceImpl; this.tableServiceImpl = tableServiceImpl; this.createDefaultDatabase(flinkProperties.getDefaultDatabase()); this.functionServiceImpl = functionServiceImpl; }
private void createDefaultDatabase(String databaseName) { DatabaseDto databaseDto = new DatabaseDto(); databaseDto.setDatabaseName(databaseName); databaseServiceImpl.createDatabase(databaseDto, true); }
@Override public void open() throws CatalogException { }
@Override public void close() throws CatalogException { }
@Override public List<String> listDatabases() throws CatalogException {
return databaseServiceImpl.listDatabaseNames(); }
@Override public CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException, CatalogException {
Assert.notBlank(databaseName, DATABASE_NAME_REQUIRED.getMsg());
DatabaseDto database = databaseServiceImpl.getDatabase(databaseName); if (database != null) { return new CatalogDatabaseImpl(database.getProperties(), database.getComment()); } else { throw new DatabaseNotExistException(getName(), databaseName); } }
@Override public boolean databaseExists(String databaseName) throws CatalogException { Assert.notBlank(databaseName, DATABASE_NAME_REQUIRED.getMsg()); return databaseServiceImpl.count(databaseName) > 0; }
@Override public void createDatabase(String databaseName, CatalogDatabase database, boolean ignoreIfExists) throws DatabaseAlreadyExistException, CatalogException {
Assert.notBlank(databaseName, DATABASE_NAME_REQUIRED.getMsg()); Assert.notNull(database, "Database must not be null.");
try {
DatabaseDto databaseReqDto = buildDatabaseFromCatalogDatabase(databaseName, database); databaseServiceImpl.createDatabase(databaseReqDto); } catch (BusinessException e) { if (e.getCode().equals(DATABASE_EXISTS.getCode())) { if (!ignoreIfExists) { throw new DatabaseAlreadyExistException(getName(), databaseName); } } else { throw new CatalogException(String.format("Failed to create database %s", databaseName), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to create database %s", databaseName), e); }
}
private DatabaseDto buildDatabaseFromCatalogDatabase(String databaseName, CatalogDatabase database) { DatabaseDto databaseReqDto = new DatabaseDto(); databaseReqDto.setProperties(database.getProperties()); databaseReqDto.setDatabaseName(databaseName); databaseReqDto.setComment(database.getComment()); return databaseReqDto; }
@Override public void dropDatabase(String databaseName, boolean ignoreIfNotExists, boolean cascade) throws DatabaseNotExistException, DatabaseNotEmptyException, CatalogException {
Assert.notBlank(databaseName, DATABASE_NAME_REQUIRED.getMsg()); try {
databaseServiceImpl.deleteDatabase(databaseName, cascade); } catch (BusinessException e) { if (e.getCode().equals(DATABASE_NOT_EXISTS.getCode())) { if (!ignoreIfNotExists) { throw new DatabaseNotExistException(getName(), databaseName); } } else if (e.getCode().equals(DATABASE_HAS_TABLE.getCode())) { throw new DatabaseNotEmptyException(getName(), databaseName); } else { throw new CatalogException(String.format("Failed to drop database %s", databaseName), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to drop database %s", databaseName), e); }
}
@Override public void alterDatabase(String databaseName, CatalogDatabase newDatabase, boolean ignoreIfNotExists) throws DatabaseNotExistException, CatalogException {
Assert.notBlank(databaseName, DATABASE_NAME_REQUIRED.getMsg());
DatabaseDto databaseDto = buildDatabaseFromCatalogDatabase(databaseName, newDatabase); try { databaseServiceImpl.updateDatabase(databaseDto); } catch (BusinessException e) { if (e.getCode().equals(DATABASE_NOT_EXISTS.getCode())) { if (!ignoreIfNotExists) { throw new DatabaseNotExistException(getName(), databaseName); } } else { throw new CatalogException(String.format("Failed to alter database %s", databaseName), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to alter database %s", databaseName), e); }
}
@Override public List<String> listTables(String databaseName) throws DatabaseNotExistException, CatalogException {
Assert.notBlank(databaseName, DATABASE_NAME_REQUIRED.getMsg()); if (!databaseExists(databaseName)) { throw new DatabaseNotExistException(getName(), databaseName); } return tableServiceImpl.listTableNames(databaseName); }
@Override public List<String> listViews(String databaseName) throws DatabaseNotExistException, CatalogException {
return Collections.emptyList(); }
@Override public CatalogBaseTable getTable(ObjectPath tablePath) throws TableNotExistException, CatalogException {
TableDto tableDto = tableServiceImpl.getTable( tablePath.getDatabaseName(), tablePath.getObjectName()); if (tableDto == null) { throw new TableNotExistException(getName(), tablePath); }
List<String> primaryKeys = new ArrayList<>(tableDto.getColumns().size()); Schema.Builder schemaBuilder = Schema.newBuilder(); for (ColumnDto column : tableDto.getColumns()) { AbstractDataType<DataType> dataType = DataTypeMapper.mappingTypes(column); schemaBuilder.column(column.getColumnName(), dataType); if (Boolean.TRUE.equals(column.getPrimaryKey())) { primaryKeys.add(column.getColumnName()); } } schemaBuilder.primaryKey(primaryKeys); if (CollUtil.isNotEmpty(tableDto.getWatermarks())) { for (WatermarkDto watermark : tableDto.getWatermarks()) { schemaBuilder.watermark(watermark.getColumnName(), watermark.getExpression()); } } Schema schema = schemaBuilder.build(); return CatalogTable.of(schema, tableDto.getComment(), Collections.emptyList(), tableDto.getProperties()); }
@Override public boolean tableExists(ObjectPath tablePath) throws CatalogException { return tableServiceImpl.exists(tablePath.getDatabaseName(), tablePath.getObjectName()); }
@Override public void dropTable(ObjectPath tablePath, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException { Assert.notNull(tablePath, TABLE_PATH_REQUIRED.getMsg()); try {
tableServiceImpl.deleteTable(tablePath.getDatabaseName(), tablePath.getObjectName()); } catch (BusinessException e) { if (e.getCode().equals(TABLE_NOT_EXISTS.getCode())) { if (!ignoreIfNotExists) { throw new TableNotExistException(getName(), tablePath); } } else { throw new CatalogException(String.format("Failed to drop table %s", tablePath.getFullName()), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to drop table %s", tablePath.getFullName()), e); } }
@Override public void renameTable(ObjectPath tablePath, String newTableName, boolean ignoreIfNotExists) throws TableNotExistException, TableAlreadyExistException, CatalogException {
Assert.notNull(tablePath, TABLE_PATH_REQUIRED.getMsg()); Assert.notBlank(newTableName, TABLE_NAME_REQUIRED.getMsg()); try {
tableServiceImpl.renameTable(tablePath.getDatabaseName(), tablePath.getObjectName(), newTableName); } catch (BusinessException e) { if (e.getCode().equals(TABLE_NOT_EXISTS.getCode())) { if (!ignoreIfNotExists) { throw new TableNotExistException(getName(), tablePath); } } else if (e.getCode().equals(TABLE_NAME_DUPLICATED.getCode())) { throw new TableAlreadyExistException(getName(), tablePath); } else { throw new CatalogException(String.format("Failed to rename table %s", tablePath.getFullName()), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to rename table %s", tablePath.getFullName()), e); } }
@Override public void createTable(ObjectPath tablePath, CatalogBaseTable table, boolean ignoreIfExists) throws TableAlreadyExistException, DatabaseNotExistException, CatalogException {
Assert.notNull(tablePath, TABLE_PATH_REQUIRED.getMsg());
TableDto tableDto = buildTableFromCatalogTable(tablePath, table); try { tableServiceImpl.createTable(tableDto, ignoreIfExists); } catch (BusinessException e) { if (e.getCode().equals(DATABASE_NOT_EXISTS.getCode())) { throw new DatabaseNotExistException(getName(), tablePath.getDatabaseName()); } else if (e.getCode().equals(TABLE_NAME_DUPLICATED.getCode())) { throw new TableAlreadyExistException(getName(), tablePath); } else { throw new CatalogException(String.format("Failed to create table %s", tablePath.getFullName()), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to create table %s", tablePath.getFullName()), e); } }
private TableDto buildTableFromCatalogTable(ObjectPath tablePath, CatalogBaseTable catalogBaseTable) {
TableDto tableDto = new TableDto(); tableDto.setDatabaseName(tablePath.getDatabaseName()); tableDto.setTableName(tablePath.getObjectName()); tableDto.setComment(catalogBaseTable.getComment()); tableDto.setProperties(catalogBaseTable.getOptions());
Schema schema = catalogBaseTable.getUnresolvedSchema(); Set<String> primaryKeys = schema.getPrimaryKey() .map(e -> new HashSet<>(e.getColumnNames())) .orElse(new HashSet<>()); List<ColumnDto> columns = new ArrayList<>(schema.getColumns().size()); List<WatermarkDto> watermarks = new ArrayList<>(schema.getWatermarkSpecs().size()); for (Schema.UnresolvedColumn unresolvedColumn : schema.getColumns()) {
if (unresolvedColumn instanceof Schema.UnresolvedPhysicalColumn) { AbstractDataType<?> dataType = ((Schema.UnresolvedPhysicalColumn) unresolvedColumn).getDataType(); ColumnDto columnDto = DataTypeMapper.mappingColumn(dataType); columnDto.setColumnName(unresolvedColumn.getName()); columnDto.setComment(unresolvedColumn.getComment().orElse(null)); columnDto.setPrimaryKey(primaryKeys.contains(unresolvedColumn.getName())); columns.add(columnDto); } else { throw new CatalogException(String.format("Unsupported column type: %s", unresolvedColumn.getName())); }
} for (Schema.UnresolvedWatermarkSpec watermarkSpec : schema.getWatermarkSpecs()) {
WatermarkDto watermarkDto = new WatermarkDto(); watermarkDto.setColumnName(watermarkSpec.getColumnName());
Expression watermarkExpression = watermarkSpec.getWatermarkExpression(); if (!(watermarkExpression instanceof SqlCallExpression)) { throw new CatalogException(String.format("Unsupported watermark type: %s", watermarkExpression.getClass())); } watermarkDto.setExpression(((SqlCallExpression) watermarkExpression).getSqlExpression()); watermarks.add(watermarkDto); } tableDto.setColumns(columns); tableDto.setWatermarks(watermarks);
return tableDto; }
@Override public void alterTable(ObjectPath tablePath, CatalogBaseTable newTable, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException {
TableDto tableDto = buildTableFromCatalogTable(tablePath, newTable); try {
tableServiceImpl.updateTable(tableDto); } catch (BusinessException e) { if (e.getCode().equals(TABLE_NOT_EXISTS.getCode())) { throw new TableNotExistException(getName(), tablePath); } else { throw new CatalogException(String.format("Failed to alter table: %s", tablePath.getFullName()), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to alter table: %s", tablePath.getFullName()), e); } }
@Override public List<String> listFunctions(String databaseName) throws DatabaseNotExistException, CatalogException { Assert.notBlank(databaseName, DATABASE_NAME_REQUIRED.getMsg()); if (!databaseExists(databaseName)) { throw new DatabaseNotExistException(getName(), databaseName); } return functionServiceImpl.listFunctionNames(databaseName); }
@Override public CatalogFunction getFunction(ObjectPath functionPath) throws FunctionNotExistException, CatalogException { FunctionDto functionDto = functionServiceImpl.getFunction(functionPath.getDatabaseName(), functionPath.getObjectName()); if (functionDto == null) { throw new FunctionNotExistException(getName(), functionPath); }
return new DacatCatalogFunctionImpl(functionDto); }
@Override public boolean functionExists(ObjectPath functionPath) throws CatalogException { return functionServiceImpl.exists(functionPath.getDatabaseName(), functionPath.getObjectName()); }
@Override public void createFunction(ObjectPath functionPath, CatalogFunction function, boolean ignoreIfExists) throws FunctionAlreadyExistException, DatabaseNotExistException, CatalogException { Assert.notNull(functionPath, FUNCTION_PATH_REQUIRED.getMsg());
FunctionDto functionDto = buildFunctionDto(functionPath, function); try { functionServiceImpl.createFunction(functionDto, ignoreIfExists); } catch (BusinessException e) { if (e.getCode().equals(DATABASE_NOT_EXISTS.getCode())) { throw new DatabaseNotExistException(getName(), functionPath.getDatabaseName()); } else if (e.getCode().equals(FUNCTION_NAME_DUPLICATED.getCode())) { throw new FunctionAlreadyExistException(getName(), functionPath); } else { throw new CatalogException(String.format("Failed to create function %s", functionPath.getFullName()), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to create function %s", functionPath.getFullName()), e); } }
private FunctionDto buildFunctionDto(ObjectPath functionPath, CatalogFunction function) {
FunctionDto functionDto = new FunctionDto(); functionDto.setDatabaseName(functionPath.getDatabaseName()); functionDto.setFunctionName(functionPath.getObjectName()); functionDto.setDescription(function.getDescription().orElse(null)); functionDto.setClassName(function.getClassName()); functionDto.setFunctionLanguage(function.getFunctionLanguage().name()); functionDto.setIsGeneric(function.isGeneric()); functionDto.setDetailDescription(function.getDetailedDescription().orElse(null)); return functionDto;
}
@Override public void alterFunction(ObjectPath functionPath, CatalogFunction newFunction, boolean ignoreIfNotExists) throws FunctionNotExistException, CatalogException {
FunctionDto functionDto = buildFunctionDto(functionPath, newFunction); try { functionServiceImpl.updateFunction(functionDto, ignoreIfNotExists); } catch (BusinessException e) { if (e.getCode().equals(FUNCTION_NOT_EXISTS.getCode())) { if (!ignoreIfNotExists) { throw new FunctionNotExistException(getName(), functionPath); } } else { throw new CatalogException(String.format("Failed to alter function %s", functionPath.getFullName()), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to alter function %s", functionPath.getFullName()), e); }
}
@Override public void dropFunction(ObjectPath functionPath, boolean ignoreIfNotExists) throws FunctionNotExistException, CatalogException { Assert.notNull(functionPath, FUNCTION_PATH_REQUIRED.getMsg()); try {
functionServiceImpl.deleteFunction(functionPath.getDatabaseName(), functionPath.getObjectName()); } catch (BusinessException e) { if (e.getCode().equals(FUNCTION_NOT_EXISTS.getCode())) { if (!ignoreIfNotExists) { throw new FunctionNotExistException(getName(), functionPath); } } else { throw new CatalogException(String.format("Failed to drop function %s", functionPath.getFullName()), e); } } catch (Exception e) { throw new CatalogException(String.format("Failed to drop function %s", functionPath.getFullName()), e); } }
@Override public List<CatalogPartitionSpec> listPartitions(ObjectPath tablePath) throws TableNotExistException, TableNotPartitionedException, CatalogException { throw new UnsupportedOperationException(); }
@Override public List<CatalogPartitionSpec> listPartitions(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws TableNotExistException, TableNotPartitionedException, PartitionSpecInvalidException, CatalogException { throw new UnsupportedOperationException(); }
@Override public List<CatalogPartitionSpec> listPartitionsByFilter(ObjectPath tablePath, List<Expression> filters) throws TableNotExistException, TableNotPartitionedException, CatalogException { throw new UnsupportedOperationException(); }
@Override public CatalogPartition getPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws PartitionNotExistException, CatalogException { throw new UnsupportedOperationException(); }
@Override public boolean partitionExists(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws CatalogException { throw new UnsupportedOperationException(); }
@Override public void createPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogPartition partition, boolean ignoreIfExists) throws TableNotExistException, TableNotPartitionedException, PartitionSpecInvalidException, PartitionAlreadyExistsException, CatalogException { throw new UnsupportedOperationException(); }
@Override public void dropPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException { throw new UnsupportedOperationException(); }
@Override public void alterPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogPartition newPartition, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException { throw new UnsupportedOperationException(); } @Override public CatalogTableStatistics getTableStatistics(ObjectPath tablePath) throws TableNotExistException, CatalogException { return null; }
@Override public CatalogColumnStatistics getTableColumnStatistics(ObjectPath tablePath) throws TableNotExistException, CatalogException { return null; }
@Override public CatalogTableStatistics getPartitionStatistics(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws PartitionNotExistException, CatalogException { return null; }
@Override public CatalogColumnStatistics getPartitionColumnStatistics(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws PartitionNotExistException, CatalogException { return null; }
@Override public void alterTableStatistics(ObjectPath tablePath, CatalogTableStatistics tableStatistics, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException { throw new UnsupportedOperationException(); }
@Override public void alterTableColumnStatistics(ObjectPath tablePath, CatalogColumnStatistics columnStatistics, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException, TablePartitionedException { throw new UnsupportedOperationException(); }
@Override public void alterPartitionStatistics(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogTableStatistics partitionStatistics, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException { throw new UnsupportedOperationException(); }
@Override public void alterPartitionColumnStatistics(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogColumnStatistics columnStatistics, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException { throw new UnsupportedOperationException(); } }
|