private PhoenixPigSchemaUtil() {
}
public static ResourceSchema getResourceSchema(final PhoenixPigConfiguration phoenixConfiguration) throws IOException {
final ResourceSchema schema = new ResourceSchema();
try {
List<ColumnInfo> columns = null;
if(SchemaType.QUERY.equals(phoenixConfiguration.getSchemaType())) {
final String sqlQuery = phoenixConfiguration.getSelectStatement();
Preconditions.checkNotNull(sqlQuery, "No Sql Query exists within the configuration");
final SqlQueryToColumnInfoFunction function = new SqlQueryToColumnInfoFunction(phoenixConfiguration);
columns = function.apply(sqlQuery);
} else {
columns = phoenixConfiguration.getSelectColumnMetadataList();
}
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);
}