Search in sources :

Example 1 with SchemaType

use of org.apache.phoenix.mapreduce.util.PhoenixConfigurationUtil.SchemaType in project phoenix by apache.

the class PhoenixPigSchemaUtil method getResourceSchema.

public static ResourceSchema getResourceSchema(final Configuration configuration, Dependencies dependencies) throws IOException {
    final ResourceSchema schema = new ResourceSchema();
    try {
        List<ColumnInfo> columns = null;
        final SchemaType schemaType = PhoenixConfigurationUtil.getSchemaType(configuration);
        if (SchemaType.QUERY.equals(schemaType)) {
            final String sqlQuery = PhoenixConfigurationUtil.getSelectStatement(configuration);
            Preconditions.checkNotNull(sqlQuery, "No Sql Query exists within the configuration");
            final SqlQueryToColumnInfoFunction function = new SqlQueryToColumnInfoFunction(configuration);
            columns = function.apply(sqlQuery);
        } else {
            columns = dependencies.getSelectColumnMetadataList(configuration);
        }
        ResourceFieldSchema[] fields = new ResourceFieldSchema[columns.size()];
        int i = 0;
        for (ColumnInfo cinfo : columns) {
            int sqlType = cinfo.getSqlType();
            PDataType phoenixDataType = PDataType.fromTypeId(sqlType);
            byte pigType = TypeUtil.getPigDataTypeForPhoenixType(phoenixDataType);
            ResourceFieldSchema field = new ResourceFieldSchema();
            field.setType(pigType).setName(cinfo.getDisplayName());
            fields[i++] = field;
        }
        schema.setFields(fields);
    } catch (SQLException sqle) {
        LOG.error(String.format("Error: SQLException [%s] ", sqle.getMessage()));
        throw new IOException(sqle);
    }
    return schema;
}
Also used : ResourceSchema(org.apache.pig.ResourceSchema) SQLException(java.sql.SQLException) ColumnInfo(org.apache.phoenix.util.ColumnInfo) IOException(java.io.IOException) SchemaType(org.apache.phoenix.mapreduce.util.PhoenixConfigurationUtil.SchemaType) PDataType(org.apache.phoenix.schema.types.PDataType) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema)

Aggregations

IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 SchemaType (org.apache.phoenix.mapreduce.util.PhoenixConfigurationUtil.SchemaType)1 PDataType (org.apache.phoenix.schema.types.PDataType)1 ColumnInfo (org.apache.phoenix.util.ColumnInfo)1 ResourceSchema (org.apache.pig.ResourceSchema)1 ResourceFieldSchema (org.apache.pig.ResourceSchema.ResourceFieldSchema)1