当前位置:网站首页>Protobuf data exchange format
Protobuf data exchange format
2022-07-28 13:11:00 【Love in Nanxin】
brief introduction
ProtoBuf(Google Protocol Buffer) By google Sequential structured data formats used by companies for data exchange , With cross platform 、 Cross language 、 Scalability , The type is suitable for the commonly used XML And JSON,
But with a smaller transport volume 、 Higher coding 、 Decoding ability , Especially suitable for data storage 、 Network data transmission and so on to the storage volume 、 Areas with high real-time requirements . With .proto For the suffix , Has its own compiler protoc, protoc2 and protoc3 edition .
Simple understanding : comparison json More efficient , Efficient serialization and deserialization , A data exchange format that can compress data into smaller sizes ( For the time being, it is simply understood as more efficient and powerful json).gRPC The default is based on protobuf Data format for data exchange , therefore
protobuf You must learn before you become gRPC Prepare for learning . Write it well .protoc After file , Use protoc And according to the input parameters , Select the type of language to convert , Can be converted into the code of various programming languages .
protobuf Examples
Next, there is no need to go over the grammar word by word , I pass the commonly used grammar through a protobuf Documents can be elaborated , Make a summary . Just go to the official for detailed documents . I'm sure to learn in half an hour ,
It's not as complicated as expected , He Xue json almost . The details are as follows :
// notes : 1. // Single-line comments 2. /* */ Multiline comment
syntax = "proto3"; // Use... Explicitly protobuf edition Do not write by default It is proto2
package com.test; // Definition package package
// Official documents : https://developers.google.com/protocol-buffers/docs/proto3
import "./pk2.proto"; // Import protoW file
option go_package = "proto"; // If you use protoc Compiled into java Code Then generate according to the package name Ignore com.test
option java_package = "com.hello"; // If it is compiled as go Code According to com.hello Package name generation
// Definition message Information
message Keyword {
string value = 1;
}
// Define a message message Parameters
message QueryData{
// Basic data type field definitions Basic data types can cover string bool int double bytes wait
string name = 1;
string orderBy = 2;
double weight = 3;
float searchWeight = 4;
int32 type = 5;
uint64 id = 6;
fixed32 type2 = 7;
fixed64 type3 = 8;
bool isOk = 9;
bytes passWord = 10;
// Define an array similar []string
repeated string cases = 11;
// Definition map type string=>string
map <string,string> myMap = 15;
// oneof Optional fields There can only be one or 0 individual
oneof OptionField {
string selectWord = 12;
string queryType = 13;
}
// Define enumeration types
enum WeekDay {
SUNDAY = 0; // Enumeration type The first value must be 0
MONDAY = 1;
TUESDAY = 2;
}
WeekDay selectDay = 14;
// keyword Compound field type Quote the above defined Keyword type
Keyword kw = 16;
}
// Define interface return message Format
message QueryResponse {
int32 code = 1;
string msg = 2;
string htmlData = 3;
}
// Definition rpc service Generally refer to gRPC More
service QueryApi {
// 1.grpc One yuan in rpc Interface Very common It can be understood as normal http The interface is almost
rpc Query (QueryData) returns (QueryResponse){
};
// 2. Server based " streaming "rpc Interface After establishing the connection The server can send data to the client
rpc ServerQueryStyle(QueryData) returns (stream QueryResponse) {
};
// 3. Server based " streaming "rpc Interface Different from the first one This way is based on " flow " transmission Instead of sending a request response and then disconnecting
rpc ClientQueryStyle(stream QueryData) returns (QueryResponse) {
};
// 4. client-based , Server side " Two way flow "rpc Interface Full duplex communication Both parties can send data to each other
rpc ClientAndServerStreamQuery(stream QueryData) returns (stream QueryResponse){
};
}
边栏推荐
- The essence of enterprise Digitalization
- .net for subtraction, intersection and union of complex type sets
- What if the win11 folder cannot be opened
- Android engineers, how to use kotlin to provide productivity?
- Connected Block & food chain - (summary of parallel search set)
- 【嵌入式C基础】第7篇:C语言流程控制详讲
- [embedded C foundation] Part 8: explanation of C language array
- FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca
- [basic teaching of Bi design] detailed explanation of OLED screen use - single chip microcomputer Internet of things
- Leetcode:704 binary search
猜你喜欢
![[graduation design] heart rate detection system based on single chip microcomputer - STM32 embedded Internet of things](/img/b4/06c822c52f5bb0045698b7107efb26.png)
[graduation design] heart rate detection system based on single chip microcomputer - STM32 embedded Internet of things

Fundamentals of machine learning - support vector machine svm-17

Redefinition problem of defining int i variable in C for loop

How to open the power saving mode of win11 system computer

UV germicidal lamp chip dlt8p65sa Jericho
![[graduation design] smart home system based on ZigBee - single chip microcomputer Internet of things stm32](/img/c3/4268d7e4e1429f9b0d9d928790d740.png)
[graduation design] smart home system based on ZigBee - single chip microcomputer Internet of things stm32

How to use databricks for data analysis on tidb cloud | tidb cloud User Guide

【嵌入式C基础】第5篇:原码/反码/补码

Chinese translation of pointnet:deep learning on point sets for 3D classification and segmentation

Change the document type in endnode and import it in word
随机推荐
Vs code is not in its original position after being updated
【嵌入式C基础】第6篇:超详细的常用的输入输出函数讲解
[embedded C foundation] Part 7: detailed introduction to C language process control
Databinding+LiveData轻松实现无重启换肤
Kotlin是如何帮助你避免内存泄漏的?
Dimming and color matching cool light touch chip-dlt8ma12ts-jericho
Leetcode206 reverse linked list
Cloud native - runtime environment
Rolling update strategy of deployment.
20220728 common methods of object class
机器学习实战-神经网络-21
【嵌入式C基础】第1篇:基本数据类型
Quick read in
MySQL limit paging optimization
【嵌入式C基础】第9篇:C语言指针的基本用法
05 pyechars basic chart (example code + effect diagram)
Smart touch screen LCD bathroom mirror light touch chip-dlt8t02s-jericho
[embedded C foundation] Part 2: binary conversion and BCD coding
Interview must ask, focus! Tell me about the Android application startup process and its source code?
How many times can the WordPress user name be changed? Attach the method of changing user name