How to create/use schema Builder?

Asked

Viewed 219 times

0

I’m trying to create a Builder schema, but I don’t know directly how to make one, looking at the Handbook I come across the following code, but I try to implement in my Java code and cannot create.

In case I will show the JSON that is in manual and its Java implementation. I also noticed that there are a quotation marks opening but not closing, I tried to close it but it did not solve the problem, I do not know exactly if it is so the code or they missed the manual.

Example from JSON:

 {
 "type": "record",
 "name": "HandshakeRequest", "namespace":"org.apache.avro.ipc",
 "fields": [
 {"name": "clientHash",
  "type": {"type": "fixed", "name": "MD5", "size": 16}},
 {"name": "clientProtocol", "type": ["null", "string"]},
 {"name": "serverHash", "type": "MD5"},
 {"name": "meta", "type": ["null", {"type": "map", "values": "bytes"}]}
 ]
}

Example of Java code:

   Schema schema = SchemaBuilder
  .record("HandshakeRequest").namespace("org.apache.avro.ipc)
  .fields()
  .name("clientHash").type().fixed("MD5").size(16).noDefault()
  .name("clientProtocol").type().nullable().stringType().noDefault()
  .name("serverHash").type("MD5")
  .name("meta").type().nullable().map().values().bytesType().noDefault()
  .endRecord();

Libraries used:

import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;

I wanted to know if the implementation of the code is correct, in case I would not like an example of how it would look to implement the above code.

The error that is giving is syntax error and not compilation error, if this is to help in something. I would just like you to help me get the syntax right and explain a little better about how the schema Builder works and the error happens on line 2 of the Java code:

"Illegal line end in string literal"

Which I believe is due to the lack of quotation marks in the manual code:

.record("HandshakeRequest").namespace("org.apache.avro.ipc)

But even closing the quotes occurs an error on this line:

.name("meta").type().nullable().map().values().bytesType().noDefault()

The mistake:

Cannot Resolve method 'name. (java.lang.String)'

I believe the code of the manual is wrong, so I believe it is important to show how to use the Builder for Schemabuilder.

1 answer

5


I’m not in the habit of using these JSON schemas, but come on...


In fact, the documentation code is wrong, because it doesn’t even compile.

The mistake "Illegal line end in string literal" you already killed, missed to close the quotes in the stretch namespace("org.apache.avro.ipc).

About the error "Cannot Resolve method 'name. (java.lang.String)'", the problem is on this line:

.name("serverHash").type("MD5")

The method type() returns a org.apache.avro.SchemaBuilder.GenericDefault, which is a class that has no method called name. Hence the mistake "Cannot Resolve method name", for the method name does not exist in this class.

In fact this class, according to the documentation, has only the methods noDefault() (indicating that the field will not have a value default), and withDefault(Object valor) (indicating what the default country).

So missed calling one of these methods, because they return a org.apache.avro.SchemaBuilder.FieldAssembler (this yes, a class that owns the method name()). Ex:

Schema schema = SchemaBuilder
  .record("HandshakeRequest").namespace("org.apache.avro.ipc") // <-- fecha as aspas
  .fields()
    .name("clientHash").type().fixed("MD5").size(16).noDefault()
    .name("clientProtocol").type().nullable().stringType().noDefault()
    .name("serverHash").type("MD5").noDefault() // <-- Faltou chamar noDefault() (ou withDefault(valorDefault))
    .name("meta").type().nullable().map().values().bytesType().noDefault()
  .endRecord();
System.out.println(schema.toString(true));

This code prints out:

{
  "type" : "record",
  "name" : "HandshakeRequest",
  "namespace" : "org.apache.avro.ipc",
  "fields" : [ {
    "name" : "clientHash",
    "type" : {
      "type" : "fixed",
      "name" : "MD5",
      "size" : 16
    }
  }, {
    "name" : "clientProtocol",
    "type" : [ "string", "null" ]
  }, {
    "name" : "serverHash",
    "type" : "MD5"
  }, {
    "name" : "meta",
    "type" : [ {
      "type" : "map",
      "values" : "bytes"
    }, "null" ]
  } ]
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.