Java Code Examples for org.apache.avro.Schema.Parser#parse()

The following examples show how to use org.apache.avro.Schema.Parser#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidateRecord.java    From nifi with Apache License 2.0 6 votes vote down vote up
protected RecordSchema getValidationSchema(final ProcessContext context, final FlowFile flowFile, final RecordReader reader)
    throws MalformedRecordException, IOException, SchemaNotFoundException {
    final String schemaAccessStrategy = context.getProperty(SCHEMA_ACCESS_STRATEGY).getValue();
    if (schemaAccessStrategy.equals(READER_SCHEMA.getValue())) {
        return reader.getSchema();
    } else if (schemaAccessStrategy.equals(SCHEMA_NAME_PROPERTY.getValue())) {
        final SchemaRegistry schemaRegistry = context.getProperty(SCHEMA_REGISTRY).asControllerService(SchemaRegistry.class);
        final String schemaName = context.getProperty(SCHEMA_NAME).evaluateAttributeExpressions(flowFile).getValue();
        final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name(schemaName).build();
        return schemaRegistry.retrieveSchema(schemaIdentifier);
    } else if (schemaAccessStrategy.equals(SCHEMA_TEXT_PROPERTY.getValue())) {
        final String schemaText = context.getProperty(SCHEMA_TEXT).evaluateAttributeExpressions(flowFile).getValue();
        final Parser parser = new Schema.Parser();
        final Schema avroSchema = parser.parse(schemaText);
        return AvroTypeUtil.createSchema(avroSchema);
    } else {
        throw new ProcessException("Invalid Schema Access Strategy: " + schemaAccessStrategy);
    }
}
 
Example 2
Source File: SchemaDeserializer.java    From data-highway with Apache License 2.0 5 votes vote down vote up
@Override
public Schema deserialize(JsonParser parser, DeserializationContext context)
  throws IOException, JsonProcessingException {
  Parser schemaParser = new Schema.Parser();
  // Validate any default values provided
  schemaParser.setValidateDefaults(true);
  // Validate all names.
  schemaParser.setValidate(true);
  return schemaParser.parse(parser.readValueAsTree().toString());
}
 
Example 3
Source File: CDM.java    From SPADE with GNU General Public License v3.0 5 votes vote down vote up
public JsonReader(String dataFilepath, String schemaFilepath) throws Exception{
	this.filepath = dataFilepath;
	Parser parser = new Schema.Parser();
	Schema schema = parser.parse(new File(schemaFilepath));
	this.datumReader = new SpecificDatumReader<Object>(schema);
	this.decoder = DecoderFactory.get().jsonDecoder(schema, 
			new FileInputStream(new File(dataFilepath)));
}
 
Example 4
Source File: CDM.java    From SPADE with GNU General Public License v3.0 5 votes vote down vote up
public BinaryReader(String dataFilepath, String schemaFilepath) throws Exception{
	this.filepath = dataFilepath;
	Parser parser = new Schema.Parser();
	Schema schema = parser.parse(new File(schemaFilepath));
	DatumReader<Object> datumReader = new SpecificDatumReader<Object>(schema);
	this.dataFileReader = new DataFileReader<>(new File(dataFilepath), datumReader);
}
 
Example 5
Source File: FileWriter.java    From SPADE with GNU General Public License v3.0 5 votes vote down vote up
public FileWriter(String schemaFile, String outputFile) throws IOException{
	Parser parser = new Schema.Parser();
	Schema schema = parser.parse(new File(schemaFile));
       DatumWriter<Object> datumWriter = new SpecificDatumWriter<Object>(schema);
	fileWriter = new DataFileWriter<>(datumWriter);
	fileWriter.create(schema, new File(outputFile));
}
 
Example 6
Source File: JsonFileWriter.java    From SPADE with GNU General Public License v3.0 5 votes vote down vote up
public JsonFileWriter(String schemaFile, String outputFilePath) throws Exception{
	
	File outputFile = new File(outputFilePath);
	if(outputFile == null || outputFile.getParentFile() == null || !outputFile.getParentFile().exists()){
		throw new Exception("Invalid file path: " + outputFilePath);
	}
	
	Parser parser = new Schema.Parser();
	Schema schema = parser.parse(new File(schemaFile));
	datumWriter = new SpecificDatumWriter<Object>(schema);
	OutputStream outputStream = new FileOutputStream(outputFile);
	jsonEncoder = EncoderFactory.get().jsonEncoder(schema, outputStream);
}
 
Example 7
Source File: DirectBigQueryRecordReaderTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  // Manually creating the parsed Schema is a pain so duplicate logic
  Parser parser = new Parser();
  parsedSchema = parser.parse(RAW_SCHEMA);

  MockitoAnnotations.initMocks(this);
  when(bqClient.readRowsCallable()).thenReturn(readRows);
  when(readRows.call(any(ReadRowsRequest.class))).thenReturn(rowsStream);

  reader = new TestDirectBigQueryRecordReader();
}
 
Example 8
Source File: StructSchema.java    From pulsar with Apache License 2.0 4 votes vote down vote up
protected static org.apache.avro.Schema parseAvroSchema(String schemaJson) {
    final Parser parser = new Parser();
    parser.setValidateDefaults(false);
    return parser.parse(schemaJson);
}
 
Example 9
Source File: MappingTestServer.java    From divolte-collector with Apache License 2.0 4 votes vote down vote up
private Schema loadSchema(final String schemaFilename) throws IOException {
    final Parser parser = new Schema.Parser();
    return parser.parse(new File(schemaFilename));
}