-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
109 lines (101 loc) · 18.8 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title><![CDATA[MacOS10.12下RStudio载入rJava出错的解决方法]]></title>
<url>https://blog.ijita.me/2017/02/14/rjava-error/</url>
<content type="html"><![CDATA[<h2 id="运行环境">运行环境</h2>
<p><code>macOS Sierra 10.12.3</code></p>
<p><code>jdk1.7.0_79</code></p>
<p><code>R version 3.3.2 (2016-10-31)</code></p>
<h2 id="错误信息">错误信息</h2>
<p>在<code>R</code>或者<code>RStudio</code>中载入依赖<code>rJava</code>的包后会抛出如下错误信息:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">JavaVM: requested Java version ((null)) not available. Using Java at "" instead.</div><div class="line">JavaVM: Failed to load JVM: /bundle/Libraries/libserver.dylib</div><div class="line">JavaVM FATAL: Failed to load the jvm library.</div><div class="line">Error in .jinit() : JNI_GetCreatedJavaVMs returned -1</div></pre></td></tr></table></figure>
<h2 id="解决方法">解决方法</h2>
<p>在终端中执行:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib</div><div class="line">sudo R CMD javareconf</div></pre></td></tr></table></figure>
<p>在R中执行:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">install.packages("rJava",type='source')</div></pre></td></tr></table></figure>
]]></content>
</entry>
<entry>
<title><![CDATA[从零开始玩转Protobuf之一 - 准备工作]]></title>
<url>https://blog.ijita.me/2017/02/08/start_protobuf/</url>
<content type="html"><![CDATA[<blockquote>
<p><strong><em>本系列文章统一使用JAVA并在MacOS上进行演绎,如遇错误请指出,期望共同学习进步</em></strong></p>
</blockquote>
<h2 id="0x00-protobuf简介">0x00 Protobuf简介</h2>
<p>Protobuf全称为<strong>Google Protocol Buffer</strong>,它是一种轻便高效的结构化数据存储格式,主要用于结构化数据串行化,很适合做数据存储或RPC数据交换格式,相比XML它更小更快更简单,它的处理时间开销、空间开销更小。</p>
<p>首次使用时定义一种结构化数据的格式即 <strong><em>.proto文件</em></strong> ,然后使用<strong><em>Protocol Compiler</em></strong>根据此结构生成对应语言的特殊源代码,之后便能轻松地向各种数据流读写结构化数据,在后期修改了数据格式之后也不会影响旧版本的程序。</p>
<h2 id="0x01-下载并编译protocol-compiler">0x01 下载并编译Protocol Compiler</h2>
<p>首先下载对应的<strong><em>Protocol Compiler</em></strong>,用于编译 <strong><em>.proto文件</em></strong> 和生成给定语言操作数据流的代码。</p>
<blockquote>
<p>书写这篇文章时,Protobuf的最新版为3.2.0,可到如下地址获取相应语言的最新版本:<a href="https://github.com/google/protobuf/releases" target="_blank" rel="external">https://github.com/google/protobuf/releases</a></p>
</blockquote>
<blockquote>
<p>由于Protocol编译器是用C++开发的,需提前安装make、g++等相关工具,安装方法请自行Google。</p>
</blockquote>
<p>下载最新Release包:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">wget https://github.com/google/protobuf/releases/download/v3.2.0/protobuf-java-3.2.0.tar.gz</div><div class="line">tar -xzf protobuf-java-3.2.0.tar.gz</div><div class="line">cd protobuf-java-3.2.0</div></pre></td></tr></table></figure>
<p>编译并安装:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">configure ## 编译三连</div><div class="line">make</div><div class="line">make install</div><div class="line"></div><div class="line">protoc --version ## 测试Protocol编译器是否安装成功</div></pre></td></tr></table></figure>
<blockquote>
<p>其他各平台的详细安装方法参见protobuf-3.2.0/src/README.md</p>
</blockquote>
<h2 id="0x02-编译java-runtime-library">0x02 编译Java Runtime Library</h2>
<h3 id="安装maven">安装Maven</h3>
<p>Protobuf的Java项目采用<strong><em>maven</em></strong>进行管理,先安装maven,若已有maven环境忽略此步骤。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">brew install maven ## 使用brew进行安装</div><div class="line"></div><div class="line">mvn test ## 安装完maven后测试</div></pre></td></tr></table></figure>
<h3 id="使用maven安装依赖并打包">使用maven安装依赖并打包</h3>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">cd protobuf-3.2.0/java/</div><div class="line">mvn install</div><div class="line">mvn package</div></pre></td></tr></table></figure>
<h3 id="配置classpath">配置CLASSPATH</h3>
<p>完成上述步骤后将会在protobuf-3.2.0/java/core/target目录生成protobuf-java-3.2.0.jar包,我们将protobuf-java-3.2.0.jar加入到CLASSPATH中:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">export CLASSPATH={PATH_TO_JARBALL}/protobuf-3.2.0/java/core/target/protobuf-java-3.2.0.jar:$CLASSPATH ## 请修改{PATH_TO_JARBALL}目录</div><div class="line"></div><div class="line">echo 'CLASSPATH={PATH_TO_JARBALL}/protobuf-3.2.0/java/core/target/protobuf-java-3.2.0.jar:$CLASSPATH' > ~/.profile ## 也可将上述代码加入~/.profile文件中</div><div class="line">source ~/.profile</div></pre></td></tr></table></figure>
<p>到此就完成了前置依赖的安装,下面我们来真正认识protobuf。</p>
<h2 id="0x03-编译并运行示例">0x03 编译并运行示例</h2>
<p>执行如下命令编译示例代码:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">cd protobuf-3.2.0/examples/</div><div class="line">make java</div></pre></td></tr></table></figure>
<p>如果一切正常将会在当前目录生成 <strong>add_person_java</strong> 和 <strong>list_people_java</strong> 两个shell脚本,其中 <strong>add_person_java</strong> 用于添加人员信息并存储到文件中,<strong>list_people_java</strong> 从文件中读取人员信息。</p>
<p>执行add_person_java脚本录入一些信息:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">sh ./add_person_java</div><div class="line">Usage: AddPerson ADDRESS_BOOK_FILE</div><div class="line"></div><div class="line">sh ./add_person_java ./person.data</div><div class="line">./person.data: File not found. Creating a new file.</div><div class="line">Enter person ID: 1</div><div class="line">Enter name: Nemo</div><div class="line">Enter email address (blank for none): [email protected]</div><div class="line">Enter a phone number (or leave blank to finish): 18012345678</div><div class="line">Is this a mobile, home, or work phone? mobile</div><div class="line">Enter a phone number (or leave blank to finish): 86260000</div><div class="line">Is this a mobile, home, or work phone? home</div><div class="line">Enter a phone number (or leave blank to finish):</div></pre></td></tr></table></figure>
<p>程序将会在当前目录生成 <strong><em>person.data</em></strong> 文件,这个就是采用protobuf结构化之后存储的数据文件。
执行list_people_java脚本查看之前录入的数据:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">sh ./list_people_java ./person.data</div><div class="line">Person ID: 1</div><div class="line"> Name: Nemo</div><div class="line"> E-mail address: [email protected]</div><div class="line"> Mobile phone #: 18012345678</div><div class="line"> Home phone #: 86260000</div></pre></td></tr></table></figure>
<h2 id="0x04-示例代码简析">0x04 示例代码简析</h2>
<p>上面我们完整的演示了Protobuf将结构化数据写入文件和从文件中读取结构化数据的示例,那么这过程是如何完成的呢?</p>
<p>示例中使用了 <strong><em>addressbook.proto</em></strong> 文件定义了相应的包名、类名、以及Person和AddressBook两种协议:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> tutorial;</div><div class="line"></div><div class="line">option java_package = <span class="string">"com.example.tutorial"</span>;</div><div class="line">option java_outer_classname = <span class="string">"AddressBookProtos"</span>;</div><div class="line"></div><div class="line">message Person {</div><div class="line"> string name = <span class="number">1</span>;</div><div class="line"> int32 id = <span class="number">2</span>; <span class="comment">// Unique ID number for this person.</span></div><div class="line"> string email = <span class="number">3</span>;</div><div class="line"></div><div class="line"> <span class="keyword">enum</span> PhoneType {</div><div class="line"> MOBILE = <span class="number">0</span>;</div><div class="line"> HOME = <span class="number">1</span>;</div><div class="line"> WORK = <span class="number">2</span>;</div><div class="line"> }</div><div class="line"></div><div class="line"> message PhoneNumber {</div><div class="line"> string number = <span class="number">1</span>;</div><div class="line"> PhoneType type = <span class="number">2</span>;</div><div class="line"> }</div><div class="line"></div><div class="line"> repeated PhoneNumber phones = <span class="number">4</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// Our address book file is just one of these.</span></div><div class="line">message AddressBook {</div><div class="line"> repeated Person people = <span class="number">1</span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>Protobuf会根据 <strong><em>addressbook.proto</em></strong> 文件中的内容生成相应的java class,即protobuf-3.2.0/examples/com/example/tutorial/AddressBookProtos.java文件,此AddressBookProtos类包含了一系列Person和AddressBook类的操作,用于读取和写入数据,可以理解为Protobuf根据定义的数据结构封装的数据操作。</p>
<p><strong><em>add_person</em></strong> 主要是将用户输入存储到文件,addPeople()方法增加people数据,build()方法将数据序列化,核心代码片段:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">AddressBook.Builder addressBook = AddressBook.newBuilder();</div><div class="line"></div><div class="line"><span class="keyword">try</span> {</div><div class="line"> FileInputStream input = <span class="keyword">new</span> FileInputStream(args[<span class="number">0</span>]);</div><div class="line"> <span class="keyword">try</span> {</div><div class="line"> addressBook.mergeFrom(input);</div><div class="line"> } <span class="keyword">finally</span> {</div><div class="line"> <span class="keyword">try</span> { input.close(); } <span class="keyword">catch</span> (Throwable ignore) {}</div><div class="line"> }</div><div class="line">} <span class="keyword">catch</span> (FileNotFoundException e) {</div><div class="line"> System.out.println(args[<span class="number">0</span>] + <span class="string">": File not found. Creating a new file."</span>);</div><div class="line">}</div><div class="line"></div><div class="line">addressBook.addPeople(</div><div class="line"> PromptForAddress(<span class="keyword">new</span> BufferedReader(<span class="keyword">new</span> InputStreamReader(System.in)),</div><div class="line"> System.out));</div><div class="line"></div><div class="line">FileOutputStream output = <span class="keyword">new</span> FileOutputStream(args[<span class="number">0</span>]);</div><div class="line"><span class="keyword">try</span> {</div><div class="line"> addressBook.build().writeTo(output);</div><div class="line">} <span class="keyword">finally</span> {</div><div class="line"> output.close();</div><div class="line">}</div></pre></td></tr></table></figure>
<p><strong><em>list_people</em></strong> 从文件中读取已序列化的二进制数据,getPeopleList()获取people列表数据,getId()和getName()为获取属性值方法,核心代码片段:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">for</span> (Person person: addressBook.getPeopleList()) {</div><div class="line"> System.out.println(<span class="string">"Person ID: "</span> + person.getId());</div><div class="line"> System.out.println(<span class="string">" Name: "</span> + person.getName());</div><div class="line"> <span class="keyword">if</span> (!person.getEmail().isEmpty()) {</div><div class="line"> System.out.println(<span class="string">" E-mail address: "</span> + person.getEmail());</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">for</span> (Person.PhoneNumber phoneNumber : person.getPhonesList()) {</div><div class="line"> <span class="keyword">switch</span> (phoneNumber.getType()) {</div><div class="line"> <span class="keyword">case</span> MOBILE:</div><div class="line"> System.out.print(<span class="string">" Mobile phone #: "</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> HOME:</div><div class="line"> System.out.print(<span class="string">" Home phone #: "</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> WORK:</div><div class="line"> System.out.print(<span class="string">" Work phone #: "</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> }</div><div class="line"> System.out.println(phoneNumber.getNumber());</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="0x05-总结">0x05 总结</h2>
<p>虽然Protobuf相比于XML和Json,其可读性差、缺乏自我描述,在通用性上也不及后两者普及,但它具有更高的效率,体积更小,并且能够自动生成对应语言的特殊源代码便于开发。</p>
]]></content>
</entry>
<entry>
<title><![CDATA[鸡年大吉]]></title>
<url>https://blog.ijita.me/2017/01/27/2017dajiba/</url>
<content type="html"><![CDATA[<h3 id="hello-2017">Hello, 2017</h3>
<p>2017年正式加入Github Pages,开始用Markdown写博客了,相比Wordpress,除了简便还是简便。之前用Wordpress写的博文就不同步过来了,没有太多有价值的文字,而且探讨的也是游戏相关的东西。</p>
<h3 id="探讨和学习">探讨和学习</h3>
<p>以后将在博客内探讨和学习以下几方面的内容:</p>
<blockquote>
<ol>
<li>人工智能 (Artificial Intelligence)</li>
<li>大数据 (Big Data)</li>
<li>物联网 (Internet of Things)</li>
</ol>
</blockquote>
<h3 id="2017年的三个目标">2017年的三个目标</h3>
<blockquote>
<ol>
<li>摆脱驾照本本族</li>
<li>求婚并且把结婚证扯了</li>
<li>带上妹纸出国旅游</li>
</ol>
</blockquote>
<p><strong>那就祝我🐥年大吉吧!</strong></p>
]]></content>
</entry>
</search>