use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.
the class AvroTypeFactoryImpl method convertSchema.
private SqlSchema convertSchema(List<Schema.Field> fields, boolean isTopLevelField) {
SqlSchemaBuilder schemaBuilder = SqlSchemaBuilder.builder();
for (Schema.Field field : fields) {
SqlFieldSchema fieldSchema = convertField(field.schema(), false, isOptional(field, isTopLevelField));
schemaBuilder.addField(field.name(), fieldSchema);
}
return schemaBuilder.build();
}
use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.
the class SamzaExecutor method getTableSchema.
@Override
public SqlSchema getTableSchema(ExecutionContext context, String tableName) throws ExecutorException {
/*
* currently Shell works only for systems that has Avro schemas
*/
int execId = execIdSeq.incrementAndGet();
Map<String, String> staticConfigs = fetchSamzaSqlConfig(execId);
Config samzaSqlConfig = new MapConfig(staticConfigs);
SqlSchema sqlSchema;
try {
SqlIOResolver ioResolver = SamzaSqlApplicationConfig.createIOResolver(samzaSqlConfig);
SqlIOConfig sourceInfo = ioResolver.fetchSourceInfo(tableName);
RelSchemaProvider schemaProvider = SamzaSqlApplicationConfig.initializePlugin("RelSchemaProvider", sourceInfo.getRelSchemaProviderName(), samzaSqlConfig, SamzaSqlApplicationConfig.CFG_FMT_REL_SCHEMA_PROVIDER_DOMAIN, (o, c) -> ((RelSchemaProviderFactory) o).create(sourceInfo.getSystemStream(), c));
sqlSchema = schemaProvider.getSqlSchema();
} catch (SamzaException ex) {
throw new ExecutorException(ex);
}
return sqlSchema;
}
use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.
the class SamzaExecutorTest method testGenerateResultSchema.
// Generate result schema needs to be fixed. SAMZA-2079
@Ignore
@Test
public void testGenerateResultSchema() {
prepareEnvironmentVariable();
Map<String, String> mapConf = mExecutor.fetchSamzaSqlConfig(1);
SqlSchema ts = mExecutor.generateResultSchema(new MapConfig(mapConf));
List<SqlSchema.SqlField> fields = ts.getFields();
Assert.assertEquals("__key__", fields.get(0).getFieldName());
Assert.assertEquals("Name", fields.get(1).getFieldName());
Assert.assertEquals("NewCompany", fields.get(2).getFieldName());
Assert.assertEquals("OldCompany", fields.get(3).getFieldName());
Assert.assertEquals("ProfileChangeTimestamp", fields.get(4).getFieldName());
Assert.assertEquals("ANY", fields.get(0).getFieldSchema().getFieldType().toString());
Assert.assertEquals("VARCHAR", fields.get(1).getFieldSchema().getFieldType().toString());
Assert.assertEquals("VARCHAR", fields.get(2).getFieldSchema().getFieldType().toString());
Assert.assertEquals("VARCHAR", fields.get(3).getFieldSchema().getFieldType().toString());
Assert.assertEquals("BIGINT", fields.get(4).getFieldSchema().getFieldType().toString());
}
use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.
the class CliCommandHandler method formatSchema4Display.
/*
Field | Type
-------------------------
Field1 | Type 1
Field2 | VARCHAR(STRING)
Field... | VARCHAR(STRING)
-------------------------
*/
private List<String> formatSchema4Display(SqlSchema schema) {
final String headerField = "Field";
final String headerType = "Type";
final char seperator = '|';
final char lineSep = '-';
int terminalWidth = terminal.getWidth();
// Two spaces * 2 plus one SEPERATOR
if (terminalWidth < 2 + 2 + 1 + headerField.length() + headerType.length()) {
return Collections.singletonList("Not enough room.");
}
// Find the best seperator position for least rows
int seperatorPos = headerField.length() + 2;
int minRowNeeded = Integer.MAX_VALUE;
int longestLineCharNum = 0;
int rowCount = schema.getFields().size();
for (int j = seperatorPos; j < terminalWidth - headerType.length() - 2; ++j) {
boolean fieldWrapped = false;
int rowNeeded = 0;
for (int i = 0; i < rowCount; ++i) {
SqlSchema.SqlField field = schema.getFields().get(i);
int fieldLen = field.getFieldName().length();
int typeLen = field.getFieldSchema().getFieldType().toString().length();
int fieldRowNeeded = CliUtil.ceilingDiv(fieldLen, j - 2);
int typeRowNeeded = CliUtil.ceilingDiv(typeLen, terminalWidth - 1 - j - 2);
rowNeeded += Math.max(fieldRowNeeded, typeRowNeeded);
fieldWrapped |= fieldRowNeeded > 1;
if (typeRowNeeded > 1) {
longestLineCharNum = terminalWidth;
} else {
longestLineCharNum = Math.max(longestLineCharNum, j + typeLen + 2 + 1);
}
}
if (rowNeeded < minRowNeeded) {
minRowNeeded = rowNeeded;
seperatorPos = j;
}
if (!fieldWrapped)
break;
}
List<String> lines = new ArrayList<>(minRowNeeded + 4);
// Header
StringBuilder line = new StringBuilder(terminalWidth);
line.append(CliConstants.SPACE);
line.append(headerField);
CliUtil.appendTo(line, seperatorPos - 1, CliConstants.SPACE);
line.append(seperator);
line.append(CliConstants.SPACE);
line.append(headerType);
lines.add(line.toString());
line = new StringBuilder(terminalWidth);
CliUtil.appendTo(line, longestLineCharNum - 1, lineSep);
lines.add(line.toString());
// Body
AttributedStyle oddLineStyle = AttributedStyle.BOLD.foreground(AttributedStyle.BLUE);
AttributedStyle evenLineStyle = AttributedStyle.BOLD.foreground(AttributedStyle.CYAN);
final int fieldColSize = seperatorPos - 2;
final int typeColSize = terminalWidth - seperatorPos - 1 - 2;
for (int i = 0; i < rowCount; ++i) {
SqlSchema.SqlField sqlField = schema.getFields().get(i);
String field = sqlField.getFieldName();
String type = getFieldDisplayValue(sqlField.getFieldSchema());
int fieldLen = field.length();
int typeLen = type.length();
int fieldStartIdx = 0, typeStartIdx = 0;
while (fieldStartIdx < fieldLen || typeStartIdx < typeLen) {
line = new StringBuilder(terminalWidth);
line.append(CliConstants.SPACE);
int numToWrite = Math.min(fieldColSize, fieldLen - fieldStartIdx);
if (numToWrite > 0) {
line.append(field, fieldStartIdx, fieldStartIdx + numToWrite);
fieldStartIdx += numToWrite;
}
CliUtil.appendTo(line, seperatorPos - 1, CliConstants.SPACE);
line.append(seperator);
line.append(CliConstants.SPACE);
numToWrite = Math.min(typeColSize, typeLen - typeStartIdx);
if (numToWrite > 0) {
line.append(type, typeStartIdx, typeStartIdx + numToWrite);
typeStartIdx += numToWrite;
}
if (i % 2 == 0) {
AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(evenLineStyle);
attrBuilder.append(line.toString());
lines.add(attrBuilder.toAnsi());
} else {
AttributedStringBuilder attrBuilder = new AttributedStringBuilder().style(oddLineStyle);
attrBuilder.append(line.toString());
lines.add(attrBuilder.toAnsi());
}
}
}
// Footer
line = new StringBuilder(terminalWidth);
CliUtil.appendTo(line, longestLineCharNum - 1, lineSep);
lines.add(line.toString());
return lines;
}
use of org.apache.samza.sql.schema.SqlSchema in project samza by apache.
the class SamzaExecutor method generateResultSchema.
SqlSchema generateResultSchema(Config config) {
SamzaSqlDslConverter converter = (SamzaSqlDslConverter) new SamzaSqlDslConverterFactory().create(config);
RelRoot relRoot = converter.convertDsl("").iterator().next();
List<String> colNames = new ArrayList<>();
List<String> colTypeNames = new ArrayList<>();
for (RelDataTypeField dataTypeField : relRoot.validatedRowType.getFieldList()) {
colNames.add(dataTypeField.getName());
colTypeNames.add(dataTypeField.getType().toString());
}
// in QueryResult class and executeQuery().
return new SqlSchema(colNames, Collections.emptyList());
}
Aggregations