use of org.pentaho.di.starmodeler.DimensionType in project pentaho-kettle by pentaho.
the class JobGenerator method generateDimensionTransformation.
/**
* Generates a template
* @param databaseMeta
* @param logicalModel
* @return
*/
public TransMeta generateDimensionTransformation(DatabaseMeta databaseMeta, LogicalTable logicalTable) {
TransMeta transMeta = new TransMeta();
String tableName = ConceptUtil.getName(logicalTable, locale);
String tableDescription = ConceptUtil.getDescription(logicalTable, locale);
DimensionType dimensionType = ConceptUtil.getDimensionType(logicalTable);
transMeta.setName("Update dimension '" + tableName + "'");
transMeta.setDescription(tableDescription);
// Let's not forget to add the target database
//
transMeta.addDatabase(databaseMeta);
Point location = new Point(GRAPH_LEFT, GRAPH_TOP);
// Find all the source columns and source tables and put them into a table input step...
//
StepMeta inputStep = generateTableInputStepFromLogicalTable(logicalTable);
DatabaseMeta sourceDatabaseMeta = ((TableInputMeta) inputStep.getStepMetaInterface()).getDatabaseMeta();
if (sourceDatabaseMeta != null)
transMeta.addOrReplaceDatabase(sourceDatabaseMeta);
inputStep.setLocation(location.x, location.y);
nextLocation(location);
transMeta.addStep(inputStep);
StepMeta lastStep = inputStep;
// Generate an dimension lookup/update step for each table
//
StepMeta dimensionStep;
if (dimensionType == DimensionType.SLOWLY_CHANGING_DIMENSION) {
dimensionStep = generateDimensionLookupStepFromLogicalTable(databaseMeta, logicalTable);
} else {
dimensionStep = generateCombinationLookupStepFromLogicalTable(databaseMeta, logicalTable);
}
dimensionStep.setLocation(location.x, location.y);
nextLocation(location);
transMeta.addStep(dimensionStep);
TransHopMeta transHop = new TransHopMeta(lastStep, dimensionStep);
transMeta.addTransHop(transHop);
return transMeta;
}
use of org.pentaho.di.starmodeler.DimensionType in project pentaho-kettle by pentaho.
the class JobGenerator method generateDimensionTransformations.
/**
* This method generates a list of transformations: one for each dimension.
*
* @return the list of generated transformations
*/
public List<TransMeta> generateDimensionTransformations() throws KettleException {
DatabaseMeta databaseMeta = findTargetDatabaseMeta();
List<TransMeta> transMetas = new ArrayList<TransMeta>();
List<LogicalTable> logicalTables = getUniqueLogicalTables();
for (LogicalTable logicalTable : logicalTables) {
TableType tableType = ConceptUtil.getTableType(logicalTable);
DimensionType dimensionType = ConceptUtil.getDimensionType(logicalTable);
if (tableType == TableType.DIMENSION) {
switch(dimensionType) {
case SLOWLY_CHANGING_DIMENSION:
case JUNK_DIMENSION:
{
TransMeta transMeta = generateDimensionTransformation(databaseMeta, logicalTable);
transMetas.add(transMeta);
}
break;
case // TODO: generate a standard date transformation
DATE:
{
TransMeta transMeta = generateDateTransformation(databaseMeta, logicalTable);
transMetas.add(transMeta);
}
break;
case // TODO: generate a standard time transformation
TIME:
{
TransMeta transMeta = generateTimeTransformation(databaseMeta, logicalTable);
transMetas.add(transMeta);
}
break;
default:
break;
}
}
}
return transMetas;
}
use of org.pentaho.di.starmodeler.DimensionType in project pentaho-kettle by pentaho.
the class JobGenerator method generateSqlJob.
public JobMeta generateSqlJob() throws KettleException {
DatabaseMeta databaseMeta = findTargetDatabaseMeta();
Database db = new Database(Spoon.loggingObject, databaseMeta);
try {
db.connect();
JobMeta jobMeta = new JobMeta();
jobMeta.setName("Create tables for '" + ConceptUtil.getName(domain, locale) + "'");
jobMeta.setDescription(ConceptUtil.getDescription(domain, locale));
// Let's not forget to add the database connection
//
jobMeta.addDatabase(databaseMeta);
Point location = new Point(GRAPH_LEFT, GRAPH_TOP);
// Create a job entry
//
JobEntryCopy startEntry = JobMeta.createStartEntry();
startEntry.setLocation(location.x, location.y);
startEntry.setDrawn();
jobMeta.addJobEntry(startEntry);
JobEntryCopy lastEntry = startEntry;
nextLocation(location);
// Create one SQL entry for all the physically unique dimensions and facts
// We need to get a list of all known dimensions with physical table name.
//
List<LogicalTable> tables = getUniqueLogicalTables();
for (LogicalTable logicalTable : tables) {
String phTable = ConceptUtil.getString(logicalTable, DefaultIDs.LOGICAL_TABLE_PHYSICAL_TABLE_NAME);
String tableName = ConceptUtil.getName(logicalTable, locale);
String tableDescription = ConceptUtil.getDescription(logicalTable, locale);
TableType tableType = ConceptUtil.getTableType(logicalTable);
DimensionType dimensionType = ConceptUtil.getDimensionType(logicalTable);
boolean isFact = tableType == TableType.FACT;
boolean isDimension = tableType == TableType.DIMENSION;
boolean isJunk = isDimension && dimensionType == DimensionType.JUNK_DIMENSION;
JobEntrySQL sqlEntry = new JobEntrySQL(phTable);
sqlEntry.setDatabase(databaseMeta);
// Get the SQL for this table...
//
String schemaTable = databaseMeta.getQuotedSchemaTableCombination(null, phTable);
String phKeyField = null;
// The technical key is the first KEY field...
//
LogicalColumn keyColumn = null;
if (isDimension) {
keyColumn = ConceptUtil.findLogicalColumn(logicalTable, AttributeType.TECHNICAL_KEY);
}
if (keyColumn != null) {
phKeyField = ConceptUtil.getString(keyColumn, DefaultIDs.LOGICAL_COLUMN_PHYSICAL_COLUMN_NAME);
}
// Get all the fields for the logical table...
//
RowMetaInterface fields = getRowForLogicalTable(databaseMeta, logicalTable);
// Generate the required SQL to make this happen
//
String sql = db.getCreateTableStatement(schemaTable, fields, phKeyField, databaseMeta.supportsAutoinc() && !isFact, null, true);
//
if (keyColumn != null) {
ValueMetaInterface keyValueMeta = getValueForLogicalColumn(databaseMeta, keyColumn);
String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_" + phKeyField.toUpperCase());
String indexSql = db.getCreateIndexStatement(schemaTable, indexName, new String[] { keyValueMeta.getName() }, true, false, true, true);
sql += Const.CR + indexSql;
}
//
if (isFact) {
List<LogicalColumn> fks = ConceptUtil.findLogicalColumns(logicalTable, AttributeType.TECHNICAL_KEY);
for (LogicalColumn fk : fks) {
ValueMetaInterface keyValueMeta = getValueForLogicalColumn(databaseMeta, fk);
String phColumn = ConceptUtil.getString(fk, DefaultIDs.LOGICAL_COLUMN_PHYSICAL_COLUMN_NAME);
if (!Utils.isEmpty(phColumn)) {
String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_" + phColumn.toUpperCase());
String indexSql = db.getCreateIndexStatement(schemaTable, indexName, new String[] { keyValueMeta.getName() }, true, false, true, true);
sql += Const.CR + indexSql;
}
}
}
//
if (isDimension) {
List<LogicalColumn> naturalKeys = ConceptUtil.findLogicalColumns(logicalTable, AttributeType.NATURAL_KEY);
if (!naturalKeys.isEmpty()) {
String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_LOOKUP");
String[] fieldNames = new String[naturalKeys.size()];
for (int i = 0; i < fieldNames.length; i++) {
ValueMetaInterface keyValueMeta = getValueForLogicalColumn(databaseMeta, naturalKeys.get(i));
fieldNames[i] = keyValueMeta.getName();
}
String indexSql = db.getCreateIndexStatement(schemaTable, indexName, fieldNames, false, false, false, true);
sql += Const.CR + indexSql;
}
}
if (isJunk) {
List<LogicalColumn> attributes = ConceptUtil.findLogicalColumns(logicalTable, AttributeType.ATTRIBUTE);
if (!attributes.isEmpty()) {
String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_LOOKUP");
String[] fieldNames = new String[attributes.size()];
for (int i = 0; i < fieldNames.length; i++) {
ValueMetaInterface attrValueMeta = getValueForLogicalColumn(databaseMeta, attributes.get(i));
fieldNames[i] = attrValueMeta.getName();
}
String indexSql = db.getCreateIndexStatement(schemaTable, indexName, fieldNames, false, false, false, true);
sql += Const.CR + indexSql;
}
}
// If it's
sqlEntry.setSQL(sql);
sqlEntry.setDescription("Generated based on logical table '" + tableName + "'" + Const.CR + Const.CR + Const.NVL(tableDescription, ""));
JobEntryCopy sqlCopy = new JobEntryCopy(sqlEntry);
sqlCopy.setLocation(location.x, location.y);
sqlCopy.setDrawn();
nextLocation(location);
jobMeta.addJobEntry(sqlCopy);
// Hook up with the previous job entry too...
//
JobHopMeta jobHop = new JobHopMeta(lastEntry, sqlCopy);
jobHop.setEnabled();
jobHop.setConditional();
jobHop.setEvaluation(true);
if (lastEntry.isStart()) {
jobHop.setUnconditional();
}
jobMeta.addJobHop(jobHop);
lastEntry = sqlCopy;
}
return jobMeta;
} catch (Exception e) {
throw new KettleException("There was an error during the generation of the SQL job", e);
} finally {
if (db != null) {
db.disconnect();
}
}
}
Aggregations