{"componentChunkName":"component---src-templates-blog-post-js","path":"/cs/프로토콜-버퍼(protocol-buffer)/","result":{"data":{"site":{"siteMetadata":{"title":"Study Log","author":"[HongDongUk]","siteUrl":"https://donguk.netlify.com","comment":{"disqusShortName":"","utterances":"doonguk/doonguk.github.io"},"sponsor":{"buyMeACoffeeId":"doonguk"}}},"markdownRemark":{"id":"0ce62cd9-5159-5102-aa60-cecec113a623","excerpt":"1. 프로토콜 버퍼란? 프로토콜 버퍼는 XML, JSON과 같은  직렬화 데이터 구조 이다. 여기서 직렬화 데이터 구조의 예시를 잠깐 들면, hello를 ‘문자열 직렬화’ 할 경우 8(h) 65(e) 6C(l) 6C(l) 6F(o) 로 표현되고, 어떤 객체를 ‘JSON 포맷 직렬화’ 할 경우 {‘something’ : ‘something value’} 처럼 표현될 수 있다. 이처럼 프로토콜 버퍼는 데이터를 표현하는 구조 라고 이해할 수 있겠다. 2. 프로토콜 버퍼의 원리 JSON…","html":"<h3 id=\"1-프로토콜-버퍼란\" style=\"position:relative;\"><a href=\"#1-%ED%94%84%EB%A1%9C%ED%86%A0%EC%BD%9C-%EB%B2%84%ED%8D%BC%EB%9E%80\" aria-label=\"1 프로토콜 버퍼란 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. 프로토콜 버퍼란?</h3>\n<p>프로토콜 버퍼는 XML, JSON과 같은  <strong>직렬화 데이터 구조</strong> 이다. 여기서 직렬화 데이터 구조의 예시를 잠깐 들면,</p>\n<p>hello를 ‘문자열 직렬화’ 할 경우 8(h) 65(e) 6C(l) 6C(l) 6F(o) 로 표현되고,</p>\n<p>어떤 객체를 ‘JSON 포맷 직렬화’ 할 경우 {‘something’ : ‘something value’} 처럼 표현될 수 있다.</p>\n<p>이처럼 프로토콜 버퍼는 데이터를 표현하는 구조 라고 이해할 수 있겠다.</p>\n<h3 id=\"2-프로토콜-버퍼의-원리\" style=\"position:relative;\"><a href=\"#2-%ED%94%84%EB%A1%9C%ED%86%A0%EC%BD%9C-%EB%B2%84%ED%8D%BC%EC%9D%98-%EC%9B%90%EB%A6%AC\" aria-label=\"2 프로토콜 버퍼의 원리 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. 프로토콜 버퍼의 원리</h3>\n<p>JSON 포맷과 비교하여 이해를 하면,</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token comment\">//Person Class object JSON 포맷화</span>\n<span class=\"token punctuation\">{</span>\n\t<span class=\"token property\">\"userName\"</span><span class=\"token operator\">:</span><span class=\"token string\">\"Martin\"</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"favouriteNumber\"</span><span class=\"token operator\">:</span><span class=\"token number\">1337</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"interests\"</span><span class=\"token operator\">:</span><span class=\"token punctuation\">[</span><span class=\"token string\">\"daydreaming\"</span><span class=\"token punctuation\">,</span><span class=\"token string\">\"hacking\"</span><span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>데이터의 크기가 얼핏 잡아도 80byte가 넘는다. 근데 같은 데이터를 프로토콜 버퍼를 이용하여 나타낸다면?</p>\n<img width=\"670\" alt=\"&#xC2A4;&#xD06C;&#xB9B0;&#xC0F7; 2019-10-28 &#xC624;&#xD6C4; 11 41 41\" src=\"https://user-images.githubusercontent.com/39187116/67690406-2fb6a400-f9e0-11e9-8b4b-fe0e88095f46.png\">\n<div class=\"gatsby-highlight\" data-language=\"protobuf\"><pre class=\"language-protobuf\"><code class=\"language-protobuf\"><span class=\"token keyword\">message</span> <span class=\"token class-name\">Person</span><span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">required</span> <span class=\"token builtin\">string</span> user_name <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> \n\t<span class=\"token keyword\">optional</span> <span class=\"token builtin\">int64</span> favourite_number <span class=\"token operator\">=</span> <span class=\"token number\">2</span><span class=\"token punctuation\">;</span> \n\t<span class=\"token keyword\">repeated</span> <span class=\"token builtin\">string</span> interests <span class=\"token operator\">=</span> <span class=\"token number\">3</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>위 코드는 프로토콜 버퍼 데이터 타입으로 작성된 .proto 파일이다. JSON 포맷과 다른점은 불필요한 속성 값들이 1, 2, 3 으로 대체 됐다. </p>\n<p>그림을 보면 데이터의 첫 바이트를 5bit. 3bit로 나눴는데 5bit에는 대체된 번호 ( 1, 2, 3 )를 나타내고 있고 3bit에는 데이터 타입을 표현한다. ( 010 은 string ) 그리고 다음 바이트에는 이어지는 데이터의 길이를  나타내준다. 이와 같은 원리로 33byte에 80byte가 넘는 데이터를 표현 가능한 것이다.</p>\n<h3 id=\"3-프로토콜-버퍼-특징\" style=\"position:relative;\"><a href=\"#3-%ED%94%84%EB%A1%9C%ED%86%A0%EC%BD%9C-%EB%B2%84%ED%8D%BC-%ED%8A%B9%EC%A7%95\" aria-label=\"3 프로토콜 버퍼 특징 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. 프로토콜 버퍼 특징</h3>\n<ul>\n<li>빠르다.</li>\n</ul>\n<p>데이터의 크기가 작기 때문에 빠르다. 그리고 JSON 포맷과 다르게 프로토콜 버퍼를 사용하게 되면 바이트가 오기 때문에 별도의 파싱이 필요없이 메모리에 저장할 수 있다.  결론은 빠르다.</p>\n<ul>\n<li>가독성이 안좋다.</li>\n</ul>\n<p>JSON은 사람이 읽기에 편하지만, 프로토콜 버퍼는 .proto파일이 없다면 데이터를 이해 할 수 없다. </p>\n<h3 id=\"4-프로토콜-버퍼-컴파일러-설치--mac-\" style=\"position:relative;\"><a href=\"#4-%ED%94%84%EB%A1%9C%ED%86%A0%EC%BD%9C-%EB%B2%84%ED%8D%BC-%EC%BB%B4%ED%8C%8C%EC%9D%BC%EB%9F%AC-%EC%84%A4%EC%B9%98--mac-\" aria-label=\"4 프로토콜 버퍼 컴파일러 설치  mac  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. 프로토콜 버퍼 컴파일러 설치 ( Mac )</h3>\n<ol>\n<li><code><a href=\"https://github.com/google/protobuf/releases\">https://github.com/google/protobuf/releases</a></code> 이곳에서 자신이 필요한 프로토콜 버퍼를 다운 받는다.</li>\n<li>터미널에서 <code>brew install autoconf &#x26;&#x26; brew install automake</code></li>\n<li>\n<p>1번에서 다운받은 protobuf 폴더에 들어가서 <code>./autogen.sh &#x26;&#x26; ./configure &#x26;&#x26;. make</code> ( 오래 걸린다 )</p>\n<ul>\n<li>Can’t exec “glibtoolize”: No such file 오류가 뜬다면 brew install libtool</li>\n<li>설치를 하는 protobuf폴더의 이름에 공백이 있으면 설치가 안된다.</li>\n</ul>\n</li>\n<li>3번 과정이 끝난다면,</li>\n</ol>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ <span class=\"token function\">make</span> check\n$ <span class=\"token function\">sudo</span> <span class=\"token function\">make</span> <span class=\"token function\">install</span>\n$ <span class=\"token function\">which</span> protoc \n$ protoc --version</code></pre></div>\n<h3 id=\"5-참고-자료\" style=\"position:relative;\"><a href=\"#5-%EC%B0%B8%EA%B3%A0-%EC%9E%90%EB%A3%8C\" aria-label=\"5 참고 자료 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5. 참고 자료</h3>\n<p><a href=\"https://medium.com/@erika_dike/installing-the-protobuf-compiler-on-a-mac-a0d397af46b8\">https://medium.com/@erika_dike/installing-the-protobuf-compiler-on-a-mac-a0d397af46b8</a></p>\n<p><a href=\"https://jeong-pro.tistory.com/190\">https://jeong-pro.tistory.com/190</a></p>","frontmatter":{"title":"프로토콜 버퍼(Protocol Buffer)","date":"November 07, 2019"}}},"pageContext":{"slug":"/cs/프로토콜-버퍼(protocol-buffer)/","previous":null,"next":{"fields":{"slug":"/linux/생활코딩-리눅스-강좌( 기초-1 )/"},"frontmatter":{"title":"생활코딩 리눅스 강좌(기초-1)","category":"linux"}}}},"staticQueryHashes":["3128451518","96099027"]}