Search in sources :

Example 1 with DefaultField

use of com.thinkbiganalytics.discovery.model.DefaultField in project kylo by Teradata.

the class DBSchemaParser method columnsResultSetToField.

private List<Field> columnsResultSetToField(ResultSet columns, Set<String> pkSet, String schema) throws SQLException {
    List<Field> fields = new Vector<>();
    if (columns != null) {
        while (columns.next()) {
            String cat = columns.getString("TABLE_CAT");
            if (StringUtils.isNotBlank(cat) && StringUtils.isNotBlank(schema)) {
                // this db supports Catalogs.  Ensure the cat matches the supplied schema
                if (!schema.equalsIgnoreCase(cat)) {
                    continue;
                }
            }
            DefaultField field = new DefaultField();
            field.setName(columns.getString("COLUMN_NAME"));
            Integer dataType = columns.getInt("DATA_TYPE");
            field.setNativeDataType(ParserHelper.toNativeType(dataType));
            field.setDerivedDataType(ParserHelper.sqlTypeToHiveType(dataType));
            field.setDescription(columns.getString("REMARKS"));
            String isNullableString = columns.getString("IS_NULLABLE");
            if ("NO".equals(isNullableString)) {
                field.setNullable(false);
            }
            if (pkSet.contains(field.getName())) {
                field.setPrimaryKey(true);
            }
            fields.add(field);
        }
    }
    return fields;
}
Also used : DefaultField(com.thinkbiganalytics.discovery.model.DefaultField) Field(com.thinkbiganalytics.discovery.schema.Field) DefaultField(com.thinkbiganalytics.discovery.model.DefaultField) Vector(java.util.Vector)

Example 2 with DefaultField

use of com.thinkbiganalytics.discovery.model.DefaultField in project kylo by Teradata.

the class DBSchemaParser method columnsResultSetToField.

@Nonnull
private List<Field> columnsResultSetToField(@Nullable ResultSet columns, @Nonnull Set<String> pkSet, @Nullable String schema, boolean schemaIsCat) throws SQLException {
    List<Field> fields = new ArrayList<>();
    if (columns != null) {
        while (columns.next()) {
            String cat = columns.getString(CATALOG_COLUMN);
            if (StringUtils.isNotBlank(cat) && StringUtils.isNotBlank(schema)) {
                // this db supports Catalogs.  Ensure the cat matches the supplied schema
                if (schemaIsCat && !schema.equalsIgnoreCase(cat)) {
                    continue;
                }
            }
            DefaultField field = new DefaultField();
            field.setName(columns.getString("COLUMN_NAME"));
            Integer dataType = columns.getInt("DATA_TYPE");
            field.setNativeDataType(ParserHelper.toNativeType(dataType));
            field.setDerivedDataType(ParserHelper.sqlTypeToHiveType(dataType));
            field.setDescription(columns.getString("REMARKS"));
            String isNullableString = columns.getString("IS_NULLABLE");
            if ("NO".equals(isNullableString)) {
                field.setNullable(false);
            }
            if (pkSet.contains(field.getName())) {
                field.setPrimaryKey(true);
            }
            fields.add(field);
        }
    }
    return fields;
}
Also used : DefaultField(com.thinkbiganalytics.discovery.model.DefaultField) Field(com.thinkbiganalytics.discovery.schema.Field) ArrayList(java.util.ArrayList) DefaultField(com.thinkbiganalytics.discovery.model.DefaultField) Nonnull(javax.annotation.Nonnull)

Example 3 with DefaultField

use of com.thinkbiganalytics.discovery.model.DefaultField in project kylo by Teradata.

the class TableSetupTest method test.

@Test
public void test() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    FeedMetadata feedMetadata = new FeedMetadata();
    feedMetadata.setCategory(new FeedCategory());
    feedMetadata.setTable(new TableSetup());
    feedMetadata.getTable().setTableSchema(new DefaultTableSchema());
    feedMetadata.getTable().getTableSchema().setName("test");
    DefaultField f1 = new DefaultField();
    f1.setName("field1");
    feedMetadata.getTable().getTableSchema().getFields().add(f1);
    String json = mapper.writeValueAsString(feedMetadata);
    FeedMetadata feedMetadata2 = mapper.readValue(json, FeedMetadata.class);
    assertEquals(feedMetadata2.getTable().getTableSchema().getName(), feedMetadata.getTable().getTableSchema().getName());
}
Also used : FeedCategory(com.thinkbiganalytics.feedmgr.rest.model.FeedCategory) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) DefaultTableSchema(com.thinkbiganalytics.discovery.model.DefaultTableSchema) DefaultField(com.thinkbiganalytics.discovery.model.DefaultField) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with DefaultField

use of com.thinkbiganalytics.discovery.model.DefaultField in project kylo by Teradata.

the class HiveMetastoreService method getTableSchemas.

public List<TableSchema> getTableSchemas() throws DataAccessException {
    String query = "SELECT d.NAME as \"DATABASE_NAME\", t.TBL_NAME, c.COLUMN_NAME, c.TYPE_NAME " + "FROM COLUMNS_V2 c " + "JOIN  SDS s on s.CD_ID = c.CD_ID " + "JOIN  TBLS t ON s.SD_ID = t.SD_ID " + "JOIN  DBS d on d.DB_ID = t.DB_ID " + "ORDER BY d.NAME, t.TBL_NAME";
    if (DatabaseType.POSTGRES.equals(getMetastoreDatabaseType())) {
        query = "SELECT d.\"NAME\" as \"DATABASE_NAME\", t.\"TBL_NAME\", c.\"COLUMN_NAME\",c.\"TYPE_NAME\" " + "FROM \"COLUMNS_V2\" c " + "JOIN  \"SDS\" s on s.\"CD_ID\" = c.\"CD_ID\" " + "JOIN  \"TBLS\" t ON s.\"SD_ID\" = t.\"SD_ID\" " + "JOIN  \"DBS\" d on d.\"DB_ID\" = t.\"DB_ID\" ";
    }
    final List<TableSchema> metadata = new ArrayList<>();
    final Map<String, Map<String, TableSchema>> databaseTables = new HashMap<>();
    hiveMetatoreJdbcTemplate.query(query, new RowMapper<Object>() {

        @Override
        public TableSchema mapRow(ResultSet rs, int i) throws SQLException {
            String dbName = rs.getString("DATABASE_NAME");
            String columnName = rs.getString("COLUMN_NAME");
            String tableName = rs.getString("TBL_NAME");
            String columnType = rs.getString("TYPE_NAME");
            if (!databaseTables.containsKey(dbName)) {
                databaseTables.put(dbName, new HashMap<String, TableSchema>());
            }
            Map<String, TableSchema> tables = databaseTables.get(dbName);
            if (!tables.containsKey(tableName)) {
                DefaultTableSchema schema = new DefaultTableSchema();
                schema.setName(tableName);
                schema.setSchemaName(dbName);
                schema.setFields(new ArrayList<Field>());
                tables.put(tableName, schema);
                metadata.add(schema);
            }
            TableSchema schema = tables.get(tableName);
            DefaultField field = new DefaultField();
            field.setName(columnName);
            field.setNativeDataType(columnType);
            field.setDerivedDataType(columnType);
            schema.getFields().add(field);
            return schema;
        }
    });
    return metadata;
}
Also used : TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) DefaultTableSchema(com.thinkbiganalytics.discovery.model.DefaultTableSchema) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) DefaultTableSchema(com.thinkbiganalytics.discovery.model.DefaultTableSchema) DefaultField(com.thinkbiganalytics.discovery.model.DefaultField) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with DefaultField

use of com.thinkbiganalytics.discovery.model.DefaultField in project kylo by Teradata.

the class CSVFileSchemaParser method populateSchema.

private DefaultFileSchema populateSchema(CSVParser parser) {
    DefaultFileSchema fileSchema = new DefaultFileSchema();
    int i = 0;
    ArrayList<Field> fields = new ArrayList<>();
    for (CSVRecord record : parser) {
        if (i > 9) {
            break;
        }
        int size = record.size();
        for (int j = 0; j < size; j++) {
            DefaultField field = null;
            if (i == 0) {
                field = new DefaultField();
                if (headerRow) {
                    field.setName(record.get(j));
                } else {
                    field.setName("Col_" + (j + 1));
                }
                fields.add(field);
            } else {
                try {
                    field = (DefaultField) fields.get(j);
                    field.getSampleValues().add(StringUtils.defaultString(record.get(j), ""));
                } catch (IndexOutOfBoundsException e) {
                    LOG.warn("Sample file has potential sparse column problem at row [?] field [?]", i + 1, j + 1);
                }
            }
        }
        i++;
    }
    fileSchema.setFields(fields);
    return fileSchema;
}
Also used : DefaultField(com.thinkbiganalytics.discovery.model.DefaultField) Field(com.thinkbiganalytics.discovery.schema.Field) DefaultFileSchema(com.thinkbiganalytics.discovery.model.DefaultFileSchema) ArrayList(java.util.ArrayList) CSVRecord(org.apache.commons.csv.CSVRecord) DefaultField(com.thinkbiganalytics.discovery.model.DefaultField)

Aggregations

DefaultField (com.thinkbiganalytics.discovery.model.DefaultField)9 Field (com.thinkbiganalytics.discovery.schema.Field)5 ArrayList (java.util.ArrayList)4 DefaultTableSchema (com.thinkbiganalytics.discovery.model.DefaultTableSchema)3 TableSchema (com.thinkbiganalytics.discovery.schema.TableSchema)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DefaultFileSchema (com.thinkbiganalytics.discovery.model.DefaultFileSchema)1 DefaultHiveSchema (com.thinkbiganalytics.discovery.model.DefaultHiveSchema)1 DefaultTag (com.thinkbiganalytics.discovery.model.DefaultTag)1 QueryResultColumn (com.thinkbiganalytics.discovery.schema.QueryResultColumn)1 Tag (com.thinkbiganalytics.discovery.schema.Tag)1 DomainType (com.thinkbiganalytics.feedmgr.rest.model.DomainType)1 FeedCategory (com.thinkbiganalytics.feedmgr.rest.model.FeedCategory)1 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)1 FieldStandardizationRule (com.thinkbiganalytics.policy.rest.model.FieldStandardizationRule)1 FieldValidationRule (com.thinkbiganalytics.policy.rest.model.FieldValidationRule)1 HashMap (java.util.HashMap)1