Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Protocol Buffers @potatochips#40

Protocol Buffers @potatochips#40

TakuSemba

May 24, 2017
Tweet

More Decks by TakuSemba

Other Decks in Technology

Transcript

  1. Taku Semba
    Protocol Buffers
    CyberAgent,Inc

    View Slide

  2. About Me
    Taku Semba
    TakuSemba
    @takusemba
    AbemaTV.Inc

    View Slide

  3. Protocol Buffers
    language-neutral, platform-neutral
    extensible mechanism for serializing
    structured data
    ɾ
    ɾ
    faster than Json
    ɾ binary based data transmission

    View Slide

  4. Protocol Buffers
    json Protocol buffers
    \
    QSFGFDUVSFT<
    \
    JE
    OBNF๺ւಓ
    SPNBKJIPLLBJEP
    ^
    \
    JE
    OBNF੨৿ݝ
    SPNBKJBPNPSJ
    ^
    \
    ʜ
    \
    JE
    OBNFࣛࣇౡݝ
    SPNBKJLBHPTIJNB
    ^
    \
    JE
    OBNFԭೄݝ
    SPNBKJPLJOBXB
    ^
    >
    ^


    ʜ


    View Slide

  5. Quick Start

    View Slide

  6. How to use
    define proto file
    ɾ
    ɾ generate compile / decompile file
    ɾ make an API call

    View Slide

  7. define proto file
    ɾ
    ɾ generate compile / decompile file
    ɾ make an API call
    How to use

    View Slide

  8. syntax = "proto3";
    option java_package = "com.your.package.name";
    option java_outer_classname = "Prefectures";
    option go_package = "proto";
    package prefectures;
    message Prefecture {
    int64 id = 1; // ID
    string name = 2; // name
    string romaji = 3; // romaji
    }
    message GetPrefecturesResponse {
    repeated Prefecture prefectures = 1;
    }
    define proto file

    View Slide

  9. syntax = "proto3";
    option java_package = "com.your.package.name";
    option java_outer_classname = "Prefectures";
    option go_package = "proto";
    package prefectures;
    message Prefecture {
    int64 id = 1; // ID
    string name = 2; // name
    string romaji = 3; // romaji
    }
    message GetPrefecturesResponse {
    repeated Prefecture prefectures = 1;
    }
    define proto file

    View Slide

  10. syntax = "proto3";
    option java_package = "com.your.package.name";
    option java_outer_classname = "Prefectures";
    option go_package = "proto";
    package prefectures;
    message Prefecture {
    int64 id = 1; // ID
    string name = 2; // name
    string romaji = 3; // romaji
    }
    message GetPrefecturesResponse {
    repeated Prefecture prefectures = 1;
    }
    define proto file

    View Slide

  11. How to use
    define proto file
    ɾ
    ɾ generate compile / decompile file
    ɾ make an API call

    View Slide

  12. wire: square੡ͷprotocol buffers༻ͷϥΠϒϥϦ
    java -jar wire-compiler-VERSION-jar-with-dependencies.jar \

    --proto_path=your/proto/path \

    --java_out=your/output/path

    ˈ
    generate compile / decompile file

    View Slide

  13. generate compile / decompile file

    View Slide

  14. public final class Prefecture extends MessagePrefecture.Builder> {
    public final class GetPrefecturesResponse extends MessageGetPrefecturesResponse.Builder> {
    ɾɾɾ
    }
    ɾɾɾ
    }
    generate compile / decompile file

    View Slide

  15. public final class Prefecture extends MessagePrefecture.Builder> {
    public final class GetPrefecturesResponse extends MessageGetPrefecturesResponse.Builder> {
    ɾɾɾ
    }
    ɾɾɾ
    }
    generate compile / decompile file

    View Slide

  16. How to use
    define proto file
    ɾ
    ɾ generate compile / decompile file
    ɾ make an API call

    View Slide

  17. dependencies {
    // wire for retrofit
    compile ‘com.squareup.retrofit2:converter-wire:x.x.x'
    // wire
    compile ‘com.squareup.wire:wire-runtime:x.x.x’
    // protocol buffers
    compile 'com.google.protobuf:protobuf-java:x.x.x'
    }
    make an API call

    View Slide

  18. interface Service {
    @GET("prefectures")
    fun getProtoPrefectures(): Single
    }
    private val retrofit: Retrofit = Retrofit
    .Builder()
    .baseUrl(endpoint)
    .client(okHttpClient)
    .addConverterFactory(WireConverterFactory.create())
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build()
    make an API call

    View Slide

  19. interface Service {
    @GET("prefectures")
    fun getProtoPrefectures(): Single
    }
    private val retrofit: Retrofit = Retrofit
    .Builder()
    .baseUrl(endpoint)
    .client(okHttpClient)
    .addConverterFactory(WireConverterFactory.create())
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build()
    make an API call

    View Slide

  20. interface Service {
    @GET("prefectures")
    fun getProtoPrefectures(): Single
    }
    private val retrofit: Retrofit = Retrofit
    .Builder()
    .baseUrl(endpoint)
    .client(okHttpClient)
    .addConverterFactory(WireConverterFactory.create())
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build()
    make an API call

    View Slide

  21. fun getPrefectures(): Single> {
    return retrofit
    .create(Service::class.java)
    .getProtoPrefectures()
    .map(GetPrefecturesResponse::prefectures)
    .map {
    val prefectures: ArrayList = ArrayList()
    it.mapTo(prefectures) { value ->
    Prefecture().apply {
    id = value.id
    name = value.name
    romaji = value.romaji
    }
    }
    prefectures.toList()
    }
    }
    make an API call

    View Slide

  22. fun getPrefectures(): Single> {
    return retrofit
    .create(Service::class.java)
    .getProtoPrefectures()
    .map(GetPrefecturesResponse::prefectures)
    .map {
    val prefectures: ArrayList = ArrayList()
    it.mapTo(prefectures) { value ->
    Prefecture().apply {
    id = value.id
    name = value.name
    romaji = value.romaji
    }
    }
    prefectures.toList()
    }
    }
    make an API call

    View Slide

  23. fun getPrefectures(): Single> {
    return retrofit
    .create(Service::class.java)
    .getProtoPrefectures()
    .map(GetPrefecturesResponse::prefectures)
    .map {
    val prefectures: ArrayList = ArrayList()
    it.mapTo(prefectures) { value ->
    Prefecture().apply {
    id = value.id
    name = value.name
    romaji = value.romaji
    }
    }
    prefectures.toList()
    }
    }
    make an API call

    View Slide

  24. fast because of small data
    proto file could be an API document
    Conclusion
    pros
    hard to debug
    take time to make an api change
    Cons

    View Slide

  25. Protocol Buffers
    https://github.com/takusemba
    https://twitter.com/takusemba

    View Slide