Need descriptor for in memory proto schema

116 views
Skip to first unread message

Vikas Dahiya

unread,
Aug 5, 2022, 5:14:38 PM8/5/22
to Protocol Buffers
I have an in memory proto schema which I am reading from the schema registry. I am looking for a way or the best way if there are more than one, to get the descriptor for this schema. How can I do that? I am new to protobuf and pardon me if the provided information is not sufficient, but I can provide any required additional details.

Thanks

Matthew Fowles Kulukundis

unread,
Aug 6, 2022, 2:57:25 PM8/6/22
to Vikas Dahiya, Protocol Buffers
Vikas~

What language are you using (C++, Java, Python, etc)?

Matt

On Fri, Aug 5, 2022 at 5:14 PM Vikas Dahiya <mailto...@gmail.com> wrote:
I have an in memory proto schema which I am reading from the schema registry. I am looking for a way or the best way if there are more than one, to get the descriptor for this schema. How can I do that? I am new to protobuf and pardon me if the provided information is not sufficient, but I can provide any required additional details.

Thanks

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/3b2a5728-549b-4c19-a1b2-4811b0124f8cn%40googlegroups.com.

Vikas Dahiya

unread,
Aug 6, 2022, 3:23:47 PM8/6/22
to Protocol Buffers
Matt, I am using Java.

Thanks!
Vikas

Matthew Fowles Kulukundis

unread,
Aug 6, 2022, 3:30:49 PM8/6/22
to Vikas Dahiya, Protocol Buffers
Vikas~

Do you have links to your code or snippets you can share?  Are you using kafka or AWS schema registry or something?

Matt

Vikas Dahiya

unread,
Aug 6, 2022, 3:57:20 PM8/6/22
to Matthew Fowles Kulukundis, Protocol Buffers
Matt, I am using kafka, I don’t have a code snippet as I am just getting the schema from confulant schema registry and need a descriptor to create the object. I can share the schema snippet if that can help.

Vikas

Matthew Fowles Kulukundis

unread,
Aug 6, 2022, 4:05:00 PM8/6/22
to Vikas Dahiya, Protocol Buffers
Vikas~

I don't really know the Kafka APIs, but it might be better to ask on one of their lists or maybe stack overflow.

Matt

Vikas Dahiya

unread,
Aug 6, 2022, 4:13:37 PM8/6/22
to Protocol Buffers
Matt,

Kafka APIs really don't have to do anything here. Think of it as a problem: I got a schema from any random place (in this case it's a schema registry), and now I need to use this schema to create an Object to use in my system. I don't have generated java classes at my disposal. So I am looking to get the descriptor to create an Object out of this schema. Hope that helps to understand the problem. 

Thanks,
Vikas

Matthew Fowles Kulukundis

unread,
Aug 6, 2022, 4:20:20 PM8/6/22
to Vikas Dahiya, Protocol Buffers
Vikas~

I suspect your best bet is:

```
Schema<?> s = ....
Object o = s.newInstance();
if (o instanceof MessageOrBuilder) {
    return ((MessageOrBuilder)o).getDescriptorForType();
}
return nullptr;
```

If you are using the JavaLite runtime, you may not have descriptor information available, so this can produce nullptr.

Matt

Vikas Dahiya

unread,
Aug 8, 2022, 1:29:20 PM8/8/22
to Protocol Buffers
Thanks, Matt!

The schema I have is in string format. Here is the snippet of the same

syntax = "proto3";
package com.company.qna.enriched;

import "imports/user/token_status.proto";
import "validate/validate.proto";
import "google/protobuf/timestamp.proto";

message QuestionViewEnriched {
  string event_id = 1 [(validate.rules).string.uuid = true];
  string source = 2 [(validate.rules).string.min_len = 3];
  string event_type = 3 [(validate.rules).string.prefix = "com.chegg"];
  .google.protobuf.Timestamp event_time = 4;
  string chegg_user_uuid = 21 [(validate.rules).string.uuid = true];
  string chegg_visitor_id = 22 [(validate.rules).string.uuid = true];
  string chegg_session_id = 23;
  string client_ip_address = 24 [(validate.rules).string.ip = true];
  string user_agent = 25 [(validate.rules).string.well_known_regex = HTTP_HEADER_VALUE];
  string language = 26 [(validate.rules).string.well_known_regex = HTTP_HEADER_VALUE];
  string experiments = 27;
  bool is_internal_user = 28;
  string device_class = 29;
  com.company.user.TokenStatus access_token_status = 30;
  com.company.user.TokenStatus id_token_status = 31;
  string url = 41;
  string referral_url = 42;

Matthew Fowles Kulukundis

unread,
Aug 8, 2022, 1:35:55 PM8/8/22
to Vikas Dahiya, Protocol Buffers
Vikas~

You are asking how to dynamically compile a .proto message into a descriptor that you can load at runtime.  I don't have a good answer here short of launching `protoc` as a subprocess.

Matt

Vikas Dahiya

unread,
Aug 8, 2022, 1:51:39 PM8/8/22
to Protocol Buffers
Ah, got it, Matt. That's what I was suspecting but wanted to avoid and see if there is any way around it. Thanks for patiently understanding and responding to my queries!

Thanks,
Vikas

Robert Yokota

unread,
Aug 8, 2022, 6:47:02 PM8/8/22
to Protocol Buffers
Vikas,

If you are using Confluent Schema Registry, there is a way to do this by using the Confluent libraries, namely `ProtobufSchema.toDescriptor()`.  But it's best that you ask this question in the Confluent Community slack forum or at https://forum.confluent.io/.  

Robert

Vikas Dahiya

unread,
Aug 8, 2022, 7:11:44 PM8/8/22
to Protocol Buffers
Thanks, Robert! I'll post it on their forum and see if they suggest something.

Thanks,
Vikas

Reply all
Reply to author
Forward
0 new messages