Skip to content

tutorial/handling null values in protobuf and avro#36

Open
Lucia Cerchie (Cerchie) wants to merge 16 commits intoconfluentinc:masterfrom
Cerchie:tutorial/handling-null-vals
Open

tutorial/handling null values in protobuf and avro#36
Lucia Cerchie (Cerchie) wants to merge 16 commits intoconfluentinc:masterfrom
Cerchie:tutorial/handling-null-vals

Conversation

@Cerchie
Copy link
Copy Markdown
Contributor

need to figure out a smooth way to handle testing and a way to teach the reader about what happens when null values are handled/not. tagging Dave Troiano (@davetroiano)

@Cerchie Lucia Cerchie (Cerchie) requested a review from a team as a code owner February 16, 2024 16:09
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread handling-null-values-in-avro-and-protobuf/kafka/.gitignore Outdated
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md Outdated
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md Outdated
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md Outdated
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md Outdated
.setTotalCost(random.nextDouble() * random.nextInt(100));
```

In this case, you'll receive a NullPointer error. No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats going to come next? the field being oneof where one is a boolean to signify null?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be a protobuf schema addition... if I were to show it I think it'd be a oneof, but also there's a google wrapper option. unfortunately because of the way the API is defined with protocol buffers and kafka there are two methods with the same name and java chokes on the protocol buffer... not sure how to fix here

Copy link
Copy Markdown
Contributor

@davetroiano Dave Troiano (davetroiano) Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does optional do the trick? i.e. does everything just work if you do that? I'm looking at the guidance here:

If not using optional would add complexity or ambiguity, then use optional primitive fields. Wrapper types must not be used going forward.

which makes me think we might want to avoid the wrapper pattern... I'm not sure why this is the guidance, e.g., will wrapper types eventually be deprecated?

I'm thinking that if optional works, go with that, and we'd also want to update this docs section to recommend optional rather than use a wrapper

wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so with this protobuf definition:

message Purchase {
  optional string item = 1;
  double total_cost = 2;
  string customer_id = 3;
}

you still end up with a NullPointerException ¯_(ツ)_/¯

but that's a good point about the guidance, I'll pass it on to docs.

Comment thread handling-null-values-in-avro-and-protobuf/kafka/build.gradle Outdated
Comment thread handling-null-values-in-avro-and-protobuf/kafka/build.gradle Outdated
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md Outdated
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md Outdated
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md Outdated
Comment thread handling-null-values-in-avro-and-protobuf/kafka/README.md Outdated
# How to allow `null` field values in Avro and Protobuf
Let's say you're using an Avro or Protobuf schema, and sometimes you want to set a field named `item` to null. Say it's a pipeline that takes both donations and purchases to be filtered later, and the donations are processed as purchases with null items. How to adjust the schema to allow for a null value?

Avro natively supports null fields with the 'null' type. In the above example, in order to make the `item` field nullable, you can allow the type to be "string" or "null" in the following manner:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Avro natively supports null fields with the 'null' type. In the above example, in order to make the `item` field nullable, you can allow the type to be "string" or "null" in the following manner:
Avro natively supports null fields with the `null` type. In the above example, in order to make the `item` field nullable, you can allow the type to be "string" or "null" in the following manner:

From the top-level directory:

```
./gradlew clean :handling-null-values-in-avro-and-protobuf:kafka:test --info
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this didn't work for me on a fresh clone:

* What went wrong:
A problem occurred evaluating project ':handling-null-values-in-avro-and-protobuf:kafka'.
> /Users/dtroiano/Desktop/tutorials/handling-null-values-in-avro-and-protobuf/kafka/src/main/resources/confluent.properties (No such file or directory)

.setTotalCost(random.nextDouble() * random.nextInt(100));
```

In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/). No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/).
In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/).

.setTotalCost(random.nextDouble() * random.nextInt(100));
```

In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/). No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning to have an example with wrapper? or leave it as an exercise to the reader

.setTotalCost(random.nextDouble() * random.nextInt(100));
```

In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/). No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest adding a clean up step that directs the user to delete the env created for this

@Cerchie Lucia Cerchie (Cerchie) changed the title 🚧 WIP: tutorial/handling null values in protobuf and avro tutorial/handling null values in protobuf and avro Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants