in src/main/java/com/amazonaws/gdcreplication/util/GlueUtil.java [264:350]
public TableReplicationStatus createOrUpdateTable(AWSGlue glue, Table sourceTable, String targetGlueCatalogId,
boolean skipTableArchive) {
TableReplicationStatus tableStatus = new TableReplicationStatus();
tableStatus.setTableName(sourceTable.getName());
tableStatus.setDbName(sourceTable.getDatabaseName());
tableStatus.setReplicationTime(System.currentTimeMillis());
// Check if a table exist already
GetTableRequest targetTableRequest = new GetTableRequest();
targetTableRequest.setCatalogId(targetGlueCatalogId);
targetTableRequest.setDatabaseName(sourceTable.getDatabaseName());
targetTableRequest.setName(sourceTable.getName());
Table targetTable = null;
try {
GetTableResult targetTableResult = glue.getTable(targetTableRequest);
targetTable = targetTableResult.getTable();
} catch (EntityNotFoundException e) {
System.out.printf("Table '%s' not found. It will be created. \n", sourceTable.getName());
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception in getting getTable");
}
TableInput tableInput = createTableInput(sourceTable);
// If table exist - update the table with the schema in the input message.
if (Optional.ofNullable(targetTable).isPresent()) {
System.out.println("Table exist. It will be updated");
UpdateTableRequest updateTableRequest = new UpdateTableRequest();
updateTableRequest.setTableInput(tableInput);
updateTableRequest.setSkipArchive(skipTableArchive);
updateTableRequest.setDatabaseName(sourceTable.getDatabaseName());
try {
UpdateTableResult updateTableResult = glue.updateTable(updateTableRequest);
int statusCode = updateTableResult.getSdkHttpMetadata().getHttpStatusCode();
if (statusCode == 200) {
tableStatus.setUpdated(true);
tableStatus.setReplicated(true);
tableStatus.setError(false);
System.out.printf("Table '%s' updated successfully. \n", sourceTable.getName());
}
} catch (EntityNotFoundException e) {
e.printStackTrace();
System.out.printf("Exception thrown while updating table '%s'. Reason: '%s' do not exist already. \n",
sourceTable.getName(), sourceTable.getDatabaseName());
tableStatus.setReplicated(false);
tableStatus.setDbNotFoundError(true);
tableStatus.setError(true);
} catch (Exception e) {
e.printStackTrace();
System.out.printf("Exception thrown while updating table '%s'. \n", sourceTable.getName());
tableStatus.setReplicated(false);
tableStatus.setError(true);
}
}
// If the table do not exist - create a new table with the schema in the input
// message.
else {
CreateTableRequest createTableRequest = new CreateTableRequest();
createTableRequest.setCatalogId(targetGlueCatalogId);
createTableRequest.setDatabaseName(sourceTable.getDatabaseName());
createTableRequest.setTableInput(tableInput);
try {
CreateTableResult createTableResult = glue.createTable(createTableRequest);
int statusCode = createTableResult.getSdkHttpMetadata().getHttpStatusCode();
if (statusCode == 200) {
tableStatus.setCreated(true);
tableStatus.setReplicated(true);
tableStatus.setError(false);
System.out.printf("Table '%s' created successfully. \n", sourceTable.getName());
}
} catch (EntityNotFoundException e) {
e.printStackTrace();
System.out.printf("Exception thrown while creating table '%s'. Reason: '%s' do not exist already. \n.",
sourceTable.getName(), sourceTable.getDatabaseName());
tableStatus.setReplicated(false);
tableStatus.setDbNotFoundError(true);
} catch (Exception e) {
e.printStackTrace();
System.out.printf("Exception thrown while creating table '%s' \n.", sourceTable.getName());
tableStatus.setReplicated(false);
tableStatus.setError(true);
}
}
return tableStatus;
}