gklijs / schema_registry_converter

A crate to convert bytes to something more useable and the other way around in a way Compatible with the Confluent Schema Registry. Supporting Avro, Protobuf, Json schema, and both async and blocking.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

encode string as key?

jonbuffington opened this issue · comments

I am unable to determine how to encode a String value as the key. Ideally, the steps would be similar to:

fn get_string_primitive_schema() -> Box<SuppliedSchema> {
    Box::from(SuppliedSchema::new(r#"{"type": "string"}"#.into()))
}

let key_strategy = SubjectNameStrategy::TopicNameStrategyWithSchema(
    "test".into(),
    true,
    cp::get_string_primitive_schema(),
);
let k = Value::String("hello".to_string());
let key = self.encoder.encode(k, &key_strategy)?;

Am I missing a feature in the library or is encoding a primitive value not implemented?

I don't test for it specifically so it could be that it's not working. Do you get an error of some kind? I'm not sure how often it's used like that. In my opinion it's better to just use Strings and a string(de)serializer in that case.

The above generates a compile error as .encode expects a Vec<(&'static str, Value)> type. If you change encode to encode_struct in the above snippet, the string is successfully encoded. I kept ignoring encode_struct since I was trying to encode a simple primitive.

Are the below name mappings roughly equivalent to your intended usage?

  • encode -> encode_name_value_pairs
  • encode_struct -> encode_using_avro_serde -> encode

Not suggesting actual changes. I am just asking to clarify my thinking.

Yes, I first just had the encode, and in that case I assumed it was a record, and thus have key-value pairs for the values. The encode_struct I added later, but it's giving errors in some cases. It's good to know it works well with primitive schema's. I might add specific tests and update te Readme when I get to it. But this will not be the case for some weeks due to other obligations.

Completely understand. Thanks for taking time to respond and sharing this project.

I added encoding a primary type as example to the docs in the pr,

/// let primitive_schema_strategy = SubjectNameStrategy::TopicNameStrategyWithSchema("heartbeat".into(), true, get_supplied_schema(&Schema::String));
.

Name is still the same, which might be confusing, but the number of breaking changes is already growing, and I think it will be used more to encode structs.

One of the changes will be that the avro encoder /decoder will be behind a feature flag, in its own module. This in order to support also other encoders.

Thanks for the update and I understand not changing the name. The change to separate the Avro codec makes sense also given the recent changes to schema registry by Confluent.

Yes, it's all become a lot more complex, also with the addition of references. Not sure when I cut a release. I want to be sure enough about the api at least to not have multiple breaking changes in a row and I also need to update the documentation a bit. For know that means I need to redesign the http calls to schema registry a bit to allow for additional headers and proxy.

Would be great if you could check the pr. As this is pretty much my only rust project this year, I much like any feedback. Closing this issue, as it's 'solved' with the pr.

Also checked against the Java client and on primitives the behaviour seems the same. For example you can't use primitives with naming strategies based on the record name. And the stored schema, for a string value, is "string".

Sorry for the delay. The example in the doc string does help clarify.

I agree about the Java client and and other language clients. The naming was the cause of my original confusion.

Thanks again for sharing your work on this project.