1
votes

I am using scala 2.12 and have following dependencies in my build.sbt.

libraryDependencies += "org.apache.kafka" % "kafka-clients" % "0.10.1.0"

libraryDependencies += "io.confluent" % "kafka-avro-serializer" % "3.1.1"

libraryDependencies += "io.confluent" % "common-config" % "3.1.1"

libraryDependencies += "io.confluent" % "common-utils" % "3.1.1"

libraryDependencies += "io.confluent" % "kafka-schema-registry-client" % "3.1.1"

Thanks to this community, I am able to convert my raw data to required avro format.

We need to use the confluent libraries to serialize and send the data to the Kafka topics.

I am using the following properties and avro record.

properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "io.confluent.kafka.serializers.KafkaAvroSerializer")
    properties.put("schema.registry.url", "http://myschemahost:8081")

Just showing required snippet of code for brevity.

val producer = new KafkaProducer[String, GenericData.Record](properties)
val schema = new Schema.Parser().parse(new File(schemaFileName))

var avroRecord = new GenericData.Record(schema)
// code to populate record
// check output below to see the data
logger.info(s"${avroRecord.toString}\n")

producer.send(new ProducerRecord[String, GenericData.Record](topic, avroRecord), new ProducerCallback)
producer.flush()
producer.close()

Schema and Data as per the output.

{"name": "person","type": "record","fields": [{"name": "address","type": {"type" : "record","name" : "AddressUSRecord","fields" : [{"name": "streetaddress", "type": "string"},{"name": "city", "type":"string"}]}}]}

I am getting the following error while publishing to Kafka.

Error registering Avro schema: 
org.apache.kafka.common.errors.SerializationException:
Caused by: io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 1, column: 2]; error code: 50005
        at io.confluent.kafka.schemaregistry.client.rest.RestService.sendHttpRequest(RestService.java:170)
        at io.confluent.kafka.schemaregistry.client.rest.RestService.httpRequest(RestService.java:187)
        at io.confluent.kafka.schemaregistry.client.rest.RestService.registerSchema(RestService.java:238)
        at io.confluent.kafka.schemaregistry.client.rest.RestService.registerSchema(RestService.java:230)
        at io.confluent.kafka.schemaregistry.client.rest.RestService.registerSchema(RestService.java:225)
        at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.registerAndGetId(CachedSchemaRegistryClient.java:59)
        at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.register(CachedSchemaRegistryClient.java:91)
        at io.confluent.kafka.serializers.AbstractKafkaAvroSerializer.serializeImpl(AbstractKafkaAvroSerializer.java:72)
        at io.confluent.kafka.serializers.KafkaAvroSerializer.serialize(KafkaAvroSerializer.java:54)
        at org.apache.kafka.common.serialization.Serializer.serialize(Serializer.java:60)
        at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:877)
        at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:839)
  1. Based on schema and data, is anything missing? My record is correct ?
  2. Also, I want to know how should I populate "avro" NULL from Scala? None doesn't work.

Any help will be appreciated. I am really stuck here.

UPDATE:

Thanks @cricket_007 for pointing out the issue. I do get following error:

2019-03-20 13:26:09.660 [application-akka.actor.default-dispatcher-5] INFO  i.c.k.s.KafkaAvroSerializerConfig.logAll(169) - KafkaAvroSerializerConfig values:
        schema.registry.url = [http://myhost:8081]
        max.schemas.per.subject = 1000


Caused by: io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 1, column: 2]; error code: 50005

However, When I use the same URL (http://myhost:8081) on my browser it works well. I can see the subjects, and other information. But as soon as I use the client (Scala program above), it fails with above error.

I just checked with a sample code like below, it gives same issue.

val client = new OkHttpClient
    val request = new Request.Builder().url("http://myhost:8081/subjects").build()
    val output = client.newCall(request).execute().body().string()
    logger.info(s"Subjects: ${output}\n")

I am getting connection refused for the schema registry URL.

Subjects: <HEAD><TITLE>Connection refused</TITLE></HEAD>
<BODY BGCOLOR="white" FGCOLOR="black"><H1>Connection refused</H1><HR>
<FONT FACE="Helvetica,Arial"><B>
Description: Connection refused</B></FONT>
<HR>
<!-- default "Connection refused" response (502) -->
</BODY>

So, wanted to check if I am missing anything. Same thing works when I run it on browser but simple code like above it fails.

1

1 Answers

1
votes

That's an HTTP response parsing error. Seems your schema registry is not returning a JSON response, and rather some HTML starting with a < open tag.

You should check if the registry is really running at http://myschemahost:8081, and you can manually post your schema to it using the REST API to do the same actions as the serializer would.