Scala :Protobuf
Protobuf file (Like json and xml , protobuf is used to store and transmit data).
Below are some of the advantages of protobuf
1. Scheme is preserved
2. similar to json / xml
3. Cross platform
4. Parsing is faster
5. Compressed
Example :
Pre - Req : Create a new project in scala using sbt
build.sbt
name := "protobuf"
version := "0.1"
scalaVersion := "2.12.7"
PB.targets in Compile := Seq(
scalapb.gen() -> (sourceManaged in Compile).value
)
PB.protoSources in Test := Seq(file("/src/protobuf/"))
libraryDependencies += "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf"
project_name/project/plugins.sbt
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.18")
libraryDependencies += "com.thesamet.scalapb
src/main/protobuf/person.proto
syntax = "proto3";
message Person {
string name = 1;
int32 age = 2;
Gender gender = 3;
}
enum Gender {
MALE = 0;
FEMALE = 1;
}
src/main/Persontest.scala
import com.google.protobuf.ByteString
import java.io.{InputStream, OutputStream}
import person.{Gender, Person}
object Persontest {
val person = Person(
name = "User1",
age = 15,
gender = Gender.MALE
)
}
No comments:
Post a Comment