Using Protocol Buffers in Scala

What is Protocol Buffers?

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the “old” format.

– Google Documentation

How It Works?

  • Define your structured data format in a descriptor file (.proto file)
  • Run the protocol buffer compiler for your application’s language on your .proto file to generate data access classes.
  • We can even update our data structure without breaking deployed programs that are compiled against the “old” format.

What is Message?

  • Define your structured data format in a descriptor file (.proto file)
  • Run the protocol buffer compiler for your application’s language on your .proto file to generate data access classes.
  • We can even update our data structure without breaking deployed programs that are compiled against the “old” format.

What is Field?

Field is represented by

Available data-types:

  • Primitive data-type
  • Enumerated data-type
  • Nested Message – Allows structuring data into an hierarchy

Field-types can be:

  • Required fields
  • Optional fields
  • Repeated fields – Dynamically sized array

Encoding-value:

A unique number (=1,=2,…) represents a tag that a particular field has in the binary encoding of the message

Advantages of Protocol Buffers

Protocol buffers with respect to XML : 

  • are simpler
  • are 3 to 10 times smaller
  • are 20 to 100 times faster
  • are less ambiguous
  • generate data access classes that are easier to use in programs

And other benefits:

  • Easy to Modify
  • Backward Compatibility

Disadvantages of Protocol Buffers

  • Protocol buffers would not be a good way to model a text-based document with markup (e.g. HTML), since you cannot easily interleave structure with text.
  • XML is human-readable and human-editable; protocol buffers, at least in their native format, are not. XML is also – to some extent – self-describing. A protocol buffer is only meaningful if you have the message definition (.proto file).

Sample Code: 

https://github.com/PKOfficial/proto-buf-scala-example

References

https://developers.google.com/protocol-buffers/docs/overview

Knoldus-Scala-spark-services-company

Written by 

Prabhat is a Sr. Software Consultant with more than 2 years of experience in C, C++, Java, MySQL, and Scala. His interests are in Cyber Security, Web Development, and New technologies. Prabhat developed software and website on different platforms which include VB6, VB.NET, ASP.NET, PHP, Wordpress, OpenCart, SMF, VBulletin, HTML5, MyBB. Prabhat is focused and result oriented, self-motivated and team-oriented and effective team player.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!