@Test public void testCreateSchema() throws Exception { ResultSet rs = CassandraQueryTestUtil.createMockResultSet(); Schema schema = QueryCassandra.createSchema(rs); assertNotNull(schema); assertEquals(Schema.Type.RECORD, schema.getType()); Schema.Field field = schema.getField("user_id"); assertNotNull(field); Schema fieldSchema = field.schema(); Schema.Type type = fieldSchema.getType(); assertEquals(Schema.Type.UNION, type); assertEquals(Schema.Type.NULL, fieldSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, fieldSchema.getTypes().get(1).getType()); field = schema.getField("first_name"); assertNotNull(field); fieldSchema = field.schema(); type = fieldSchema.getType(); assertEquals(Schema.Type.UNION, type); assertEquals(Schema.Type.NULL, fieldSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, fieldSchema.getTypes().get(1).getType()); field = schema.getField("last_name"); assertNotNull(field); fieldSchema = field.schema(); type = fieldSchema.getType(); assertEquals(Schema.Type.UNION, type); assertEquals(Schema.Type.NULL, fieldSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, fieldSchema.getTypes().get(1).getType()); field = schema.getField("emails"); assertNotNull(field); fieldSchema = field.schema(); type = fieldSchema.getType(); assertEquals(Schema.Type.UNION, type); assertEquals(Schema.Type.NULL, fieldSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.ARRAY, fieldSchema.getTypes().get(1).getType()); Schema arraySchema = fieldSchema.getTypes().get(1); Schema elementSchema = arraySchema.getElementType(); assertEquals(Schema.Type.UNION, elementSchema.getType()); assertEquals(Schema.Type.NULL, elementSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, elementSchema.getTypes().get(1).getType()); field = schema.getField("top_places"); assertNotNull(field); fieldSchema = field.schema(); type = fieldSchema.getType(); assertEquals(Schema.Type.UNION, type); assertEquals(Schema.Type.ARRAY, fieldSchema.getTypes().get(1).getType()); arraySchema = fieldSchema.getTypes().get(1); elementSchema = arraySchema.getElementType(); assertEquals(Schema.Type.UNION, elementSchema.getType()); assertEquals(Schema.Type.NULL, elementSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, elementSchema.getTypes().get(1).getType()); field = schema.getField("todo"); assertNotNull(field); fieldSchema = field.schema(); type = fieldSchema.getType(); assertEquals(Schema.Type.UNION, type); assertEquals(Schema.Type.MAP, fieldSchema.getTypes().get(1).getType()); Schema mapSchema = fieldSchema.getTypes().get(1); Schema valueSchema = mapSchema.getValueType(); assertEquals(Schema.Type.NULL, valueSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, valueSchema.getTypes().get(1).getType()); field = schema.getField("registered"); assertNotNull(field); fieldSchema = field.schema(); type = fieldSchema.getType(); assertEquals(Schema.Type.UNION, type); assertEquals(Schema.Type.NULL, fieldSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.BOOLEAN, fieldSchema.getTypes().get(1).getType()); field = schema.getField("scale"); assertNotNull(field); fieldSchema = field.schema(); type = fieldSchema.getType(); assertEquals(Schema.Type.UNION, type); assertEquals(Schema.Type.NULL, fieldSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.FLOAT, fieldSchema.getTypes().get(1).getType()); field = schema.getField("metric"); assertNotNull(field); fieldSchema = field.schema(); type = fieldSchema.getType(); assertEquals(Schema.Type.UNION, type); assertEquals(Schema.Type.NULL, fieldSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.DOUBLE, fieldSchema.getTypes().get(1).getType()); }
public static Schema createSchema(final ResultSet rs) throws IOException { final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions(); final int nrOfColumns = (columnDefinitions == null ? 0 : columnDefinitions.size()); String tableName = "NiFi_Cassandra_Query_Record"; if (nrOfColumns > 0) { String tableNameFromMeta = columnDefinitions.getTable(1); if (!StringUtils.isBlank(tableNameFromMeta)) { tableName = tableNameFromMeta; } } final SchemaBuilder.FieldAssembler<Schema> builder = SchemaBuilder.record(tableName).namespace("any.data").fields(); if (columnDefinitions != null) { for (int i = 0; i < nrOfColumns; i++) { DataType dataType = columnDefinitions.getType(i); if (dataType == null) { throw new IllegalArgumentException("No data type for column[" + i + "] with name " + columnDefinitions.getName(i)); } if (dataType.isCollection()) { List<DataType> typeArguments = dataType.getTypeArguments(); if (typeArguments == null || typeArguments.size() == 0) { throw new IllegalArgumentException("Column[" + i + "] " + dataType.getName() + " is a collection but no type arguments were specified!"); } DataType firstArg = typeArguments.get(0); if (dataType.equals(DataType.set(firstArg)) || dataType.equals(DataType.list(firstArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().array() .items(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(firstArg))).endUnion().noDefault(); } else { DataType secondArg = typeArguments.get(1); if (dataType.equals(DataType.map(firstArg, secondArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().map().values( getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(secondArg))).endUnion().noDefault(); } } } else { builder.name(columnDefinitions.getName(i)) .type(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(dataType))).noDefault(); } } } return builder.endRecord(); }
QueryCassandra extends AbstractCassandraProcessor { public static Schema createSchema(final ResultSet rs) throws IOException { final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions(); final int nrOfColumns = (columnDefinitions == null ? 0 : columnDefinitions.size()); String tableName = "NiFi_Cassandra_Query_Record"; if (nrOfColumns > 0) { String tableNameFromMeta = columnDefinitions.getTable(1); if (!StringUtils.isBlank(tableNameFromMeta)) { tableName = tableNameFromMeta; } } final SchemaBuilder.FieldAssembler<Schema> builder = SchemaBuilder.record(tableName).namespace("any.data").fields(); if (columnDefinitions != null) { for (int i = 0; i < nrOfColumns; i++) { DataType dataType = columnDefinitions.getType(i); if (dataType == null) { throw new IllegalArgumentException("No data type for column[" + i + "] with name " + columnDefinitions.getName(i)); } if (dataType.isCollection()) { List<DataType> typeArguments = dataType.getTypeArguments(); if (typeArguments == null || typeArguments.size() == 0) { throw new IllegalArgumentException("Column[" + i + "] " + dataType.getName() + " is a collection but no type arguments were specified!"); } DataType firstArg = typeArguments.get(0); if (dataType.equals(DataType.set(firstArg)) || dataType.equals(DataType.list(firstArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().array() .items(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(firstArg))).endUnion().noDefault(); } else { DataType secondArg = typeArguments.get(1); if (dataType.equals(DataType.map(firstArg, secondArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().map().values( getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(secondArg))).endUnion().noDefault(); } } } else { builder.name(columnDefinitions.getName(i)) .type(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(dataType))).noDefault(); } } } return builder.endRecord(); } }
QueryCassandra extends AbstractCassandraProcessor { public static Schema createSchema(final ResultSet rs) throws IOException { final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions(); final int nrOfColumns = (columnDefinitions == null ? 0 : columnDefinitions.size()); String tableName = "NiFi_Cassandra_Query_Record"; if (nrOfColumns > 0) { String tableNameFromMeta = columnDefinitions.getTable(1); if (!StringUtils.isBlank(tableNameFromMeta)) { tableName = tableNameFromMeta; } } final SchemaBuilder.FieldAssembler<Schema> builder = SchemaBuilder.record(tableName).namespace("any.data").fields(); if (columnDefinitions != null) { for (int i = 0; i < nrOfColumns; i++) { DataType dataType = columnDefinitions.getType(i); if (dataType == null) { throw new IllegalArgumentException("No data type for column[" + i + "] with name " + columnDefinitions.getName(i)); } if (dataType.isCollection()) { List<DataType> typeArguments = dataType.getTypeArguments(); if (typeArguments == null || typeArguments.size() == 0) { throw new IllegalArgumentException("Column[" + i + "] " + dataType.getName() + " is a collection but no type arguments were specified!"); } DataType firstArg = typeArguments.get(0); if (dataType.equals(DataType.set(firstArg)) || dataType.equals(DataType.list(firstArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().array() .items(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(firstArg))).endUnion().noDefault(); } else { DataType secondArg = typeArguments.get(1); if (dataType.equals(DataType.map(firstArg, secondArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().map().values( getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(secondArg))).endUnion().noDefault(); } } } else { builder.name(columnDefinitions.getName(i)) .type(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(dataType))).noDefault(); } } } return builder.endRecord(); }  }
QueryCassandra extends AbstractCassandraProcessor { public static Schema createSchema(final ResultSet rs) throws IOException { final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions(); final int nrOfColumns = (columnDefinitions == null ? 0 : columnDefinitions.size()); String tableName = "NiFi_Cassandra_Query_Record"; if (nrOfColumns > 0) { String tableNameFromMeta = columnDefinitions.getTable(1); if (!StringUtils.isBlank(tableNameFromMeta)) { tableName = tableNameFromMeta; } } final SchemaBuilder.FieldAssembler<Schema> builder = SchemaBuilder.record(tableName).namespace("any.data").fields(); if (columnDefinitions != null) { for (int i = 0; i < nrOfColumns; i++) { DataType dataType = columnDefinitions.getType(i); if (dataType == null) { throw new IllegalArgumentException("No data type for column[" + i + "] with name " + columnDefinitions.getName(i)); } if (dataType.isCollection()) { List<DataType> typeArguments = dataType.getTypeArguments(); if (typeArguments == null || typeArguments.size() == 0) { throw new IllegalArgumentException("Column[" + i + "] " + dataType.getName() + " is a collection but no type arguments were specified!"); } DataType firstArg = typeArguments.get(0); if (dataType.equals(DataType.set(firstArg)) || dataType.equals(DataType.list(firstArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().array() .items(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(firstArg))).endUnion().noDefault(); } else { DataType secondArg = typeArguments.get(1); if (dataType.equals(DataType.map(firstArg, secondArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().map().values( getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(secondArg))).endUnion().noDefault(); } } } else { builder.name(columnDefinitions.getName(i)) .type(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(dataType))).noDefault(); } } } return builder.endRecord(); }  @Override Set<Relationship> getRelationships(); @Override final List<PropertyDescriptor> getSupportedPropertyDescriptors(); @OnScheduled void onScheduled(final ProcessContext context); @Override void onTrigger(final ProcessContext context, final ProcessSession session); @OnUnscheduled void stop(); @OnShutdown void shutdown(); static long convertToAvroStream(final ResultSet rs, final OutputStream outStream,
                                           long timeout, TimeUnit timeUnit); static long convertToJsonStream(final ResultSet rs, final OutputStream outStream,
                                           Charset charset, long timeout, TimeUnit timeUnit); static Schema createSchema(final ResultSet rs); }
QueryCassandra extends AbstractCassandraProcessor { public static Schema createSchema(final ResultSet rs) throws IOException { final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions(); final int nrOfColumns = (columnDefinitions == null ? 0 : columnDefinitions.size()); String tableName = "NiFi_Cassandra_Query_Record"; if (nrOfColumns > 0) { String tableNameFromMeta = columnDefinitions.getTable(1); if (!StringUtils.isBlank(tableNameFromMeta)) { tableName = tableNameFromMeta; } } final SchemaBuilder.FieldAssembler<Schema> builder = SchemaBuilder.record(tableName).namespace("any.data").fields(); if (columnDefinitions != null) { for (int i = 0; i < nrOfColumns; i++) { DataType dataType = columnDefinitions.getType(i); if (dataType == null) { throw new IllegalArgumentException("No data type for column[" + i + "] with name " + columnDefinitions.getName(i)); } if (dataType.isCollection()) { List<DataType> typeArguments = dataType.getTypeArguments(); if (typeArguments == null || typeArguments.size() == 0) { throw new IllegalArgumentException("Column[" + i + "] " + dataType.getName() + " is a collection but no type arguments were specified!"); } DataType firstArg = typeArguments.get(0); if (dataType.equals(DataType.set(firstArg)) || dataType.equals(DataType.list(firstArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().array() .items(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(firstArg))).endUnion().noDefault(); } else { DataType secondArg = typeArguments.get(1); if (dataType.equals(DataType.map(firstArg, secondArg))) { builder.name(columnDefinitions.getName(i)).type().unionOf().nullBuilder().endNull().and().map().values( getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(secondArg))).endUnion().noDefault(); } } } else { builder.name(columnDefinitions.getName(i)) .type(getUnionFieldType(getPrimitiveAvroTypeFromCassandraType(dataType))).noDefault(); } } } return builder.endRecord(); }  @Override Set<Relationship> getRelationships(); @Override final List<PropertyDescriptor> getSupportedPropertyDescriptors(); @OnScheduled void onScheduled(final ProcessContext context); @Override void onTrigger(final ProcessContext context, final ProcessSession session); @OnUnscheduled void stop(); @OnShutdown void shutdown(); static long convertToAvroStream(final ResultSet rs, final OutputStream outStream,
                                           long timeout, TimeUnit timeUnit); static long convertToJsonStream(final ResultSet rs, final OutputStream outStream,
                                           Charset charset, long timeout, TimeUnit timeUnit); static Schema createSchema(final ResultSet rs); static final String AVRO_FORMAT; static final String JSON_FORMAT; static final String RESULT_ROW_COUNT; static final PropertyDescriptor CQL_SELECT_QUERY; static final PropertyDescriptor QUERY_TIMEOUT; static final PropertyDescriptor FETCH_SIZE; static final PropertyDescriptor OUTPUT_FORMAT; static final Relationship REL_SUCCESS; static final Relationship REL_FAILURE; static final Relationship REL_RETRY; }