Search code examples
google-cloud-platformgoogle-cloud-spanner

Represent postal addresses in Cloud Spanner


How would one represent postal addresses in Cloud Spanner so that they are consistent with the proto defined in postal_address.proto? I want to read and write the from/to the database using Go.

Initially, I represented the postal address as a single Address column of type STRING in Spanner. I did this by concatenating all of the fields defined in the proto file. This works decently well for write operations.

However, when I wanted to read from the database, I was obviously not able to extract these specific fields that were concatenated as a string. I need the fields because I need to populate a struct with a PostalAddress field.

What would be the best way to represent a postal address of this nature? I could represent each field as a separate column, but this seems far too involved and would mean I would have to write far more complex queries to get a single address.


Solution

  • It sounds like your use case is a good fit for JSON type which Cloud Spanner now supports. You should be able to create a corresponding JSON representation for the postal_address.proto that you have mentioned. Please see https://cloud.google.com/spanner/docs/working-with-json for how-to as well as examples of how to query individual fields during reads.