4

Using Protocol Buffers in Scala

 3 years ago
source link: https://blog.knoldus.com/using-protocol-buffers-in-scala/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Reading Time: 2 minutes

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.
xxxxxxxxxx
message Person {
    string name = 1;
    int32 age = 2;
    Gender gender = 3;
}

What is Field?

Field is represented by

xxxxxxxxxx
string name = 1;

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK