<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 30 Apr 2024 09:11:34 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] JAVA map-reduce job的counter页面无法显示的问题(error 500)</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-map-reduce-job%e7%9a%84counter%e9%a1%b5%e9%9d%a2%e6%97%a0%e6%b3%95%e6%98%be%e7%a4%ba%e7%9a%84%e9%97%ae%e9%a2%98error-500/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-map-reduce-job%e7%9a%84counter%e9%a1%b5%e9%9d%a2%e6%97%a0%e6%b3%95%e6%98%be%e7%a4%ba%e7%9a%84%e9%97%ae%e9%a2%98error-500/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 30 Apr 2024 09:11:34 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[error 500]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[map-reduce]]></category>
		<category><![CDATA[RFC 2616]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=14101</guid>

					<description><![CDATA[<p>这个问题已经不是第一次遇到了，只不过今天痛下决心花费不少时间把它写成文章，给遇到同样问题的朋友一些参考。<br />
我们知道，一个JAVA M-R job跑完后，无论是在命令行，还是在job的信息展示网页上，都会看到输出job counter的信息。在网页上，通过点击job信息页中的&#34;counter&#34;链接就能看到。<br />
<span id="more-14101"></span><br />
<img decoding="async" alt="hadoop job info page" src="https://www.codelast.com/wp-content/uploads/2024/04/hadoop_job_info_page.png" style="width: 339px; height: 361px;" /></p>
<p>本来嘛，点进这个页面，会看到正常的counter数据，但是，出问题的情况下，点进去看到的是这种情况：<br />
<img decoding="async" alt="hadoop counter info error" src="https://www.codelast.com/wp-content/uploads/2024/04/hadoop_counter_error.png" style="width: 561px; height: 144px;" /><br />
同时，在shell命令行你也会发现，该job没有任何counter信息输出。<br />
从错误信息页上，你得不到关于错误的任何有效信息，那个&#34;Error Details&#34;里也没有。<br />
虽然counter无法显示，但M-R job是可以正常跑完、正常输出数据的。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
经过测试，我这个case的的问题是：在程序中添加了数量比较多的Hadoop counter造成的。<br />
什么算多？我不知道。我的程序里有240多个counter就出问题了，当我把counter缩减了一半，最终只有120多个counter的时候，counter信息统计就正常了。<br />
如果你遇到了类似问题，可以首先检查一下job中的counter数量是否太多。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;版权声明&#160;<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&#160;<br />
感谢关注我的微信公众号（微信扫一扫）：<br />
<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" />&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-map-reduce-job%e7%9a%84counter%e9%a1%b5%e9%9d%a2%e6%97%a0%e6%b3%95%e6%98%be%e7%a4%ba%e7%9a%84%e9%97%ae%e9%a2%98error-500/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>这个问题已经不是第一次遇到了，只不过今天痛下决心花费不少时间把它写成文章，给遇到同样问题的朋友一些参考。<br />
我们知道，一个JAVA M-R job跑完后，无论是在命令行，还是在job的信息展示网页上，都会看到输出job counter的信息。在网页上，通过点击job信息页中的&quot;counter&quot;链接就能看到。<br />
<span id="more-14101"></span><br />
<img decoding="async" alt="hadoop job info page" src="https://www.codelast.com/wp-content/uploads/2024/04/hadoop_job_info_page.png" style="width: 339px; height: 361px;" /></p>
<p>本来嘛，点进这个页面，会看到正常的counter数据，但是，出问题的情况下，点进去看到的是这种情况：<br />
<img decoding="async" alt="hadoop counter info error" src="https://www.codelast.com/wp-content/uploads/2024/04/hadoop_counter_error.png" style="width: 561px; height: 144px;" /><br />
同时，在shell命令行你也会发现，该job没有任何counter信息输出。<br />
从错误信息页上，你得不到关于错误的任何有效信息，那个&quot;Error Details&quot;里也没有。<br />
虽然counter无法显示，但M-R job是可以正常跑完、正常输出数据的。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
经过测试，我这个case的的问题是：在程序中添加了数量比较多的Hadoop counter造成的。<br />
什么算多？我不知道。我的程序里有240多个counter就出问题了，当我把counter缩减了一半，最终只有120多个counter的时候，counter信息统计就正常了。<br />
如果你遇到了类似问题，可以首先检查一下job中的counter数量是否太多。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：<br />
<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-map-reduce-job%e7%9a%84counter%e9%a1%b5%e9%9d%a2%e6%97%a0%e6%b3%95%e6%98%be%e7%a4%ba%e7%9a%84%e9%97%ae%e9%a2%98error-500/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] JAVA map-reduce job中，reduce()方法漏写 @Override 注解引起的问题</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-map-reduce-job%e4%b8%ad%ef%bc%8creduce%e6%96%b9%e6%b3%95%e6%bc%8f%e5%86%99-override-%e6%b3%a8%e8%a7%a3%e5%bc%95%e8%b5%b7%e7%9a%84%e9%97%ae%e9%a2%98/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-map-reduce-job%e4%b8%ad%ef%bc%8creduce%e6%96%b9%e6%b3%95%e6%bc%8f%e5%86%99-override-%e6%b3%a8%e8%a7%a3%e5%bc%95%e8%b5%b7%e7%9a%84%e9%97%ae%e9%a2%98/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sun, 06 Aug 2023 12:12:10 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[map-reduce job]]></category>
		<category><![CDATA[类型错误]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13934</guid>

					<description><![CDATA[<p>有一个JAVA写的map-reduce&#160;job，mapper输出的key、value类型分别为Text、NullWritable，所以reducer应该像下面这样写：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&#160;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">QuerySegmentResultFromKVReducer</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">extends</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Reducer</span>&#60;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Text</span>,&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">NullWritable</span>,&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">NullWritable</span>,&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">NullWritable</span>&#62;&#160;</span>{

&#160;&#160;<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Override</span>
&#160;&#160;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">protected</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">setup</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(Reducer.Context&#160;context)</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&#160;IOException,&#160;InterruptedException&#160;</span>{
&#160;&#160;}

&#160;&#160;<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Override</span>
&#160;&#160;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">protected</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">cleanup</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(Reducer.Context&#160;context)</span></span></code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-map-reduce-job%e4%b8%ad%ef%bc%8creduce%e6%96%b9%e6%b3%95%e6%bc%8f%e5%86%99-override-%e6%b3%a8%e8%a7%a3%e5%bc%95%e8%b5%b7%e7%9a%84%e9%97%ae%e9%a2%98/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>有一个JAVA写的map-reduce&nbsp;job，mapper输出的key、value类型分别为Text、NullWritable，所以reducer应该像下面这样写：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">QuerySegmentResultFromKVReducer</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">extends</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Reducer</span>&lt;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Text</span>,&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">NullWritable</span>,&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">NullWritable</span>,&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">NullWritable</span>&gt;&nbsp;</span>{

&nbsp;&nbsp;<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Override</span>
&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">protected</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">setup</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(Reducer.Context&nbsp;context)</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;IOException,&nbsp;InterruptedException&nbsp;</span>{
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Override</span>
&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">protected</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">cleanup</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(Reducer.Context&nbsp;context)</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;IOException,&nbsp;InterruptedException&nbsp;</span>{
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); overflow-wrap: inherit !important; word-break: inherit !important;">@Override</span>
&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">protected</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">reduce</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(Text&nbsp;key,&nbsp;Iterable&lt;NullWritable&gt;&nbsp;values,&nbsp;Context&nbsp;context)</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;IOException,&nbsp;InterruptedException&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">TODO:</span></span>
&nbsp;&nbsp;}
}
</code></pre>
</section>
<p>在这里，reducer输出的key、value类型都是NullWritable，我们不用关心，这不是本文的关注点。<br />
<span id="more-13934"></span><br />
如果reduce()方法漏掉了&nbsp;<span style="color:#ff0000;">@Override</span>&nbsp;注解，并且把&nbsp; Reducer&lt;Text, NullWritable, NullWritable, NullWritable&gt;&nbsp;错误地写成了&nbsp;Reducer&lt;Text, Text, NullWritable, NullWritable&gt;，会发现编译并不报错。<br />
但是，当你跑这个job的时候，诡异的事情就来了。你会发现，你在&ldquo;TODO:&rdquo;那里写的reduce逻辑并没有执行，即使没有用 context.write()&nbsp;方法把任何数据输出到HDFS上，Hadoop counter仍然显示该job输出了和reducer输入一样多的数据。<br />
从现象上看，就像是执行了一个默认的Reducer，把reducer的输入数据原样输出。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
所以这里不得不强调，一定不要漏写&nbsp;<span style="color: rgb(255, 0, 0);">@Override</span>&nbsp;注解！有这个注解的时候，IDE就会提示错误，编译就会失败！</p>
<div>
	@Override 注解是可选的，如果删除了它，编译器不会报错，因为在 JAVA 中，重写一个方法时不使用 @Override 注解也是允许的。</div>
<div>
	&nbsp;</div>
<div>
	但是，建议在重写父类或接口中的方法时使用 @Override 注解。这样做有以下几个好处：</div>
<div>
	➤ 增加代码可读性：通过使用 @Override 注解，其他开发人员可以清楚地知道该方法是对父类或接口中的方法进行重写的，代码更易于理解。</div>
<div>
	➤ 防止错误：如果你错误地拼写了要重写的方法名，或者方法签名不正确，编译器会给出错误提示，帮助你发现潜在的问题。</div>
<div>
	➤ 保证代码的健壮性：如果父类或接口中的方法发生了变化，使用 @Override 注解的方法会在编译时产生错误，提醒你需要更新重写的方法。</div>
<p>
	在本文的例子中，如果reduce()方法没有写 @Override 注解，那么当reducer类错误地定义成了extends Reducer&lt;Text, Text, NullWritable, NullWritable&gt;的时候，IDE并不会发现reduce()方法有错，从而让你误以为一切正常。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：<br />
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
	以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-map-reduce-job%e4%b8%ad%ef%bc%8creduce%e6%96%b9%e6%b3%95%e6%bc%8f%e5%86%99-override-%e6%b3%a8%e8%a7%a3%e5%bc%95%e8%b5%b7%e7%9a%84%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 用JAVA读取本地的TFRecord文件</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84tfrecord%e6%96%87%e4%bb%b6/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84tfrecord%e6%96%87%e4%bb%b6/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 24 Apr 2023 18:09:06 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[TFRecord]]></category>
		<category><![CDATA[本地]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13895</guid>

					<description><![CDATA[<div>
	TFRecord是一种用于TensorFlow的二进制数据格式，它可以更高效地存储和读取大规模数据集。TFRecord文件包含了一系列记录（record），每个记录可以是一个张量（tensor）或者一个序列（sequence）。</div>
<div>
	与文本文件不同，TFRecord文件被编码成二进制格式，这使得它们更易于在网络上传输和存储。同时，TFRecord也允许我们将大型数据集分割成多个部分，并且可以有效地并行读取和处理这些部分。</div>
<div>
	在TensorFlow中，我们通常使用TFRecord文件来存储和加载模型的训练数据、验证数据、测试数据等。创建TFRecord文件需要经过一定的序列化操作，但这些操作很容易实现，因为TensorFlow提供了相应的API支持。</div>
<p><span id="more-13895"></span><br />
在大数据处理流程中，TFRecord文件通常是由map-reduce&#160;job生成的，数据量通常很大。有时为了验证文件内容正确，我们需要取少量数据来检查，例如，我们可以拿map-reduce job生成的N个TFRecord文件中的一个，在本地解析出来，打印出其中的内容看是否正确。<br />
下面就是一个用JAVA程序读取TFRecord文件并打印出其中一个Example的例子：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="javascript language-javascript hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&#160;&#160;&#160;&#160;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">String</span>&#160;localTfRecordFile&#160;=&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;/path/to/your/tfrecord/file&#34;</span>;
&#160;&#160;&#160;&#160;InputStream&#160;inputStream&#160;=&#160;Files.newInputStream(Paths.get(localTfRecordFile));
&#160;&#160;&#160;&#160;DataInput&#160;dataInput&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;DataInputStream(inputStream);
&#160;&#160;&#160;&#160;TFRecordReader&#160;reader&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;TFRecordReader(dataInput,&#160;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">true</span>);

&#160;&#160;&#160;&#160;byte[]&#160;recordBytes&#160;=&#160;reader.read();
&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&#160;(recordBytes&#160;!=&#160;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;Example&#160;example&#160;=&#160;Example.parseFrom(recordBytes);
&#160;&#160;&#160;&#160;&#160;&#160;System.out.println(example.toString());
&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">break</span>;&#160;&#160;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&#160;只打印一个Example</span>
&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;inputStream.close();
</code></pre>
</section>
<p>唯一需要注意的就是一个引入：import java.nio.file.Paths;<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
再详细说明一下：</p>
<div>
	TFRecord文件和Example是TensorFlow中用于数据序列化和存储的两个概念，它们之间有着紧密的关系。</div>
<div>
	TFRecord是一种二进制格式的文件，在TensorFlow中被用来高效地存储大量的数据。它通常是由多个Example组成的序列化数据。而Example则是TensorFlow中序列化数据的标准格式，可以包含多个Features，每个Feature又包含一个Tensor（可以是张量、字符串等）。在将数据写入TFRecord文件时，需要将其封装为Example格式；在读取TFRecord文件时，也需要将其中的每个Example解析出来。</div>
<div>
	简而言之，TFRecord文件就像是一个容器，而Example则是这个容器里面每个元素的具体格式。在使用TFRecord时，我们通常会先定义好我们要存储哪些数据以及这些数据应该怎么被划分为不同的Features，并封装成一个或多个Example，在把这些Example写入到TFRecord文件中。
<p>
		<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a></p></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84tfrecord%e6%96%87%e4%bb%b6/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<div>
	TFRecord是一种用于TensorFlow的二进制数据格式，它可以更高效地存储和读取大规模数据集。TFRecord文件包含了一系列记录（record），每个记录可以是一个张量（tensor）或者一个序列（sequence）。</div>
<div>
	与文本文件不同，TFRecord文件被编码成二进制格式，这使得它们更易于在网络上传输和存储。同时，TFRecord也允许我们将大型数据集分割成多个部分，并且可以有效地并行读取和处理这些部分。</div>
<div>
	在TensorFlow中，我们通常使用TFRecord文件来存储和加载模型的训练数据、验证数据、测试数据等。创建TFRecord文件需要经过一定的序列化操作，但这些操作很容易实现，因为TensorFlow提供了相应的API支持。</div>
<p><span id="more-13895"></span><br />
在大数据处理流程中，TFRecord文件通常是由map-reduce&nbsp;job生成的，数据量通常很大。有时为了验证文件内容正确，我们需要取少量数据来检查，例如，我们可以拿map-reduce job生成的N个TFRecord文件中的一个，在本地解析出来，打印出其中的内容看是否正确。<br />
下面就是一个用JAVA程序读取TFRecord文件并打印出其中一个Example的例子：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="javascript language-javascript hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">String</span>&nbsp;localTfRecordFile&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;/path/to/your/tfrecord/file&quot;</span>;
&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;inputStream&nbsp;=&nbsp;Files.newInputStream(Paths.get(localTfRecordFile));
&nbsp;&nbsp;&nbsp;&nbsp;DataInput&nbsp;dataInput&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;DataInputStream(inputStream);
&nbsp;&nbsp;&nbsp;&nbsp;TFRecordReader&nbsp;reader&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;TFRecordReader(dataInput,&nbsp;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">true</span>);

&nbsp;&nbsp;&nbsp;&nbsp;byte[]&nbsp;recordBytes&nbsp;=&nbsp;reader.read();
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&nbsp;(recordBytes&nbsp;!=&nbsp;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Example&nbsp;example&nbsp;=&nbsp;Example.parseFrom(recordBytes);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(example.toString());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">break</span>;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;只打印一个Example</span>
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;inputStream.close();
</code></pre>
</section>
<p>唯一需要注意的就是一个引入：import java.nio.file.Paths;<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
再详细说明一下：</p>
<div>
	TFRecord文件和Example是TensorFlow中用于数据序列化和存储的两个概念，它们之间有着紧密的关系。</div>
<div>
	TFRecord是一种二进制格式的文件，在TensorFlow中被用来高效地存储大量的数据。它通常是由多个Example组成的序列化数据。而Example则是TensorFlow中序列化数据的标准格式，可以包含多个Features，每个Feature又包含一个Tensor（可以是张量、字符串等）。在将数据写入TFRecord文件时，需要将其封装为Example格式；在读取TFRecord文件时，也需要将其中的每个Example解析出来。</div>
<div>
	简而言之，TFRecord文件就像是一个容器，而Example则是这个容器里面每个元素的具体格式。在使用TFRecord时，我们通常会先定义好我们要存储哪些数据以及这些数据应该怎么被划分为不同的Features，并封装成一个或多个Example，在把这些Example写入到TFRecord文件中。</p>
<p>
		<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
		<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
		转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
		感谢关注我的微信公众号（微信扫一扫）：<br />
		<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
		以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
		<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84tfrecord%e6%96%87%e4%bb%b6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 用JAVA程序读取本地的Hadoop sequence file</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e7%a8%8b%e5%ba%8f%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84hadoop-sequence-file/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e7%a8%8b%e5%ba%8f%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84hadoop-sequence-file/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 20 Apr 2023 12:23:18 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[Hadoop sequence file]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[本地]]></category>
		<category><![CDATA[读取]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13885</guid>

					<description><![CDATA[<div>
	Hadoop SequenceFile是Hadoop用于存储二进制键值对的文件格式。它支持存储不同的键值对类型,如:IntWritable/Text, NullWritable/BytesWritable等。</div>
<div>
	假设我的sequence file的key是BooleanWritable类型，value是Text类型，怎么读取它呢？<br />
	<span id="more-13885"></span></div>
<p>话不多说，直接上代码：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&#160;&#160;&#160;&#160;Configuration&#160;conf&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;Configuration();
&#160;&#160;&#160;&#160;FileSystem&#160;fs&#160;=&#160;FileSystem.getLocal(conf);
&#160;&#160;&#160;&#160;Path&#160;seqFilePath&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;Path(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;/path/to/your/sequence-file&#34;</span>);

&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&#160;打印出sequence&#160;file的key和value的类型</span>
&#160;&#160;&#160;&#160;&#160;&#160;SequenceFile.Reader&#160;reader&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;SequenceFile.Reader(fs,&#160;seqFilePath,&#160;conf);
&#160;&#160;&#160;&#160;&#160;&#160;Writable&#160;key&#160;=&#160;(Writable)&#160;reader.getKeyClass().newInstance();
&#160;&#160;&#160;&#160;&#160;&#160;Writable&#160;value&#160;=&#160;(Writable)&#160;reader.getValueClass().newInstance();
&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&#160;(reader.next(key,&#160;value))&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.out.println(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;Key&#160;type:&#160;&#34;</span>&#160;+&#160;key.getClass());
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.out.println(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;Value&#160;type:&#160;&#34;</span>&#160;+&#160;value.getClass());
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">break</span>;
&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;&#160;&#160;reader.close();
&#160;&#160;&#160;&#160;}

&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&#160;打印出sequence&#160;file的内容</span>
&#160;&#160;&#160;&#160;&#160;&#160;SequenceFile.Reader&#160;reader&#160;=&#160;</code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e7%a8%8b%e5%ba%8f%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84hadoop-sequence-file/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<div>
	Hadoop SequenceFile是Hadoop用于存储二进制键值对的文件格式。它支持存储不同的键值对类型,如:IntWritable/Text, NullWritable/BytesWritable等。</div>
<div>
	假设我的sequence file的key是BooleanWritable类型，value是Text类型，怎么读取它呢？<br />
	<span id="more-13885"></span></div>
<p>话不多说，直接上代码：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&nbsp;&nbsp;&nbsp;&nbsp;Configuration&nbsp;conf&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Configuration();
&nbsp;&nbsp;&nbsp;&nbsp;FileSystem&nbsp;fs&nbsp;=&nbsp;FileSystem.getLocal(conf);
&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;seqFilePath&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Path(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;/path/to/your/sequence-file&quot;</span>);

&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;打印出sequence&nbsp;file的key和value的类型</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SequenceFile.Reader&nbsp;reader&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;SequenceFile.Reader(fs,&nbsp;seqFilePath,&nbsp;conf);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Writable&nbsp;key&nbsp;=&nbsp;(Writable)&nbsp;reader.getKeyClass().newInstance();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Writable&nbsp;value&nbsp;=&nbsp;(Writable)&nbsp;reader.getValueClass().newInstance();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&nbsp;(reader.next(key,&nbsp;value))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Key&nbsp;type:&nbsp;&quot;</span>&nbsp;+&nbsp;key.getClass());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Value&nbsp;type:&nbsp;&quot;</span>&nbsp;+&nbsp;value.getClass());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">break</span>;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.close();
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;打印出sequence&nbsp;file的内容</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SequenceFile.Reader&nbsp;reader&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;SequenceFile.Reader(fs,&nbsp;seqFilePath),&nbsp;conf);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BooleanWritable&nbsp;keyObj&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;BooleanWritable();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text&nbsp;valueObj&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Text();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&nbsp;(reader.next(keyObj,&nbsp;valueObj))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(keyObj.get()&nbsp;+&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;:&nbsp;&quot;</span>&nbsp;+&nbsp;valueObj);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.close();
&nbsp;&nbsp;&nbsp;&nbsp;}
</code></pre>
</section>
<p>
输出类似于：</p>
<blockquote>
<div>
		Key type: class org.apache.hadoop.io.BooleanWritable</div>
<div>
		Value type: class org.apache.hadoop.io.Text<br />
		（文件内容此处省略）</div>
</blockquote>
<p>Maven依赖取决于你使用的Hadoop版本，下面是一个例子：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependencies</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>org.apache.hadoop<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>hadoop-common<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>3.2.1<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>org.apache.hadoop<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>hadoop-client<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>3.2.1<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>org.apache.hadoop<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>hadoop-hdfs<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>3.2.1<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependencies</span>&gt;</span></code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;关注不迷路&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;</p>
<p>
	感谢关注我的微信公众号（微信扫一扫）：<br />
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
	以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e7%a8%8b%e5%ba%8f%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84hadoop-sequence-file/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 通过第三方工具/JAVA程序访问S3存储</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%80%9a%e8%bf%87%e7%ac%ac%e4%b8%89%e6%96%b9%e5%b7%a5%e5%85%b7-java%e7%a8%8b%e5%ba%8f%e8%ae%bf%e9%97%aes3%e5%ad%98%e5%82%a8/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%80%9a%e8%bf%87%e7%ac%ac%e4%b8%89%e6%96%b9%e5%b7%a5%e5%85%b7-java%e7%a8%8b%e5%ba%8f%e8%ae%bf%e9%97%aes3%e5%ad%98%e5%82%a8/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 14 Nov 2022 16:32:43 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Amazon S3]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[s3-command-line]]></category>
		<category><![CDATA[对象存储]]></category>
		<category><![CDATA[访问]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13652</guid>

					<description><![CDATA[<p>S3是亚马逊2006年推出的简单存储服务（Simple Storage Service），理论上是一个全球存储区域网络，你可以把它想像成一个超大的硬盘，可以在其中存储和检索数字资产，通过 S3 存储和检索的资产被称为对象，对象存储在存储段（bucket）中。<br />
很多公司都推出了自己的对象存储服务，例如阿里云对象存储服务OSS，可以使用S3 API进行访问。<br />
<span id="more-13652"></span><br />
话不多说，我们看看如何读写S3。<br />
<span style="color:#ff0000;">➤</span> 通过第三方工具<br />
这种工具很多，<a href="https://github.com/lpflpf/s3-command-line" rel="noopener" target="_blank">s3-command-line</a>&#160;就是一个。这是一个GoLang开发的命令行工具，编译出来只有13MB。它支持文件的上传、下载、删除、列表，已经满足了大多数使用场景需求。<br />
checkout它的源码，在安装有Go语言环境的服务器上执行 go build&#160;即可编译出可执行程序 s3-command-line，这个只需要做一次，最终运行 s3-command-line&#160;软件的服务器上并不需要安装GoLang环境。<br />
在使用 s3-command-line&#160;之前，我们需要创建一个配置文件 <span style="color:#0000ff;">config.json</span>，填入我们访问S3所需的密钥等信息：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="json language-json hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">{
&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;id&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;...&#34;</span>,
&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;secret&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;...&#34;</span>,
&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;endPoint&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;http://...&#34;</span>,
&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;region&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;...&#34;</span></code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%80%9a%e8%bf%87%e7%ac%ac%e4%b8%89%e6%96%b9%e5%b7%a5%e5%85%b7-java%e7%a8%8b%e5%ba%8f%e8%ae%bf%e9%97%aes3%e5%ad%98%e5%82%a8/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>S3是亚马逊2006年推出的简单存储服务（Simple Storage Service），理论上是一个全球存储区域网络，你可以把它想像成一个超大的硬盘，可以在其中存储和检索数字资产，通过 S3 存储和检索的资产被称为对象，对象存储在存储段（bucket）中。<br />
很多公司都推出了自己的对象存储服务，例如阿里云对象存储服务OSS，可以使用S3 API进行访问。<br />
<span id="more-13652"></span><br />
话不多说，我们看看如何读写S3。<br />
<span style="color:#ff0000;">➤</span> 通过第三方工具<br />
这种工具很多，<a href="https://github.com/lpflpf/s3-command-line" rel="noopener" target="_blank">s3-command-line</a>&nbsp;就是一个。这是一个GoLang开发的命令行工具，编译出来只有13MB。它支持文件的上传、下载、删除、列表，已经满足了大多数使用场景需求。<br />
checkout它的源码，在安装有Go语言环境的服务器上执行 go build&nbsp;即可编译出可执行程序 s3-command-line，这个只需要做一次，最终运行 s3-command-line&nbsp;软件的服务器上并不需要安装GoLang环境。<br />
在使用 s3-command-line&nbsp;之前，我们需要创建一个配置文件 <span style="color:#0000ff;">config.json</span>，填入我们访问S3所需的密钥等信息：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="json language-json hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">{
&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;id&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;...&quot;</span>,
&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;secret&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;...&quot;</span>,
&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;endPoint&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;http://...&quot;</span>,
&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;region&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;...&quot;</span>
}
</code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
这些信息你可以在你使用的S3平台上找到。<br />
假设我们要读写的bucket名为 <span style="color:#0000ff;">my-bucket</span>，那么下面的命令会告诉你怎么使用 s3-command-line：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">列出指定&nbsp;bucket&nbsp;里的文件：
./s3-command-line&nbsp;ls&nbsp;-c&nbsp;config.json&nbsp;my-bucket&nbsp;/

从指定的&nbsp;bucket&nbsp;删除文件&nbsp;1.txt：
./s3-command-line&nbsp;rm&nbsp;-c&nbsp;config.json&nbsp;my-bucket&nbsp;1.txt

上传本地文件&nbsp;1.txt&nbsp;到指定的&nbsp;bucket：
./s3-command-line&nbsp;put&nbsp;-c&nbsp;config.json&nbsp;my-bucket&nbsp;1.txt

从指定的&nbsp;bucket&nbsp;下载文件&nbsp;1.txt&nbsp;到本地：
./s3-command-line&nbsp;get&nbsp;-c&nbsp;config.json&nbsp;my-bucket&nbsp;1.txt
</code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
总结：简单好用，编译成可执行程序后没有对其他library的依赖，令人舒适。</p>
<p><span style="color: rgb(255, 0, 0);">➤</span>&nbsp;自己写JAVA程序<br />
JDK版本：1.8.0_221，Maven：3.6.3<br />
首先你需要在 pom.xml&nbsp;中添加相关依赖（参考<a href="https://github.com/aws/aws-sdk-java-v2/#using-the-sdk" rel="noopener" target="_blank">这里</a>）：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>software.amazon.awssdk<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>aws-sdk-java<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>
&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>2.18.16<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>
<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span></code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
然后就可以开始写代码了（测试可用）：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">package</span>&nbsp;com.codelast;


<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;lombok.extern.slf4j.Slf4j;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;org.apache.commons.lang.StringUtils;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;org.kohsuke.args4j.CmdLineException;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;org.kohsuke.args4j.CmdLineParser;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;org.kohsuke.args4j.Option;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.core.sync.RequestBody;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.regions.Region;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.S3Client;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.model.PutObjectRequest;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.model.PutObjectResponse;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;software.amazon.awssdk.services.s3.model.S3Exception;

<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.io.File;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.io.FileInputStream;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.io.IOException;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.net.URI;

<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">/**
&nbsp;*&nbsp;S3存储操作类。
&nbsp;*&nbsp;参考：https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/s3/src/main/java/com/example/s3/PutObject.java
&nbsp;*
&nbsp;*&nbsp;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@author</span>&nbsp;DarranZhang@codelast.com
&nbsp;*&nbsp;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@version</span>&nbsp;2022-11-14
&nbsp;*/
</span>
<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Slf</span>4j
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">S3Operator</span>&nbsp;</span>{
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;ACCESS_KEY&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;your_key&quot;</span>;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;SECRET_KEY&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;your_secret&quot;</span>;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;END_POINT&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;http://your_end_point_url&quot;</span>;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;REGION&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;your_region&quot;</span>;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;String&nbsp;BUCKET_NAME&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;your_bucket_name&quot;</span>;

&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Options</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Option</span>(name&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;--localFile&quot;</span>,&nbsp;usage&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;the&nbsp;path&nbsp;of&nbsp;the&nbsp;local&nbsp;file&nbsp;to&nbsp;upload&nbsp;to&nbsp;S3&quot;</span>,&nbsp;required&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">true</span>)
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;localFile;
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String[]&nbsp;args)</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;Exception&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;Options&nbsp;opts&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Options();
&nbsp;&nbsp;&nbsp;&nbsp;CmdLineParser&nbsp;parser&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;CmdLineParser(opts);
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parser.parseArgument(args);
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(CmdLineException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.error(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;command&nbsp;parse&nbsp;fail&quot;</span>,&nbsp;e);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.exit(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1</span>);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;S3Operator&nbsp;operator&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;S3Operator();
&nbsp;&nbsp;&nbsp;&nbsp;operator.upload(opts.localFile);
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">upload</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String&nbsp;localFile)</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;Exception&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;AwsBasicCredentials&nbsp;credentials&nbsp;=&nbsp;AwsBasicCredentials.create(ACCESS_KEY,&nbsp;SECRET_KEY);
&nbsp;&nbsp;&nbsp;&nbsp;Region&nbsp;region&nbsp;=&nbsp;Region.of(REGION);
&nbsp;&nbsp;&nbsp;&nbsp;S3Client&nbsp;s3Client&nbsp;=&nbsp;S3Client.builder()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.endpointOverride(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;URI(END_POINT))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.region(region)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.credentialsProvider(StaticCredentialsProvider.create(credentials))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build();

&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;s3FileName&nbsp;=&nbsp;StringUtils.substringAfterLast(localFile,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;/&quot;</span>);&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;上传到S3之后的文件名，不一定要和本地文件名一致，但为了直观，这里保持一致</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(StringUtils.isEmpty(s3FileName))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;msg&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;local&nbsp;file&nbsp;name&nbsp;is&nbsp;empty&quot;</span>;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.error(msg);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">throw</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;RuntimeException(msg);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;每次上传前，不用先删除再上传，因为最新上传的同名文件会覆盖之前的文件</span>
&nbsp;&nbsp;&nbsp;&nbsp;putObject(s3Client,&nbsp;BUCKET_NAME,&nbsp;s3FileName,&nbsp;localFile);
&nbsp;&nbsp;&nbsp;&nbsp;s3Client.close();
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">putObject</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(S3Client&nbsp;s3Client,&nbsp;String&nbsp;bucketName,&nbsp;String&nbsp;objectKey,&nbsp;String&nbsp;objectPath)</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PutObjectRequest&nbsp;request&nbsp;=&nbsp;PutObjectRequest.builder().bucket(bucketName).key(objectKey).build();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PutObjectResponse&nbsp;response&nbsp;=&nbsp;s3Client.putObject(request,&nbsp;RequestBody.fromBytes(getObjectFile(objectPath)));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.info(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;successfully&nbsp;upload&nbsp;file&nbsp;[{}]&nbsp;to&nbsp;S3,&nbsp;entity&nbsp;tag:&nbsp;[{}]&quot;</span>,&nbsp;objectPath,&nbsp;response.eTag());
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(S3Exception&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.error(String.format(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;failed&nbsp;to&nbsp;upload&nbsp;file&nbsp;[%s]&nbsp;to&nbsp;S3&quot;</span>,&nbsp;objectPath),&nbsp;e);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">byte</span>[]&nbsp;getObjectFile(String&nbsp;filePath)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;FileInputStream&nbsp;fileInputStream&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">byte</span>[]&nbsp;bytesArray&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;file&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;File(filePath);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bytesArray&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">byte</span>[(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">int</span>)&nbsp;file.length()];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileInputStream&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;FileInputStream(file);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileInputStream.read(bytesArray);
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(IOException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">finally</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(fileInputStream&nbsp;!=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileInputStream.close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(IOException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;bytesArray;
&nbsp;&nbsp;}

&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">deleteObject</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(S3Client&nbsp;s3,&nbsp;String&nbsp;bucket,&nbsp;String&nbsp;s3FileName)</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;DeleteObjectRequest&nbsp;request&nbsp;=&nbsp;DeleteObjectRequest.builder().bucket(bucket).key(s3FileName).build();
&nbsp;&nbsp;&nbsp;&nbsp;s3.deleteObject(request);
&nbsp;&nbsp;}
}
</code></pre>
</section>
<p> 这样我们就实现了用JAVA访问S3，不过，如果你仔细看会发现，当引入S3的依赖后，你的项目编译出的 fat jar&nbsp;体积剧增了400MB左右！这也太恶心了...<br />
当然，上面引入的 whole-SDK 依赖，是我为了省事引入了所有S3相关的模块，或许你有些用不到的模块，可以不用引入 whole-SDK，而是分别引入单独的模块，但我有空去想这些玩意的话，还不如使用 s3-command-line&nbsp;简单呢，不是吗？毕竟时间就是生命，别在这些无聊的事上浪费生命。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤</span>&nbsp;总结<br />
如果你操作S3时只使用上传、下载、删除、列表功能，那么建议不要自己写代码，使用 s3-command-line&nbsp;更香。</p>
<p>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：<br />
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
	以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e9%80%9a%e8%bf%87%e7%ac%ac%e4%b8%89%e6%96%b9%e5%b7%a5%e5%85%b7-java%e7%a8%8b%e5%ba%8f%e8%ae%bf%e9%97%aes3%e5%ad%98%e5%82%a8/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 使用Google HTTP Client/OkHttpClient来发送同步POST请求(JAVA)</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8google-http-client-okhttpclient%e6%9d%a5%e5%8f%91%e9%80%81%e5%90%8c%e6%ad%a5post%e8%af%b7%e6%b1%82java/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8google-http-client-okhttpclient%e6%9d%a5%e5%8f%91%e9%80%81%e5%90%8c%e6%ad%a5post%e8%af%b7%e6%b1%82java/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sun, 13 Nov 2022 04:47:47 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[Google HTTP Client]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[OkHttpClient]]></category>
		<category><![CDATA[POST]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13673</guid>

					<description><![CDATA[<p>这是一篇很水的文章，纯粹为了记录一些代码。<br />
<a href="https://github.com/googleapis/google-http-java-client" rel="noopener" target="_blank">Google HTTP Client Library for Java</a>&#160;和&#160;<a href="https://github.com/square/okhttp" rel="noopener" target="_blank">OkHttpClient</a>&#160;是两个很流行的http client JAVA库。<br />
下面来看看用它们分别怎样通过POST请求把JSON数据发送到服务器。<br />
JDK版本：1.8.0_221<br />
<span id="more-13673"></span><br />
<span style="color:#ff0000;">➤</span> Google HTTP Client</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&#160;String&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">performHttpPost</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String&#160;url,&#160;String&#160;payload,&#160;Map&#60;String,&#160;String&#62;&#160;headers)</span>&#160;</span>{
&#160;&#160;HttpRequestFactory&#160;requestFactory&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;NetHttpTransport().createRequestFactory();
&#160;&#160;HttpRequest&#160;request;
&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&#160;{
&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">byte</span>[]&#160;contentBytes&#160;=&#160;payload.getBytes(Charsets.UTF_8);
&#160;&#160;&#160;&#160;HttpContent&#160;content&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;ByteArrayContent(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;application/json&#34;</span>,&#160;contentBytes);
&#160;&#160;&#160;&#160;request&#160;=&#160;requestFactory.buildPostRequest(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;GenericUrl(url),&#160;content);
&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&#160;(MapUtils.isNotEmpty(headers))&#160;{</code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8google-http-client-okhttpclient%e6%9d%a5%e5%8f%91%e9%80%81%e5%90%8c%e6%ad%a5post%e8%af%b7%e6%b1%82java/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>这是一篇很水的文章，纯粹为了记录一些代码。<br />
<a href="https://github.com/googleapis/google-http-java-client" rel="noopener" target="_blank">Google HTTP Client Library for Java</a>&nbsp;和&nbsp;<a href="https://github.com/square/okhttp" rel="noopener" target="_blank">OkHttpClient</a>&nbsp;是两个很流行的http client JAVA库。<br />
下面来看看用它们分别怎样通过POST请求把JSON数据发送到服务器。<br />
JDK版本：1.8.0_221<br />
<span id="more-13673"></span><br />
<span style="color:#ff0000;">➤</span> Google HTTP Client</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;String&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">performHttpPost</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String&nbsp;url,&nbsp;String&nbsp;payload,&nbsp;Map&lt;String,&nbsp;String&gt;&nbsp;headers)</span>&nbsp;</span>{
&nbsp;&nbsp;HttpRequestFactory&nbsp;requestFactory&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;NetHttpTransport().createRequestFactory();
&nbsp;&nbsp;HttpRequest&nbsp;request;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">byte</span>[]&nbsp;contentBytes&nbsp;=&nbsp;payload.getBytes(Charsets.UTF_8);
&nbsp;&nbsp;&nbsp;&nbsp;HttpContent&nbsp;content&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;ByteArrayContent(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;application/json&quot;</span>,&nbsp;contentBytes);
&nbsp;&nbsp;&nbsp;&nbsp;request&nbsp;=&nbsp;requestFactory.buildPostRequest(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;GenericUrl(url),&nbsp;content);
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(MapUtils.isNotEmpty(headers))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;(Map.Entry&lt;String,&nbsp;String&gt;&nbsp;entry&nbsp;:&nbsp;headers.entrySet())&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request.getHeaders().set(entry.getKey(),&nbsp;entry.getValue());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;request.execute().parseAsString();
&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(IOException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;log.error(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;failed&nbsp;to&nbsp;send&nbsp;POST&nbsp;request&quot;</span>,&nbsp;e);
&nbsp;&nbsp;}
}
</code></pre>
</section>
<p>其中，payLoad是你要发送的JSON数据字符串，headers是要设置到请求头中的key-value对。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤</span>&nbsp;OkHttpClient</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;String&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">performHttpPost</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String&nbsp;url,&nbsp;String&nbsp;payload)</span>&nbsp;</span>{
&nbsp;&nbsp;HttpUrl.Builder&nbsp;builder&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;HttpUrl&nbsp;httpUrl&nbsp;=&nbsp;HttpUrl.parse(url);
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(httpUrl&nbsp;!=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;builder&nbsp;=&nbsp;httpUrl.newBuilder();
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(IllegalArgumentException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;log.error(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;failed&nbsp;to&nbsp;create&nbsp;HttpUrl.Builder&nbsp;from&nbsp;url:&nbsp;{}&quot;</span>,&nbsp;url,&nbsp;e);
&nbsp;&nbsp;}
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(builder&nbsp;==&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;
&nbsp;&nbsp;}
&nbsp;&nbsp;OkHttpClient&nbsp;client&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;OkHttpClient();
&nbsp;&nbsp;RequestBody&nbsp;requestBody&nbsp;=&nbsp;RequestBody.create(MediaType.parse(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;application/json;&nbsp;charset=utf-8&quot;</span>),&nbsp;payload);
&nbsp;&nbsp;Request&nbsp;request&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Request.Builder()
&nbsp;&nbsp;&nbsp;&nbsp;.url(builder.build())
&nbsp;&nbsp;&nbsp;&nbsp;.post(requestBody)
&nbsp;&nbsp;&nbsp;&nbsp;.build();
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;Response&nbsp;response&nbsp;=&nbsp;client.newCall(request).execute();
&nbsp;&nbsp;&nbsp;&nbsp;ResponseBody&nbsp;body&nbsp;=&nbsp;response.body();
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(body&nbsp;==&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.error(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;null&nbsp;response&nbsp;body&quot;</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;body.string();
&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(IOException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;log.error(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;failed&nbsp;to&nbsp;send&nbsp;POST&nbsp;request&quot;</span>,&nbsp;e);
&nbsp;&nbsp;}
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>;
}
</code></pre>
</section>
<p> 其中，payLoad是你要发送的JSON数据字符串。</p>
<p>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：<br />
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
	以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8google-http-client-okhttpclient%e6%9d%a5%e5%8f%91%e9%80%81%e5%90%8c%e6%ad%a5post%e8%af%b7%e6%b1%82java/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] JAVA Jackson解析无名(匿名)数组</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-jackson%e8%a7%a3%e6%9e%90%e6%97%a0%e5%90%8d%e5%8c%bf%e5%90%8d%e6%95%b0%e7%bb%84/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-jackson%e8%a7%a3%e6%9e%90%e6%97%a0%e5%90%8d%e5%8c%bf%e5%90%8d%e6%95%b0%e7%bb%84/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 03 May 2022 05:18:13 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[jackson]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[匿名]]></category>
		<category><![CDATA[无名]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13557</guid>

					<description><![CDATA[<p>假设有JSON文件 1.json：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="json language-json hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">[
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;sku&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;ABC&#34;</span>,
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;num&#34;</span>:&#160;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">3</span>
&#160;&#160;&#160;&#160;},
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;sku&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;DEF&#34;</span>,
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;num&#34;</span>:&#160;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">2</span>
&#160;&#160;&#160;&#160;}
]
</code></pre>
</section>
<p>由中括号可知，这个JSON字符串映射成JAVA对象是一个List，而它里面又包含了两个JAVA对象（每个大括号对应一个）。<br />
<span id="more-13557"></span><br />
假设JSON字符串&#160;<span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;">{ </span><span style="font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&#34;sku&#34;</span><span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre; color: rgb(163, 21, 21);">&#34;ABC&#34;</span><span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;">, </span><span style="font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&#34;num&#34;</span><span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre; color: rgb(9, 134, 88);">3</span><span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;"> }</span>&#160;映射成的POJO为：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&#160;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">SkuInfo</span>&#160;</span>{
&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&#160;String&#160;sku;
&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">int</span>&#160;num;
}
</code></pre>
</section>
<p>那么，应该怎么用Jackson库来解析原JSON？<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-jackson%e8%a7%a3%e6%9e%90%e6%97%a0%e5%90%8d%e5%8c%bf%e5%90%8d%e6%95%b0%e7%bb%84/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>假设有JSON文件 1.json：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="json language-json hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">[
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;sku&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ABC&quot;</span>,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;num&quot;</span>:&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">3</span>
&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;sku&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;DEF&quot;</span>,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;num&quot;</span>:&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">2</span>
&nbsp;&nbsp;&nbsp;&nbsp;}
]
</code></pre>
</section>
<p>由中括号可知，这个JSON字符串映射成JAVA对象是一个List，而它里面又包含了两个JAVA对象（每个大括号对应一个）。<br />
<span id="more-13557"></span><br />
假设JSON字符串&nbsp;<span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">{ </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&quot;sku&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(163, 21, 21);">&quot;ABC&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">, </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&quot;num&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(9, 134, 88);">3</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;"> }</span>&nbsp;映射成的POJO为：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">SkuInfo</span>&nbsp;</span>{
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;String&nbsp;sku;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;num;
}
</code></pre>
</section>
<p>那么，应该怎么用Jackson库来解析原JSON？<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
由于JSON字符串&nbsp;<span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">{ </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&quot;sku&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(163, 21, 21);">&quot;ABC&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">, </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&quot;num&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(9, 134, 88);">3</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;"> }</span>&nbsp;是无名（匿名）的，所以要用这样的方法来解析它：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;ObjectMapper&nbsp;mapper&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;ObjectMapper();
SkuInfo[]&nbsp;skuInfos&nbsp;=&nbsp;mapper.readValue(FileUtils.readFileToString(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;File(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;1.json&quot;</span>),&nbsp;Charsets.UTF_8),&nbsp;SkuInfo[].class);
</code></pre>
</section>
<p>或者你不想把JSON解析成一个JAVA数组，而是一个List的话，可以这么做：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">List&lt;SkuInfo&gt;&nbsp;skuInfos&nbsp;=&nbsp;MAPPER.readValue(FileUtils.readFileToString(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;File(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;1.json&quot;</span>),&nbsp;Charsets.UTF_8),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;TypeReference&lt;List&lt;SkuInfo&gt;&gt;()&nbsp;{});
</code></pre>
</section>
<p>
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：<br />
<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-jackson%e8%a7%a3%e6%9e%90%e6%97%a0%e5%90%8d%e5%8c%bf%e5%90%8d%e6%95%b0%e7%bb%84/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] selenium-java的wait.until(ExpectedConditions)编译出错问题</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-selenium-java%e7%9a%84wait-untilexpectedconditions%e7%bc%96%e8%af%91%e5%87%ba%e9%94%99%e9%97%ae%e9%a2%98/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-selenium-java%e7%9a%84wait-untilexpectedconditions%e7%bc%96%e8%af%91%e5%87%ba%e9%94%99%e9%97%ae%e9%a2%98/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 25 Aug 2020 07:52:47 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[ExpectedConditions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Selenium]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=12876</guid>

					<description><![CDATA[<p>代码示例：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">wait.until(ExpectedConditions.alertIsPresent());
Alert&#160;alert&#160;=&#160;driver.switchTo().alert();
alert.accept();
</code></pre>
</section>
<p>在升级了pom.xml中的selenium-java版本到3.141.59后，代码编译出错了（挂在第一行），错误信息：</p>
<blockquote>
<div>
		无法将类 org.openqa.selenium.support.ui.FluentWait&#60;T&#62;中的方法 until应用到给定类型;</div>
<div>
		[ERROR]&#160; &#160;需要: java.util.function.Function&#60;? super org.openqa.selenium.WebDriver,V&#62;</div>
<div>
		[ERROR]&#160; &#160;找到: org.openqa.selenium.support.ui.ExpectedCondition&#60;org.openqa.selenium.Alert&#62;</div>
<div>
		[ERROR]&#160; &#160;原因: 无法推断类型变量 V</div>
<div>
		[ERROR]&#160; &#160; &#160;(参数不匹配; org.openqa.selenium.support.ui.ExpectedCondition&#60;org.openqa.selenium.Alert&#62;无法转换为java.util.function.Function&#60;? super org.openqa.selenium.WebDriver,V&#62;)</div>
</blockquote>
<div>
<span id="more-12876"></span>	<br />
	原因：pom.xml里如下依赖版本太低：
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&#62;</span>
&#160;&#160;&#160;&#160;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&#62;</span>com.google.guava<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&#62;</span>
&#160;&#160;&#160;&#160;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&#62;</span>guava<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&#62;</span>
&#160;&#160;&#160;&#160;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&#62;</span>19.0<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&#62;</span>
<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&#62;</span></code></pre>
</section>
<p>
	改成：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&#62;</span>
&#160;&#160;&#160;&#160;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&#60;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&#62;</span>com.google.guava</code></pre></section></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-selenium-java%e7%9a%84wait-untilexpectedconditions%e7%bc%96%e8%af%91%e5%87%ba%e9%94%99%e9%97%ae%e9%a2%98/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>代码示例：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">wait.until(ExpectedConditions.alertIsPresent());
Alert&nbsp;alert&nbsp;=&nbsp;driver.switchTo().alert();
alert.accept();
</code></pre>
</section>
<p>在升级了pom.xml中的selenium-java版本到3.141.59后，代码编译出错了（挂在第一行），错误信息：</p>
<blockquote>
<div>
		无法将类 org.openqa.selenium.support.ui.FluentWait&lt;T&gt;中的方法 until应用到给定类型;</div>
<div>
		[ERROR]&nbsp; &nbsp;需要: java.util.function.Function&lt;? super org.openqa.selenium.WebDriver,V&gt;</div>
<div>
		[ERROR]&nbsp; &nbsp;找到: org.openqa.selenium.support.ui.ExpectedCondition&lt;org.openqa.selenium.Alert&gt;</div>
<div>
		[ERROR]&nbsp; &nbsp;原因: 无法推断类型变量 V</div>
<div>
		[ERROR]&nbsp; &nbsp; &nbsp;(参数不匹配; org.openqa.selenium.support.ui.ExpectedCondition&lt;org.openqa.selenium.Alert&gt;无法转换为java.util.function.Function&lt;? super org.openqa.selenium.WebDriver,V&gt;)</div>
</blockquote>
<div>
<span id="more-12876"></span>	<br />
	原因：pom.xml里如下依赖版本太低：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>com.google.guava<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>guava<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>19.0<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>
<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span></code></pre>
</section>
<p>
	改成：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>com.google.guava<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>guava<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>21.0<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>
<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span></code></pre>
</section>
<p>即可解决问题。</p></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-selenium-java%e7%9a%84wait-untilexpectedconditions%e7%bc%96%e8%af%91%e5%87%ba%e9%94%99%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] Hadoop 2.6.x 下Distributed Cache的用法</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-hadoop-2-6-x-%e4%b8%8bdistributed-cache%e7%9a%84%e7%94%a8%e6%b3%95/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-hadoop-2-6-x-%e4%b8%8bdistributed-cache%e7%9a%84%e7%94%a8%e6%b3%95/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 28 Jul 2020 03:27:51 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Distributed Cache]]></category>
		<category><![CDATA[hadoop]]></category>
		<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=12744</guid>

					<description><![CDATA[<p>仔细记录一下Java的Map-Reduce job使用distributed cache的方法，毕竟以前一直都是copy paste ~</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&#160;适用的Hadoop版本<br />
CDH 5.8.0（Hadoop 2.6.0）<br />
别的版本没有测试过，但后面相近的版本应该也能用。</p>
<p><span style="background-color:#ffff00;">✓</span>&#160;准备工作：上传本地文件到HDFS<br />
为了在Java代码中把一个文件加入 distributed cache，需要先把它上传到HDFS，之后应使用 HDFS 路径来加入 distributed cache。<br />
假设要加入 distributed cache 的文件为 file.txt：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="javascript language-javascript hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">hadoop&#160;fs&#160;-put&#160;file.txt&#160;/your/hdfs/dir/
</code></pre>
</section>
<p><span id="more-12744"></span><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;job 配置</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&#160;&#160;&#160;&#160;String&#160;fileName&#160;=&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;file.txt&#34;</span>;
&#160;&#160;&#160;&#160;String&#160;filePathHdfs&#160;=&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;/your/hdfs/dir/file.txt&#34;</span>;

&#160;&#160;&#160;&#160;Job&#160;job&#160;=&#160;Job.getInstance(getConf());
&#160;&#160;&#160;&#160;Configuration&#160;conf&#160;=&#160;job.getConfiguration();
&#160;&#160;&#160;&#160;conf.set(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;fileName&#34;</span>,&#160;fileName);
&#160;&#160;&#160;&#160;conf.set(</code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-hadoop-2-6-x-%e4%b8%8bdistributed-cache%e7%9a%84%e7%94%a8%e6%b3%95/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>仔细记录一下Java的Map-Reduce job使用distributed cache的方法，毕竟以前一直都是copy paste ~</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;适用的Hadoop版本<br />
CDH 5.8.0（Hadoop 2.6.0）<br />
别的版本没有测试过，但后面相近的版本应该也能用。</p>
<p><span style="background-color:#ffff00;">✓</span>&nbsp;准备工作：上传本地文件到HDFS<br />
为了在Java代码中把一个文件加入 distributed cache，需要先把它上传到HDFS，之后应使用 HDFS 路径来加入 distributed cache。<br />
假设要加入 distributed cache 的文件为 file.txt：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="javascript language-javascript hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">hadoop&nbsp;fs&nbsp;-put&nbsp;file.txt&nbsp;/your/hdfs/dir/
</code></pre>
</section>
<p><span id="more-12744"></span><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;job 配置</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;fileName&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;file.txt&quot;</span>;
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;filePathHdfs&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;/your/hdfs/dir/file.txt&quot;</span>;

&nbsp;&nbsp;&nbsp;&nbsp;Job&nbsp;job&nbsp;=&nbsp;Job.getInstance(getConf());
&nbsp;&nbsp;&nbsp;&nbsp;Configuration&nbsp;conf&nbsp;=&nbsp;job.getConfiguration();
&nbsp;&nbsp;&nbsp;&nbsp;conf.set(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;fileName&quot;</span>,&nbsp;fileName);
&nbsp;&nbsp;&nbsp;&nbsp;conf.set(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;filePathHdfs&quot;</span>,&nbsp;filePathHdfs);

&nbsp;&nbsp;&nbsp;&nbsp;job.addCacheFile(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;URI(String.format(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;%s#%s&quot;</span>,&nbsp;filePathHdfs,&nbsp;fileName)));
</code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
这里往 conf 里塞了两个变量，一个是文件名 fileName，另一个是文件的HDFS路径 filePathHdfs，并且在 addCacheFile() 的时候，拼成了&nbsp;<span style="color:#0000ff;">/your/hdfs/dir/file.txt</span><span style="color:#ff0000;">#</span><span style="color:#0000ff;">file.txt</span> 这样奇怪的形式，这种形式使得在 mapper 或 reducer 中读取 distributed cache 文件的时候，直接用文件名就能读出文件，特别方便！</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;在 mapper 或 reducer 的 setup() 方法中读取 distributed cache 文件</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Override</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">protected</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">setup</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(Context&nbsp;context)</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;IOException,&nbsp;InterruptedException&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Configuration&nbsp;conf&nbsp;=&nbsp;context.getConfiguration();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;myFile&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;File(conf.get(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;file.txt&quot;</span>));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInputStream&nbsp;fis&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;FileInputStream(myFile);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader&nbsp;reader&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;BufferedReader(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;InputStreamReader(fis));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;line;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&nbsp;((line&nbsp;=&nbsp;reader.readLine())&nbsp;!=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">TODO:</span>&nbsp;deal&nbsp;with&nbsp;each&nbsp;line</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
</code></pre>
</section>
<p>
看到没？上面的 conf.get(&quot;file.txt&quot;) 就只使用了文件名就能找到 distributed cache 里的文件，可以做这样&ldquo;神奇&rdquo;的操作是因为背后有一种叫symbolic link的技术。<br />
下面留空的 TODO 那里，需要你自己填写处理每一行数据的逻辑。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-hadoop-2-6-x-%e4%b8%8bdistributed-cache%e7%9a%84%e7%94%a8%e6%b3%95/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 如何用Jackson解析含有转义字符的JSON到Java对象？</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e7%94%a8jackson%e8%a7%a3%e6%9e%90%e5%90%ab%e6%9c%89%e8%bd%ac%e4%b9%89%e5%ad%97%e7%ac%a6%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%b0java%e5%af%b9%e8%b1%a1/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e7%94%a8jackson%e8%a7%a3%e6%9e%90%e5%90%ab%e6%9c%89%e8%bd%ac%e4%b9%89%e5%ad%97%e7%ac%a6%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%b0java%e5%af%b9%e8%b1%a1/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 22 Feb 2019 05:17:50 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[escaped]]></category>
		<category><![CDATA[jackson]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[斜杠]]></category>
		<category><![CDATA[解析]]></category>
		<category><![CDATA[转义字符]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10256</guid>

					<description><![CDATA[<p>
如果一个JSON字符串里有一个字段，它的值又是一个完整的JSON字符串，那么这时候，它可能会含有转义字符。举个例子，我们有一个文件&#160; codelast.json，它的内容为一行字符串：</p>
<blockquote>
<p>
		{&#34;aaa&#34;:&#34;{\&#34;ccc\&#34;:\&#34;ccc\&#34;,\&#34;ddd\&#34;:\&#34;ddd\&#34;}&#34;,&#34;bbb&#34;:{&#34;ccc&#34;:&#34;ccc&#34;,&#34;ddd&#34;:&#34;ddd&#34;}}</p>
</blockquote>
<p>现在要用Jackson解析它为一个Java对象，怎么做？<br />
<span id="more-10256"></span><br />
我们先把这个JSON格式化，便于观察它的结构：</p>
<pre class="CodeContainer" style="margin-top: 0px; margin-bottom: 0px; padding: 8.5px; font-family: Menlo, Monaco, Consolas, &#34;Courier New&#34;, monospace; font-size: 12.025px; color: rgb(51, 51, 51); border-radius: 4px; line-height: 18px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border-style: solid; border-color: rgba(0, 0, 0, 0.15);">
<span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">{</span>
    <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&#34;aaa&#34;</span>: <span class="String" style="color: rgb(0, 119, 119);">&#34;{\&#34;ccc\&#34;:\&#34;ccc\&#34;,\&#34;ddd\&#34;:\&#34;ddd\&#34;}&#34;<span class="Comma" style="color: rgb(0, 0, 0); font-weight: bold;">,</span> </span>
    <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&#34;bbb&#34;</span>: <span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">{</span>
        <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&#34;ccc&#34;</span>: <span class="String" style="color: rgb(0, 119, 119);">&#34;ccc&#34;<span class="Comma" style="color: rgb(0, 0, 0); font-weight: bold;">,</span> </span>
        <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&#34;ddd&#34;</span>: <span class="String" style="color: rgb(0, 119, 119);">&#34;ddd&#34;</span>
    <span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">}</span>
<span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">}</span></pre>
<p>所以按照Jackson的套路，我们似乎很容易就可以写出对应的JSON解析代码，先定义几个POJO：<br />
A.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>A {
  <span style="color:#cc7832;">private </span>AAA <span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  private </span>BBB <span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>AAA <span style="color:#ffc66d;">getAaa</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setAaa</span>(AAA aaa) {
    <span style="color:#cc7832;">this</span>.</pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e7%94%a8jackson%e8%a7%a3%e6%9e%90%e5%90%ab%e6%9c%89%e8%bd%ac%e4%b9%89%e5%ad%97%e7%ac%a6%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%b0java%e5%af%b9%e8%b1%a1/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
如果一个JSON字符串里有一个字段，它的值又是一个完整的JSON字符串，那么这时候，它可能会含有转义字符。举个例子，我们有一个文件&nbsp; codelast.json，它的内容为一行字符串：</p>
<blockquote>
<p>
		{&quot;aaa&quot;:&quot;{\&quot;ccc\&quot;:\&quot;ccc\&quot;,\&quot;ddd\&quot;:\&quot;ddd\&quot;}&quot;,&quot;bbb&quot;:{&quot;ccc&quot;:&quot;ccc&quot;,&quot;ddd&quot;:&quot;ddd&quot;}}</p>
</blockquote>
<p>现在要用Jackson解析它为一个Java对象，怎么做？<br />
<span id="more-10256"></span><br />
我们先把这个JSON格式化，便于观察它的结构：</p>
<pre class="CodeContainer" style="margin-top: 0px; margin-bottom: 0px; padding: 8.5px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.025px; color: rgb(51, 51, 51); border-radius: 4px; line-height: 18px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border-style: solid; border-color: rgba(0, 0, 0, 0.15);">
<span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">{</span>
    <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&quot;aaa&quot;</span>: <span class="String" style="color: rgb(0, 119, 119);">&quot;{\&quot;ccc\&quot;:\&quot;ccc\&quot;,\&quot;ddd\&quot;:\&quot;ddd\&quot;}&quot;<span class="Comma" style="color: rgb(0, 0, 0); font-weight: bold;">,</span> </span>
    <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&quot;bbb&quot;</span>: <span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">{</span>
        <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&quot;ccc&quot;</span>: <span class="String" style="color: rgb(0, 119, 119);">&quot;ccc&quot;<span class="Comma" style="color: rgb(0, 0, 0); font-weight: bold;">,</span> </span>
        <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&quot;ddd&quot;</span>: <span class="String" style="color: rgb(0, 119, 119);">&quot;ddd&quot;</span>
    <span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">}</span>
<span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">}</span></pre>
<p>所以按照Jackson的套路，我们似乎很容易就可以写出对应的JSON解析代码，先定义几个POJO：<br />
A.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>A {
  <span style="color:#cc7832;">private </span>AAA <span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  private </span>BBB <span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>AAA <span style="color:#ffc66d;">getAaa</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setAaa</span>(AAA aaa) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">aaa </span>= aaa<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public </span>BBB <span style="color:#ffc66d;">getBbb</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setBbb</span>(BBB bbb) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">bbb </span>= bbb<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
AAA.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>AAA {
  <span style="color:#cc7832;">private </span>String <span style="color:#9876aa;">ccc</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  private </span>String <span style="color:#9876aa;">ddd</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>String <span style="color:#ffc66d;">getCcc</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">ccc</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setCcc</span>(String ccc) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">ccc </span>= ccc<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public </span>String <span style="color:#ffc66d;">getDdd</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">ddd</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setDdd</span>(String ddd) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">ddd </span>= ddd<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}
</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
BBB.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>BBB {
  <span style="color:#cc7832;">private </span>String <span style="color:#9876aa;">ccc</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  private </span>String <span style="color:#9876aa;">ddd</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>String <span style="color:#ffc66d;">getCcc</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">ccc</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setCcc</span>(String ccc) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">ccc </span>= ccc<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public </span>String <span style="color:#ffc66d;">getDdd</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">ddd</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setDdd</span>(String ddd) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">ddd </span>= ddd<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
然后就是解析的逻辑：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
String json = FileUtils.<span style="font-style:italic;">readFileToString</span>(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/codelast.json&quot;</span>))<span style="color:#cc7832;">;
</span>ObjectMapper mapper = <span style="color:#cc7832;">new </span>ObjectMapper()<span style="color:#cc7832;">;
</span>A data = mapper.readValue(json<span style="color:#cc7832;">, </span>A.<span style="color:#cc7832;">class</span>)<span style="color:#cc7832;">;</span></pre>
<p>我们的目的是把codelast.json这个文件里保存的JSON字符串解析成类A的一个对象data。</p>
<p>这不是很简单嘛？要不运行一下试试看？<br />
马上报错：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="hljs coffeescript" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">com.fasterxml.jackson.databind.exc.MismatchedInputException:&nbsp;Cannot&nbsp;construct&nbsp;instance&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">of</span>&nbsp;`<span class="javascript" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">com.codelast.AAA</span>`&nbsp;(although&nbsp;at&nbsp;least&nbsp;one&nbsp;Creator&nbsp;exists):&nbsp;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">no</span>&nbsp;String-argument&nbsp;constructor/factory&nbsp;method&nbsp;to&nbsp;deserialize&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">from</span>&nbsp;String&nbsp;value&nbsp;(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;{&quot;ccc&quot;:&quot;ccc&quot;,&quot;ddd&quot;:&quot;ddd&quot;}&#39;</span>)
&nbsp;at&nbsp;[Source:&nbsp;(String)<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;{&quot;</span>aaa<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;:&quot;</span>{\<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ccc\&quot;:\&quot;ccc\&quot;,\&quot;ddd\&quot;:\&quot;ddd\&quot;}&quot;</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;bbb&quot;</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ccc&quot;</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ccc&quot;</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ddd&quot;</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ddd&quot;</span>}}
<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;;&nbsp;line:&nbsp;1,&nbsp;column:&nbsp;8]&nbsp;(through&nbsp;reference&nbsp;chain:&nbsp;com.codelast.A[&quot;</span>aaa<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;])

at&nbsp;com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at&nbsp;com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)
at&nbsp;com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1031)
at&nbsp;com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371)
at&nbsp;com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1366)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:171)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
at&nbsp;com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at&nbsp;com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at&nbsp;com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
</span></code></pre>
</section>
<p> 这是什么情况？事实上，这就是由于JSON的&ldquo;aaa&rdquo;这个字段里面包含转义字符造成的，如果你Google一下，可能会找到<a href="https://www.zhihu.com/question/21074655" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这个</span></a>知乎帖子，它告诉我们，要为Jackson的 ObjectMapper 设置一个属性为true：<span style="color:#0000ff;">Feature.ALLOW_UNQUOTED_CONTROL_CHARS</span><br />
所以我们可以试试：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
String json = FileUtils.<span style="font-style:italic;">readFileToString</span>(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/codelast.json&quot;</span>))<span style="color:#cc7832;">;
</span>ObjectMapper mapper = <span style="color:#cc7832;">new </span>ObjectMapper()<span style="color:#cc7832;">;
</span>mapper.configure(JsonParser.Feature.<span style="color:#9876aa;font-style:italic;">ALLOW_UNQUOTED_CONTROL_CHARS</span><span style="color:#cc7832;">, true</span>)<span style="color:#cc7832;">;
</span>A data = mapper.readValue(json<span style="color:#cc7832;">, </span>A.<span style="color:#cc7832;">class</span>)<span style="color:#cc7832;">;</span></pre>
<p>再执行代码，会发现报一样的错。<br />
所以上面的解决方案是<span style="color:#ff0000;">无效</span>的。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
正确解决这个问题的办法，是参考 Stack Overflow的<a href="https://stackoverflow.com/questions/36462971/json-to-java-object-deserialization-with-escaped-properties" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这个</span></a>帖子。里面说到，应该创建一个&nbsp;JsonDeserializer 来搞定这个问题。下面，我就把正确的代码依次贴上来：<br />
A.java 的主要代码（和之前不一样，这里使用了一个自定义的 <span style="color:#ff0000;">AAADeserializer</span>）：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>A {
  <span style="color:#bbb529;">@JsonDeserialize</span>(<span style="color:#d0d0ff;">using </span>= AAADeserializer.<span style="color:#cc7832;">class</span>)
  <span style="color:#cc7832;">private </span>AAA <span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;

</span><span style="color:#cc7832;">  private </span>BBB <span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>AAA <span style="color:#ffc66d;">getAaa</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setAaa</span>(AAA aaa) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">aaa </span>= aaa<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public </span>BBB <span style="color:#ffc66d;">getBbb</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setBbb</span>(BBB bbb) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">bbb </span>= bbb<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}
</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
AAA.java、BBB.java 和之前完全一样，不重复贴代码上来。<br />
下面就是关键的 AAADeserializer.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>AAADeserializer <span style="color:#cc7832;">extends </span>JsonDeserializer&lt;AAA&gt; {
  <span style="color:#cc7832;">private static final </span>ObjectMapper <span style="color:#9876aa;font-style:italic;">mapper </span>= <span style="color:#cc7832;">new </span>ObjectMapper()<span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  </span><span style="color:#bbb529;">@Override
</span><span style="color:#bbb529;">  </span><span style="color:#cc7832;">public </span>AAA <span style="color:#ffc66d;">deserialize</span>(JsonParser jsonParser<span style="color:#cc7832;">, </span>DeserializationContext deserializationContext) <span style="color:#cc7832;">throws </span>IOException {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;font-style:italic;">mapper</span>.readValue(jsonParser.getText()<span style="color:#cc7832;">, </span>AAA.<span style="color:#cc7832;">class</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
然后就可以解析JSON了：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
String json = FileUtils.<span style="font-style:italic;">readFileToString</span>(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/codelast.json&quot;</span>))<span style="color:#cc7832;">;
</span>ObjectMapper mapper = <span style="color:#cc7832;">new </span>ObjectMapper()<span style="color:#cc7832;">;
</span>A data = mapper.readValue(json<span style="color:#cc7832;">, </span>A.<span style="color:#cc7832;">class</span>)<span style="color:#cc7832;">;</span></pre>
<p>
你这时候再运行一下这段代码，就会发现不再报错，JSON已经成功解析到对象 data 中，问题解决！</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e7%94%a8jackson%e8%a7%a3%e6%9e%90%e5%90%ab%e6%9c%89%e8%bd%ac%e4%b9%89%e5%ad%97%e7%ac%a6%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%b0java%e5%af%b9%e8%b1%a1/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>[翻译] 在JAVA中，怎样周期性运行一个任务</title>
		<link>https://www.codelast.com/%e7%bf%bb%e8%af%91-%e5%9c%a8java%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%91%a8%e6%9c%9f%e6%80%a7%e8%bf%90%e8%a1%8c%e4%b8%80%e4%b8%aa%e4%bb%bb%e5%8a%a1/</link>
					<comments>https://www.codelast.com/%e7%bf%bb%e8%af%91-%e5%9c%a8java%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%91%a8%e6%9c%9f%e6%80%a7%e8%bf%90%e8%a1%8c%e4%b8%80%e4%b8%aa%e4%bb%bb%e5%8a%a1/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 25 Jul 2014 03:30:25 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[ScheduledExecutorService]]></category>
		<category><![CDATA[Timer]]></category>
		<category><![CDATA[TimerTask]]></category>
		<category><![CDATA[任务]]></category>
		<category><![CDATA[周期性]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8270</guid>

					<description><![CDATA[<p>
本文是对这篇文章的翻译：<a href="http://java.dzone.com/articles/how-schedule-task-run-interval" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">《How to Schedule a Task to Run in an Interval》</span></a><br />
我不知道原作者是否允许这样做，但我翻译本文仅在于传播知识的目的，在此向原作者表示深深的感谢：感谢你们的分享。</p>
<div>
	在程序中通常都有在后台周期性运行某些任务的需求。例如，类似于Java垃圾收集的后台运行任务。</div>
<div>
	我将在本文中向你展示3种不同的实现方法，如下：</div>
<div>
<span id="more-8270"></span></div>
<ul>
<li>
		<span style="color:#b22222;">使用简单的线程</span></li>
<li>
		<span style="color:#b22222;">使用TimerTask</span></li>
<li>
		<span style="color:#b22222;">使用ScheduledExecutorService</span></li>
</ul>
<div>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></div>
<ul>
<li>
		<span style="color:#0000ff;">使用简单的线程</span></li>
</ul>
<div>
	这个方法非常简单：创建一个线程，使用while循环让它永久运行，并利用sleep方法在运行中插入时间间隔。</div>
<div>
	&#160;</div>
<div>
	这是个简单又快的实现方法。</div>
<div>
	代码示例如下：</div>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&#160;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Task1</span>&#160;</span>{
<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String[]&#160;args)</span>&#160;</span>{
&#160;&#160;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&#160;run&#160;in&#160;a&#160;second</span>
&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">long</span>&#160;timeInterval&#160;=&#160;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1000</span>;
&#160;&#160;Runnable&#160;runnable&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;Runnable()&#160;{
&#160;&#160;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">run</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">()</span>&#160;</span>{
&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&#160;(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">true</span>)&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&#160;-------&#160;code&#160;for&#160;task&#160;to&#160;run</span>
&#160;&#160;&#160;&#160;&#160;&#160;System.out.println(</code></pre>&#8230; <a href="https://www.codelast.com/%e7%bf%bb%e8%af%91-%e5%9c%a8java%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%91%a8%e6%9c%9f%e6%80%a7%e8%bf%90%e8%a1%8c%e4%b8%80%e4%b8%aa%e4%bb%bb%e5%8a%a1/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>
本文是对这篇文章的翻译：<a href="http://java.dzone.com/articles/how-schedule-task-run-interval" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">《How to Schedule a Task to Run in an Interval》</span></a><br />
我不知道原作者是否允许这样做，但我翻译本文仅在于传播知识的目的，在此向原作者表示深深的感谢：感谢你们的分享。</p>
<div>
	在程序中通常都有在后台周期性运行某些任务的需求。例如，类似于Java垃圾收集的后台运行任务。</div>
<div>
	我将在本文中向你展示3种不同的实现方法，如下：</div>
<div>
<span id="more-8270"></span></div>
<ul>
<li>
		<span style="color:#b22222;">使用简单的线程</span></li>
<li>
		<span style="color:#b22222;">使用TimerTask</span></li>
<li>
		<span style="color:#b22222;">使用ScheduledExecutorService</span></li>
</ul>
<div>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></div>
<ul>
<li>
		<span style="color:#0000ff;">使用简单的线程</span></li>
</ul>
<div>
	这个方法非常简单：创建一个线程，使用while循环让它永久运行，并利用sleep方法在运行中插入时间间隔。</div>
<div>
	&nbsp;</div>
<div>
	这是个简单又快的实现方法。</div>
<div>
	代码示例如下：</div>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Task1</span>&nbsp;</span>{
<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String[]&nbsp;args)</span>&nbsp;</span>{
&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;run&nbsp;in&nbsp;a&nbsp;second</span>
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">long</span>&nbsp;timeInterval&nbsp;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1000</span>;
&nbsp;&nbsp;Runnable&nbsp;runnable&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Runnable()&nbsp;{
&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">run</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">()</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&nbsp;(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">true</span>)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;-------&nbsp;code&nbsp;for&nbsp;task&nbsp;to&nbsp;run</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Hello&nbsp;!!&quot;</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;-------&nbsp;ends&nbsp;here</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">try</span>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(timeInterval);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">catch</span>&nbsp;(InterruptedException&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;};
&nbsp;&nbsp;Thread&nbsp;thread&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Thread(runnable);
&nbsp;&nbsp;thread.start();
&nbsp;&nbsp;}
}
</code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<ul>
<li>
		<span style="color:#0000ff;">使用Timer和TimerTask</span></li>
</ul>
<p>之前我们看到的方法极其简单快速，但是它缺少一些功能。</p>
<div>
	这个方法比之前的方法有更多好处：</div>
<div>
	可以控制什么时候开始、取消任务；</div>
<div>
	如果愿意的话，可以延迟第一次执行时间，这个特性很有用。</div>
<div>
	在本例中，<a href="http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#add8e6;">Timer</span></a>类是用于schedule的，<a href="http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#add8e6;">TimerTask</span></a>类是用于包装在它里面的run()方法中执行的任务。</div>
<div>
	Timer实例可以共享，用于schedule多个任务，并且它是线程安全的。</div>
<div>
	当Timer类的构造函数被调用的时候，就创建了一个线程，并且这个单独的线程可用于schedule任务。</div>
<div>
	为了实现我们的目的，我们使用<a href="http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html#scheduleAtFixedRate(java.util.TimerTask,%20long, long)" rel="noopener noreferrer" target="_blank"><span style="background-color:#add8e6;">Timer类的scheduleAtFixedRate方法</span></a>。</div>
<div>
	下面的代码展示了Timer类和TimerTask类的使用方法：</div>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.util.Timer;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.util.TimerTask;

<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Task2</span>&nbsp;</span>{
&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String[]&nbsp;args)</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;TimerTask&nbsp;task&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;TimerTask()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Override</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">run</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">()</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;task&nbsp;to&nbsp;run&nbsp;goes&nbsp;here</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Hello&nbsp;!!!&quot;</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;};
&nbsp;&nbsp;&nbsp;&nbsp;Timer&nbsp;timer&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Timer();
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">long</span>&nbsp;delay&nbsp;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">0</span>;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">long</span>&nbsp;intevalPeriod&nbsp;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;*&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1000</span>;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;schedules&nbsp;the&nbsp;task&nbsp;to&nbsp;be&nbsp;run&nbsp;in&nbsp;an&nbsp;interval&nbsp;</span>
&nbsp;&nbsp;&nbsp;&nbsp;timer.scheduleAtFixedRate(task,&nbsp;delay,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intevalPeriod);
&nbsp;&nbsp;}&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;end&nbsp;of&nbsp;main</span>
}
</code></pre>
</section>
<p>从JDK 1.3开始这些类就已经有了。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<ul>
<li>
		<span style="color:#0000ff;">使用ScheduledExecutorService</span></li>
</ul>
<p>这个类是从Java SE 5开始作为并发工具引入的，位于包java.util.concurrent中。</p>
<div>
	与之前的解决方案相比，它提供了如下好处：</div>
<div>
	用线程池来执行任务，而之前的Timer类是单线程的。</div>
<div>
	提供了第一次任务延迟执行的灵活性。</div>
<div>
	提供了设置时间间隔的良好惯例。</div>
<div>
	&nbsp;</div>
<div>
	下面的代码展示了使用方法。<br />
	在本例中，我们使用了<a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable,%20long, long, java.util.concurrent.TimeUnit)" rel="noopener noreferrer" target="_blank"><span style="background-color:#add8e6;">ScheduledExecutorService类的scheduleAtFixedRate方法</span></a>，它接受一个Runnable对象作为参数（其包含了要周期性运行的代码），以及第一次执行任务的延迟时间。</div>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.util.concurrent.Executors;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.util.concurrent.ScheduledExecutorService;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;java.util.concurrent.TimeUnit;

<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">Task3</span>&nbsp;</span>{
&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">static</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String[]&nbsp;args)</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;Runnable&nbsp;runnable&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Runnable()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">run</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">()</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;task&nbsp;to&nbsp;run&nbsp;goes&nbsp;here</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Hello&nbsp;!!&quot;</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;};
&nbsp;&nbsp;&nbsp;&nbsp;ScheduledExecutorService&nbsp;service&nbsp;=&nbsp;Executors
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.newSingleThreadScheduledExecutor();
&nbsp;&nbsp;&nbsp;&nbsp;service.scheduleAtFixedRate(runnable,&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">0</span>,&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1</span>,&nbsp;TimeUnit.SECONDS);
&nbsp;&nbsp;}
}
</code></pre>
</section>
<p style="font-size: 16px; margin: 5px 0px; clear: both; font-family: sans-serif;">
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e7%bf%bb%e8%af%91-%e5%9c%a8java%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%91%a8%e6%9c%9f%e6%80%a7%e8%bf%90%e8%a1%8c%e4%b8%80%e4%b8%aa%e4%bb%bb%e5%8a%a1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] Java动态添加一个目录到classpath中</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java%e5%8a%a8%e6%80%81%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e7%9b%ae%e5%bd%95%e5%88%b0classpath%e4%b8%ad/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java%e5%8a%a8%e6%80%81%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e7%9b%ae%e5%bd%95%e5%88%b0classpath%e4%b8%ad/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 10 Apr 2014 06:43:03 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[classpath]]></category>
		<category><![CDATA[getResourceAsStream]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[动态]]></category>
		<category><![CDATA[动态添加]]></category>
		<category><![CDATA[添加目录到classpath]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8104</guid>

					<description><![CDATA[<p>
通常我们写程序，会用到一些配置文件，这些文件与程序在同一个父目录下，配置文件可能放在config之类的目录下，程序中会使用这些配置文件。<br />
我遇到了这么件事情：因为一个特殊需求，需要把配置文件放到jar包中，最终部署出去的只能是一个jar包（所有东西都在jar包里，包括程序和所需的资源），然后在程序中使用&#160;<span style="color:#0000ff;">getClass().getResourceAsStream(文件在jar包中的路径)</span> 的方式来读取配置文件。这也倒没什么不爽的，但是在单元测试中怎么办？<br />
<span id="more-8104"></span><br />
单元测试需要在classpath中能找到同样目录结构下的文件，就需要把你的配置文件路径加入classpath，这里你有几种方法：<br />
<span style="background-color:#00ff00;">『1』</span>修改build.xml<br />
在build.xml中可以指定run test时的classpath，你只要把那个目录添加进去就可以了。这样，nightly build（例如Jenkins）就不会失败，但是这种方法是有局限性的，即：你在命令行运行 ant test没有任何问题，但是你如果你的Java工程使用的是某IDE来开发（例如，IntelliJ 或 Eclipse 等），它们在没有特殊设置的情况下，在classpath中可能是找不到那个配置文件路径的，这时，你可以像<a href="http://www.codelast.com/?p=5744" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章一样设置一下，单元测试就没问题了。<br />
不过显然，第一次从代码库checkout出项目之后，还必须要设置一下总是让人不爽的，尤其是不熟悉项目的人，怎么会知道该添加哪个路径到classpath呢？<br />
所以这种方法就不详述了，我觉得不好。</p>
<p><span style="background-color:#00ff00;">『2』</span>修改IDE的项目设置<br />
如上所述，修改IDE的项目设置可以使得在IDE中run test的时候通过，但是对诸如Jenkins之类的工具执行的nightly build（最终是通过build.xml实现的），仅仅修改IDE设置是没用的，nightly build依然会挂掉。所以这种方法只能保证&#8220;临时用用&#8221;。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『3』</span>动态添加目录到classpath中<br />
本文的主角登场了。<br />
如果我们能在单元测试中，添加一些代码，动态地将需要的目录添加到classpath中，那么问题不就从最根本上解决了吗？不仅不用修改IDE里的项目设置，也不用修改build.xml，好多啊。是的，你只需要在单元测试中的setUp()里添加下面的代码：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Before
</span>
<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">setUp</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">()</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&#160;Exception&#160;</span>{
&#160;&#160;&#160;&#160;File&#160;programRootDir&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;File(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;./&#34;</span></code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-java%e5%8a%a8%e6%80%81%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e7%9b%ae%e5%bd%95%e5%88%b0classpath%e4%b8%ad/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>
通常我们写程序，会用到一些配置文件，这些文件与程序在同一个父目录下，配置文件可能放在config之类的目录下，程序中会使用这些配置文件。<br />
我遇到了这么件事情：因为一个特殊需求，需要把配置文件放到jar包中，最终部署出去的只能是一个jar包（所有东西都在jar包里，包括程序和所需的资源），然后在程序中使用&nbsp;<span style="color:#0000ff;">getClass().getResourceAsStream(文件在jar包中的路径)</span> 的方式来读取配置文件。这也倒没什么不爽的，但是在单元测试中怎么办？<br />
<span id="more-8104"></span><br />
单元测试需要在classpath中能找到同样目录结构下的文件，就需要把你的配置文件路径加入classpath，这里你有几种方法：<br />
<span style="background-color:#00ff00;">『1』</span>修改build.xml<br />
在build.xml中可以指定run test时的classpath，你只要把那个目录添加进去就可以了。这样，nightly build（例如Jenkins）就不会失败，但是这种方法是有局限性的，即：你在命令行运行 ant test没有任何问题，但是你如果你的Java工程使用的是某IDE来开发（例如，IntelliJ 或 Eclipse 等），它们在没有特殊设置的情况下，在classpath中可能是找不到那个配置文件路径的，这时，你可以像<a href="http://www.codelast.com/?p=5744" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章一样设置一下，单元测试就没问题了。<br />
不过显然，第一次从代码库checkout出项目之后，还必须要设置一下总是让人不爽的，尤其是不熟悉项目的人，怎么会知道该添加哪个路径到classpath呢？<br />
所以这种方法就不详述了，我觉得不好。</p>
<p><span style="background-color:#00ff00;">『2』</span>修改IDE的项目设置<br />
如上所述，修改IDE的项目设置可以使得在IDE中run test的时候通过，但是对诸如Jenkins之类的工具执行的nightly build（最终是通过build.xml实现的），仅仅修改IDE设置是没用的，nightly build依然会挂掉。所以这种方法只能保证&ldquo;临时用用&rdquo;。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『3』</span>动态添加目录到classpath中<br />
本文的主角登场了。<br />
如果我们能在单元测试中，添加一些代码，动态地将需要的目录添加到classpath中，那么问题不就从最根本上解决了吗？不仅不用修改IDE里的项目设置，也不用修改build.xml，好多啊。是的，你只需要在单元测试中的setUp()里添加下面的代码：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Before
</span>
<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">setUp</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">()</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;Exception&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;programRootDir&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;File(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;./&quot;</span>);
&nbsp;&nbsp;&nbsp;&nbsp;URLClassLoader&nbsp;classLoader&nbsp;=&nbsp;(URLClassLoader)&nbsp;ClassLoader.getSystemClassLoader();
&nbsp;&nbsp;&nbsp;&nbsp;Method&nbsp;add&nbsp;=&nbsp;URLClassLoader.class.getDeclaredMethod(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;addURL&quot;</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;Class[]{URL.class});
&nbsp;&nbsp;&nbsp;&nbsp;add.setAccessible(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">true</span>);
&nbsp;&nbsp;&nbsp;&nbsp;add.invoke(classLoader,&nbsp;programRootDir.toURI().toURL());
}
</code></pre>
</section>
<p>就可以把目录动态添加到classpath中了。在这里，代码中的&ldquo;./&rdquo;表示我把项目的根目录添加到了classpath中。<br />
修改之后，无论是在IDE中run unit test，还是在命令行ant test，都完全没问题了。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java%e5%8a%a8%e6%80%81%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e7%9b%ae%e5%bd%95%e5%88%b0classpath%e4%b8%ad/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创]（翻译）什么是Java的永久代（PermGen）内存泄漏</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%ef%bc%88%e7%bf%bb%e8%af%91%ef%bc%89%e4%bb%80%e4%b9%88%e6%98%afjava%e7%9a%84%e6%b0%b8%e4%b9%85%e4%bb%a3%ef%bc%88permgen%ef%bc%89%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%ef%bc%88%e7%bf%bb%e8%af%91%ef%bc%89%e4%bb%80%e4%b9%88%e6%98%afjava%e7%9a%84%e6%b0%b8%e4%b9%85%e4%bb%a3%ef%bc%88permgen%ef%bc%89%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 01 Oct 2013 08:30:14 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java.lang.OutOfMemoryError: PermGen space]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[PermGen]]></category>
		<category><![CDATA[内存泄漏]]></category>
		<category><![CDATA[永久代]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=7248</guid>

					<description><![CDATA[<p>
本文是我对这篇文章的翻译：<a href="http://plumbr.eu/blog/what-is-a-permgen-leak" target="_blank" rel="noopener noreferrer"><span style="background-color:#add8e6;">What is a PermGen leak?</span></a>&#160;为了便于阅读，我将原文附于此处，翻译穿插在其中。此外，为了防止原链接在未来某一天失效后，文中的图片再也看不到的问题，我将原文中的图片也保存到了本站的服务器上，我不知道原作者是否允许这样做，但我翻译本文仅在于传播知识的目的，在此向原作者表示深深的感谢：感谢你们的分享。<br />
<span id="more-7248"></span><br />
<span style="color: rgb(60, 58, 55); font-family: Helvetica, Arial, sans-serif; font-size: 24px; line-height: 26px; text-transform: uppercase;">WHAT IS A PERMGEN LEAK?</span><br />
<span style="font-size:14px;"><span style="font-family: 宋体;"><span style="color: rgb(0, 0, 255);">什么是Java的永久代（PermGen）内存泄漏？</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	What follows is a practical introduction to a specific type of&#160;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">memory</i>&#160;problems in Java applications. Namely &#8211; we will analyze the errors that cause the&#160;<b style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;"><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">java.lang.OutOfMemoryError: PermGen space</i></b>symptom in the stack trace.&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%ef%bc%88%e7%bf%bb%e8%af%91%ef%bc%89%e4%bb%80%e4%b9%88%e6%98%afjava%e7%9a%84%e6%b0%b8%e4%b9%85%e4%bb%a3%ef%bc%88permgen%ef%bc%89%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
本文是我对这篇文章的翻译：<a href="http://plumbr.eu/blog/what-is-a-permgen-leak" target="_blank" rel="noopener noreferrer"><span style="background-color:#add8e6;">What is a PermGen leak?</span></a>&nbsp;为了便于阅读，我将原文附于此处，翻译穿插在其中。此外，为了防止原链接在未来某一天失效后，文中的图片再也看不到的问题，我将原文中的图片也保存到了本站的服务器上，我不知道原作者是否允许这样做，但我翻译本文仅在于传播知识的目的，在此向原作者表示深深的感谢：感谢你们的分享。<br />
<span id="more-7248"></span><br />
<span style="color: rgb(60, 58, 55); font-family: Helvetica, Arial, sans-serif; font-size: 24px; line-height: 26px; text-transform: uppercase;">WHAT IS A PERMGEN LEAK?</span><br />
<span style="font-size:14px;"><span style="font-family: 宋体;"><span style="color: rgb(0, 0, 255);">什么是Java的永久代（PermGen）内存泄漏？</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	What follows is a practical introduction to a specific type of&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">memory</i>&nbsp;problems in Java applications. Namely &ndash; we will analyze the errors that cause the&nbsp;<b style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;"><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">java.lang.OutOfMemoryError: PermGen space</i></b>symptom in the stack trace.<br />
	<span style="font-family:宋体;"><span style="color: rgb(0, 0, 255);"><span style="font-size: 14px;">本文实用性地介绍了Java程序的一个特殊的内存问题，即&mdash;&mdash;我们将分析stack trace中产生java.lang.OutOfMemoryError: PermGen space错误的原因。</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	First of all we will go through the&nbsp;<a href="http://plumbr.eu/blog/what-is-a-permgen-leak#intro" style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213); text-decoration: none;">core concepts</a>&nbsp;required to understand the subject &ndash; and explain what objects, classes, classloaders and the JVM memory model are. If you are familiar with the basic concepts, you can jump directly to&nbsp;<a href="http://plumbr.eu/blog/what-is-a-permgen-leak#samples" style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213); text-decoration: none;">the next section</a>&nbsp;where I will describe two typical cases for the error in question alongside with hints and suggestions for solving it.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">首先，我们来看看理解该主题所需的<a href="http://plumbr.eu/blog/what-is-a-permgen-leak#intro" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">核心概念</span></a>，并且解释一下什么是对象（objects），类（classes），类加载器（classloaders）以及JVM内存模型（JVM memory model）。如果你已经熟知了这些基础概念，可以直接跳到下一节，我在它里面讨论了两种产生该错误的典型原因，以及解决此问题的提示和建议。</span></span></span><br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<h2 style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px 0px 15px; border: 0px; outline: 0px; font-size: 22px; vertical-align: baseline; font-family: Helvetica, Arial, sans-serif;">
	Objects, Classes and ClassLoaders</h2>
<p><span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">对象（Objects），类（Classes）以及类加载器（ClassLoaders）</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	Well, I will not start with the very basics. I guess if you have already found us, you should be familiar with the concept that everything in Java is an&nbsp;<b style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">Object</b>. And that all Objects are specified by their&nbsp;<span style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; font-weight: bold;">Class</span>. So every object has a reference to an instance of&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">java.lang.Class</i>&nbsp;describing the structure of that object&rsquo;s class.</p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	But what actually happens under the hood, when you create a new object in your code? For example if you write something truly complicated like</p>
<div>
	<span style="font-size:14px;"><span style="font-family: 宋体;"><span style="color: rgb(0, 0, 255);">我不会从最基础的概念开始讲。我假设你已经知道了我们，你应该熟知：在Java中一切皆是对象（Object），并且所有对象都是由它们的类（Class）指定的。所以每一个对象都有一个到java.lang.Class（用于描述对象的结构）的实例的引用。</span></span></span></div>
<div>
	<span style="font-size:14px;"><span style="font-family: 宋体;"><span style="color: rgb(0, 0, 255);">但是当你在代码中创建一个新对象的时候，到底发生了什么呢？例如，当你写下如下代码的时候：</span></span></span></div>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	&nbsp;</p>
<pre class="brush:java;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
Person boss = new Person();
</pre>
<p>
<span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">The Java Virtual Machine (JVM) needs to understand the structure of the object to create. To achieve this, the JVM looks for the class called Person. And if the&nbsp;</span><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; line-height: 18px;">Person</i><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">&nbsp;class is accessed for the first time during this particular execution of the program, it has to be&nbsp;</span><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; line-height: 18px;">loaded</i><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">&nbsp;by the JVM, normally from the corresponding&nbsp;</span><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; line-height: 18px;">Person.class</i><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">&nbsp;file. The process of seeking for the&nbsp;</span><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; line-height: 18px;">Person.class&nbsp;</i><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">file on the drive, loading it into memory and parsing it&rsquo;s structure is called&nbsp;</span><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; line-height: 18px;">class loading</i><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">. Ensuring proper class loading process is the responsibility of a&nbsp;</span><b style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; line-height: 18px;"><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">ClassLoader</i></b><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; line-height: 18px;">.&nbsp;</i><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">ClassLoaders are instances of&nbsp;</span><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; line-height: 18px;">java.lang.ClassLoader</i><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">&nbsp;class and each and every class in a Java program has to be loaded by some&nbsp;</span><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; line-height: 18px;">ClassLoader.&nbsp;</i><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">As a result we now have the following relationships:</span><br />
<span style="font-family:宋体;"><span style="font-size: 14px;"><span style="color: rgb(0, 0, 255);">Java虚拟机（JVM）需要理解待创建对象的结构，为此，JVM会查找叫Person的类。并且，如果在程序的这一次运行中，Person类是第一次被访问的话，它必须要被JVM加载（loaded），通常这是从对应的Person.class文件加载的。从磁盘上查找Person.class文件、将其加载到内存中，并解析它的结构的过程，就叫作类的加载（class loading）。保证合适的类被加载是类加载器（ClassLoader）的职责。ClassLoader是java.lang.ClassLoader类的实例，并且Java程序中的每一个类都必须被一些ClassLoader加载。最后，我们可以得到下面的关系图：</span></span></span></p>
<p><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;"> <img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2013/10/permgen_leak_1.png" style="width: 570px; height: 72px;" /></span></p>
<p><span style="font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" style="font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;">As you can see from the next diagram every classloader holds references to all the classes it has loaded. For the purpose of our article these relationships are very interesting.</span><br />
<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">从下一幅图你可以看到，每一个classloader都持有它所加载的所有类的引用。由于本文的主旨的原因，这些关系非常有趣。</span></span></span></p>
<p><span style="color: rgb(60, 58, 55); font-family: Helverica, Arial, sans-serif; font-size: 13px; line-height: 18px;"> <img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2013/10/permgen_leak_2.png" style="width: 570px; height: 262px;" /></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	Remember this image, we will need it later.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">记住这幅图，我们稍后会用到它。</span></span></span><br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<h2 style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px 0px 15px; border: 0px; outline: 0px; font-size: 22px; vertical-align: baseline; font-family: Helvetica, Arial, sans-serif;">
	Permanent Generation</h2>
<p><span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">永久代</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	Almost every JVM nowadays uses a separate region of memory, called the Permanent Generation (or<b style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">PermGen</b>&nbsp;for short), to hold internal representations of java classes. PermGen is also used to store more information &ndash; find out the details from&nbsp;<a href="https://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation" style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213); text-decoration: none;">this post</a>&nbsp;if you are interested &ndash; but for our article it is safe to assume that only the class definitions are being stored in PermGen. The default size of this region on my two machines running java 1.6 is not a very impressive 82MB.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">当今，几乎所有JVM都使用一个称为永久代（Permanent Generation，或者简写为PermGen）的独立内存区域来保存Java类的内在表征。PermGen也用来保存更多信息&mdash;&mdash;如果感兴趣的话，你可以看看<a href="http://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">这篇</span></a>文章&mdash;&mdash;但是对本文来说，我们可以假定PermGen只保存类的定义。对我那运行Java 1.6的机器来说，此内存区域的默认大小是82MB，一个很普通的数值。</span></span></span></p>
<p>	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2013/10/permgen_leak_3.png" style="width: 595px; height: 298px;" /><br />
	&nbsp;</p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	As I have explained in one of my earlier posts, a&nbsp;<a href="http://plumbr.eu/blog/what-is-a-memory-leak" style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213); text-decoration: none;">memory leak</a>&nbsp;in Java is a situation where some objects are no longer used by an application, but the&nbsp;<b style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">Garbage Collector</b>&nbsp;fails to recognize them as unused. This leads to the&nbsp;<b style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;"><i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">OutOfMemoryError</i></b>&nbsp;if those unused objects contribute to the heap usage significantly enough that the next memory allocation request by the application cannot be fulfilled.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">就像我在以前一篇文章中解释的那样，Java的<a href="http://plumbr.eu/blog/what-is-a-memory-leak" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">内存泄漏</span></a>是指某些对象不再被应用程序使用，而垃圾收集器（Garbage Collector）却没能识别它们是&ldquo;不再使用的&rdquo;。如果那些不使用的对象占用堆（heap）空间足够大，使得应用程序无法满足下一次内存分配需求，就会导致OutOfMemoryError错误。</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	The root cause of&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">java.lang.OutOfMemoryError: PermGen space</i>&nbsp;is exactly the same: the JVM needs to load the definition of a new class but there is not enough space in PermGen to do it &ndash; there are already too many classes stored there. A possible reason for this could be your application or server using too many classes for the current size of PermGen not to be able to accommodate them. Another commone reason could be a memory leak.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">java.lang.OutOfMemoryError: PermGen space错误的根本原因也是完全一样的：JVM需要加载一个新类的定义，而永久代（PermGen）的空间不足&mdash;&mdash;已经有太多的类存储在那里了。一个可能的原因是：你的应用程序或服务器使用了太多的类，当前的永久代（PermGen）大小无法满足需求。另一个原因可能是内存泄漏。</span></span></span><br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<h2 style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px 0px 15px; border: 0px; outline: 0px; font-size: 22px; vertical-align: baseline; font-family: Helvetica, Arial, sans-serif;">
	Permanent Generation Leak</h2>
<p><span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">永久代（内存）泄漏</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	But still, how on earth it is possible to leak something in PermGen? It holds definitions of java classes and they cannot become unused, can they? Actually, they can. In case of a Java web application deployed into an application server all those classes in your EAR/WAR become worthless when the application is undeployed. The JVM continues to run as the application server is still alive, but a whole bunch of class definitions are not in use anymore. And they should be removed from PermGen. If not, we will have memory leak in the PermGen area.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">但是，永久代（PermGen）到底有没有可能会内存泄漏？它保存了Java类定义，并且这些类定义是不会变成&ldquo;无用&rdquo;的，是吗？事实上，它们是可以变成&ldquo;无用&rdquo;的。以一个部署到应用程序服务器的Java web程序来说，当该应用程序被卸载的时候，你的EAR/WAR包中的所有类都将变得无用。只要应用程序服务器还活着，JVM将继续运行，但是一大堆的类定义将不再使用，理应将它们从永久代（PermGen）中移除。如果不移除的话，我们在永久代（PermGen）区域就会有内存泄漏。</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	As a nice sample on the reasons &ndash; the Tomcat developers have set up a&nbsp;<a href="http://wiki.apache.org/tomcat/MemoryLeakProtection" style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213); text-decoration: none;">Wiki page</a>&nbsp;describing different leaks found and fixed in the Apache Tomcat versions 6.0.24 and above.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">这里有一个说明内存泄漏的原因的好例子&mdash;&mdash;Tomcat的开发者们写了一个<a href="http://wiki.apache.org/tomcat/MemoryLeakProtection" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">Wiki页面</span></a>来描述在Apache Tomcat 6.0.24及之后的版本中发现、修复的不同的内存泄漏原因。</span></span></span><br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<h2 style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px 0px 15px; border: 0px; outline: 0px; font-size: 22px; vertical-align: baseline; font-family: Helvetica, Arial, sans-serif;">
	Leaking Threads</h2>
<p><span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">线程内存泄漏</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	One possible scenario for a classloader leak is through long running threads. This happens when your application or &ndash; as often was the case in my experience &ndash; a 3rd party library used by your application, starts some long running thread. An example of this could be a timer thread whose job is to execute some code periodically.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">类加载器（classloader）泄漏的一个可能的场景就是通过运行的线程（而内存泄漏）。当你的程序，或者你的程序使用的第三方库（我经常遇到这种情况）开启了一些长时间运行的线程。一个例子：一个用于周期性执行代码的计时器（timer）线程。</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	If the intended lifespan of this thread is not fixed, we are heading directly into a trouble. When any part of your application ever starts a thread, you must make sure that it is not going to outlive the application. In typical cases the developer either is not aware of this responsibility or simply forgets to write the clean-up code.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">如果不解决该线程预期的生命周期问题，我们直接会遇到麻烦。当你程序的任何一部分启动一个线程的时候，你要确保它不会比程序活得还要久。在典型的情况下，开发者要么不知道自己有责任处理好这个问题，或者忘了写清理（clean-up）的代码。</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	Otherwise, if some thread continues to run after the application is undeployed, it will, usually, hold a reference to a classloader of the web application it was started by, called&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">context classloader</i>. Which in turn means that all classes of the undeployed application continue to be held in memory. Remedy? If it is your application that starts new threads, you should shut them down during undeployment using a servlet context listener. If it is a 3rd party library, you should search for its own specific shutdown hook. Or file a bug report if there is none.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">否则，如果应用程序卸载后，线程还在继续运行，它通常将维持一个到web应用程序的classloader的引用，即我们所说的context classloader。这也就意味着，所有卸载掉的应用程序仍然保存在内存中。怎么解决？如果是你的程序开启了新线程，那么你就应该在卸载的时候关闭它们，这可以通过使用一个servlet context listener来实现。如果是第三方库开启的新线程，你应该搜索它的关闭线程的接口，如果没有的话，就上报一个bug吧。</span></span></span><br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<h2 style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px 0px 15px; border: 0px; outline: 0px; font-size: 22px; vertical-align: baseline; font-family: Helvetica, Arial, sans-serif;">
	<a style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213);">Leaking Drivers</a></h2>
<p><span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">驱动内存泄漏</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	Another typical case of a leak can be caused by database drivers. We have encountered this leak in&nbsp;<a href="http://plumbr.eu/download" style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213); text-decoration: none;">our own demo application</a>&nbsp;that we ship with Plumbr. It is a slightly modified Pet Clinic application shipped along with Spring MVC. Let us highlight some things that happen when this application is being deployed to the server.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">另一个典型的内存泄漏原因是由数据库驱动造成的。我们在和Plumbr一起发布的<a href="http://plumbr.eu/download" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">demo程序</span></a>中遇到了这种内存泄漏情况。它是一个与Sprint MVC一起发布的、代码稍微修改过的Pet Clinic程序。让我们关注一下当这个应用程序部署到服务器上的时候，发生了什么：</span></span></span></p>
<ul style="box-sizing: border-box; margin: 10px 0px 20px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 12px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
<li style="box-sizing: border-box; margin-top: 4px; margin-left: 30px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Arial, Helvetica, sans-serif; color: black; list-style-image: url(http://plumbr.eu/wp-content/themes/plumbr/assets/images/bullet.jpg); list-style-type: none; margin-right: 0px !important; margin-bottom: 0px !important;">
		The server creates a new instance of&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">java.lang.Classloader&nbsp;</i>and starts to load the application&rsquo;s classes using it.</li>
<li style="box-sizing: border-box; margin-top: 4px; margin-left: 30px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Arial, Helvetica, sans-serif; color: black; list-style-image: url(http://plumbr.eu/wp-content/themes/plumbr/assets/images/bullet.jpg); list-style-type: none; margin-right: 0px !important; margin-bottom: 0px !important;">
		<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">服务器创建一个java.lang.Classloader的新实例，并用它来加载程序的类。</span></span></span></li>
<li style="box-sizing: border-box; margin-top: 4px; margin-left: 30px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Arial, Helvetica, sans-serif; color: black; list-style-image: url(http://plumbr.eu/wp-content/themes/plumbr/assets/images/bullet.jpg); list-style-type: none; margin-right: 0px !important; margin-bottom: 0px !important;">
		Since the PetClinic uses a HSQL database, it loads the corresponding JDBC driver,<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">org.hsqldb.jdbcDriver</i></li>
<li style="box-sizing: border-box; margin-top: 4px; margin-left: 30px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Arial, Helvetica, sans-serif; color: black; list-style-image: url(http://plumbr.eu/wp-content/themes/plumbr/assets/images/bullet.jpg); list-style-type: none; margin-right: 0px !important; margin-bottom: 0px !important;">
		<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">由于PetClinic使用了HSQL数据库，所以它会加载相应的JDBC驱动，即org.hsqldb.jdbcDriver</span></span></span></li>
<li style="box-sizing: border-box; margin-top: 4px; margin-left: 30px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Arial, Helvetica, sans-serif; color: black; list-style-image: url(http://plumbr.eu/wp-content/themes/plumbr/assets/images/bullet.jpg); list-style-type: none; margin-right: 0px !important; margin-bottom: 0px !important;">
		This class, being a good-mannered JDBC driver, registers itself with&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">java.sql.DriverManager</i>&nbsp;during initialization, as required per JDBC specification. This registration includes storing inside a static field of DriverManager a reference to an instance of&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">org.hsqldb.jdbcDriver</i>.</li>
<li style="box-sizing: border-box; margin-top: 4px; margin-left: 30px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Arial, Helvetica, sans-serif; color: black; list-style-image: url(http://plumbr.eu/wp-content/themes/plumbr/assets/images/bullet.jpg); list-style-type: none; margin-right: 0px !important; margin-bottom: 0px !important;">
		<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">这个JDBC驱动类会在初始化的时候将它注册到java.sql.DriverManager中（正如JDBC规范所要求的那样）。这个注册过程包括了存储org.hsqldb.jdbcDriver的一个实例的引用到DriverManager的一个静态域中</span></span></span></li>
</ul>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	Now, when the application is undeployed from the application server, the&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">java.sql.DriverManager&nbsp;</i>will still hold that reference, as there is no code in the HSQLDB library nor in the Spring framework nor in the application to remove that! As was explained above, a&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">jdbcDriver</i>&nbsp;object still holds a reference to an<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">org.hsqldb.jdbcDriver&nbsp;</i>class, which in turn holds a reference to the instance of&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">java.lang.Classloader</i>used to load the application. This classloader now still reference all the classes of the application. In case of our particular demo application, during application startup almost 2000 classes are loaded, occupying roughly 10MB in PermGen. Which means that it takes about 5-10 redeploys to fill the PermGen with default size to reach the&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">java.lang.OutOfMemoryError: PermGen space</i>&nbsp;crash.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">现在，当从服务器上卸载应用程序的时候，java.sql.DriverManager仍将持有那个引用，无论在HSQLDB库，或者在Spring framework中，都没有代码可以移除它！正如上面解释的那样，一个jdbcDriver对象将持有一个到org.hsqldb.jdbcDriver类的引用，从而持有用于加载应用程序的java.lang.Classloader的一个实例的引用。这个classloader现在仍然引用着应用程序的所有类。在我们那特殊的demo应用程序中，在程序启动的时候，需要加载将近2000个类，占用约10MB永久代（PermGen）内存。这就意味着需要5~10次重新部署，才会将默认大小的永久代（PermGen）塞满，然后就会触发java.lang.OutOfMemoryError: PermGen space错误并崩溃。</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	How to fix that? One possibility is to write a servlet context listener, which de-registers the HSQLDB driver from&nbsp;<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">DriverManager</i>&nbsp;during application shutdown. This is pretty straightforward. But remember &ndash; you will have to write the corresponding code in every application using the driver.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">怎样解决此问题？一个可能的办法就是写一个servlet content listener，用于在应用程序关闭的时候，从DriverManager反注册HSQLDB驱动。这个方法很直接，但是请记住&mdash;&mdash;你需要在使用该驱动的每一个应用程序中都这么写。</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	<a href="http://www.plumbr.eu/" style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213); text-decoration: none;">Download</a>&nbsp;our latest version of Plumbr with our demo application and play with it to find out how the leak occurs, how Plumbr finds it and how do we explain the cause.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">下载我们最新版的Plumbr以及demo应用程序来研究一下，看看内存泄漏是怎么发生的，Plumbr是怎么发现它，以及我们是怎么解释其原因的。</span></span></span><br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<h2 style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px 0px 15px; border: 0px; outline: 0px; font-size: 22px; vertical-align: baseline; font-family: Helvetica, Arial, sans-serif;">
	Conclusion</h2>
<p><span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">结论</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	There are many reasons why your application might encounter a<i style="box-sizing: border-box; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline;">&nbsp;java.lang.OutOfMemoryError: PermGen space</i>. The root cause for the majority of them is some reference to an object or a class loaded by the application&rsquo;s class loader that has died after that. Or a direct link to the class loader itself. The actions you need to take for remedy are quite similar for most of these causes. Firstly, find out where that reference is being held. Secondly, add a shutdown hook to your web application to remove the reference during application&rsquo;s undeployment. You can do that by either using a servlet context listener or by using the API provided by your 3rd party library.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">你的应用程序遇到java.lang.OutOfMemoryError: PermGen space错误的原因很多，究其根本原因，大多数是由于object或程序的class loader加载的类的引用已经无用了导致的。对此类问题，你需要采取的补救措施都非常相似，即，首先，找出引用在哪里被持有；其次，给你的web应用程序添加一个关闭的hook，或者在应用程序卸载后移除引用。你要么通过servlet context listener，要么通过第三方库提供的API来实现这一点。</span></span></span></p>
<p style="box-sizing: border-box; margin: 0px 0px 20px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; font-family: Helverica, Arial, sans-serif; color: rgb(60, 58, 55); line-height: 18px;">
	Finding those leaking references has never been easy. We ourselves have spent countless hours trying to trace down why some applications require 20MB of PermGen on each redeploy. But as of version 1.1,, Plumbr will show you the cause of the leak and give you a hint on how to fix it. If you want to try it out,&nbsp;<a href="http://www.plumbr.eu/" style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213); text-decoration: none;">register and download the tool</a>. If you are running an older version of Plumbr, we strongly recommend downloading&nbsp;<a href="http://plumbr.eu/download" style="box-sizing: border-box; margin: 0px; padding: 0px; vertical-align: baseline; color: rgb(0, 156, 213); text-decoration: none;">an upgrade</a>.<br />
	<span style="color:#0000ff;"><span style="font-size: 14px;"><span style="font-family: 宋体;">找出内存泄漏的引用从来就不是件容易事。例如，就我们自己来说，我们曾经花费了数不清的时间去追踪为什么某些程序每一次部署都需要用掉20MB的永久代（PermGen）内存。但是，Plumbr 1.1版能告诉你内存泄漏的原因，并给出修复提示。如果你想试试，就去注册下载吧。如果你还在使用旧版的Plumbr，我们强烈建议你升级到新版。</span></span></span></p>
<p>	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushJava.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%ef%bc%88%e7%bf%bb%e8%af%91%ef%bc%89%e4%bb%80%e4%b9%88%e6%98%afjava%e7%9a%84%e6%b0%b8%e4%b9%85%e4%bb%a3%ef%bc%88permgen%ef%bc%89%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创]如何添加一个文件/目录到IntelliJ项目的classpath中</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9b%ae%e5%bd%95%e5%88%b0intellij%e9%a1%b9%e7%9b%ae%e7%9a%84classpath%e4%b8%ad/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9b%ae%e5%bd%95%e5%88%b0intellij%e9%a1%b9%e7%9b%ae%e7%9a%84classpath%e4%b8%ad/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sun, 03 Mar 2013 13:10:40 +0000</pubDate>
				<category><![CDATA[未分类]]></category>
		<category><![CDATA[classpath]]></category>
		<category><![CDATA[IntelliJ]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[添加到classpath]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=5744</guid>

					<description><![CDATA[<p>
以下内容的背景：IntelliJ，Java项目。<br />
在单元测试中，我们经常会使用与生产环境里不同的一些配置文件来为提供参数，而你的程序又是以类似下面的方法来读取配置文件：</p>
<pre class="brush:java;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
String configFileName = &#34;config.properties&#34;;
Properties properties = new Properties();
properties.load(MyClass.class.getClassLoader().getResourceAsStream(configFileName));
</pre>
<p>上面的代码中仅使用了配置文件名，而不是其完整路径，这就导致了可能在IntelliJ中运行一个单独的单元测试的时候，找不到该配置文件，因为你只提供文件名的话，该文件必须要在classpath中，否则是找不到它的。<br />
<span id="more-5744"></span><br />
所以，我们可以把该配置文件所在的目录添加到IntelliJ项目里的classpath中，让它可以在运行单元测试的时候找得到。方法如下（一图胜千言，直接看下面的演示图就明白了）：<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
点击菜单File&#8594;Project Structure，进入&#8220;Project Structure&#8221;对话框，点击左侧的&#8220;Modules&#8221;，然后点击中部的&#8220;Dependencies&#8221;tab，然后再点击右部的&#8220;+&#8221;号添加你指定的目录，这里要注意了，在弹出的&#8220;Attach Files&#8221;对话框中，你必须要选择&#8220;classes&#8221;（如图所示）。最后，把目录添加进来之后，再把它们对应的&#8220;Scope&#8221;设置为&#8220;Test&#8221;，表示在测试中会依赖这个目录，即可。<br />
<img decoding="async" alt="add a file or directory to the classpath of an IntelliJ Java project" src="http://www.codelast.com/wp-content/uploads/2013/03/project_structure_with_demo_final.png" style="width: 576px; height: 218px; " /><br />
<strong><span style="color:#ff0000;">图片可保存下来放大查看。</span></strong></p>
<p>以上方法在IntelliJ 12.0.4中亲测通过，对于其他版本的IntelliJ也应该类似。</p>
<p><span style="background-color:#00ff00;">【补充】</span><br />
12.1.4版的IntelliJ与上面的图略有不同。在点击了&#8220;+&#8221;号之后，会弹出一个菜单：</p>
<div style="text-align: center;">
	<img decoding="async" alt="add menu" src="http://www.codelast.com/wp-content/uploads/2013/03/add_menu.png" style="width: 158px; height: 89px;" /></div>
<p>选择&#8220;Jars or directories...&#8221;，然后会弹出对话框让你选择目录或文件，选择好之后，和上面一样，要选择&#8220;classes&#8221;，后面的操作也一样了。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;版权声明&#160;<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&#160;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushJava.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']</script>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9b%ae%e5%bd%95%e5%88%b0intellij%e9%a1%b9%e7%9b%ae%e7%9a%84classpath%e4%b8%ad/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
以下内容的背景：IntelliJ，Java项目。<br />
在单元测试中，我们经常会使用与生产环境里不同的一些配置文件来为提供参数，而你的程序又是以类似下面的方法来读取配置文件：</p>
<pre class="brush:java;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
String configFileName = &quot;config.properties&quot;;
Properties properties = new Properties();
properties.load(MyClass.class.getClassLoader().getResourceAsStream(configFileName));
</pre>
<p>上面的代码中仅使用了配置文件名，而不是其完整路径，这就导致了可能在IntelliJ中运行一个单独的单元测试的时候，找不到该配置文件，因为你只提供文件名的话，该文件必须要在classpath中，否则是找不到它的。<br />
<span id="more-5744"></span><br />
所以，我们可以把该配置文件所在的目录添加到IntelliJ项目里的classpath中，让它可以在运行单元测试的时候找得到。方法如下（一图胜千言，直接看下面的演示图就明白了）：<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
点击菜单File&rarr;Project Structure，进入&ldquo;Project Structure&rdquo;对话框，点击左侧的&ldquo;Modules&rdquo;，然后点击中部的&ldquo;Dependencies&rdquo;tab，然后再点击右部的&ldquo;+&rdquo;号添加你指定的目录，这里要注意了，在弹出的&ldquo;Attach Files&rdquo;对话框中，你必须要选择&ldquo;classes&rdquo;（如图所示）。最后，把目录添加进来之后，再把它们对应的&ldquo;Scope&rdquo;设置为&ldquo;Test&rdquo;，表示在测试中会依赖这个目录，即可。<br />
<img decoding="async" alt="add a file or directory to the classpath of an IntelliJ Java project" src="http://www.codelast.com/wp-content/uploads/2013/03/project_structure_with_demo_final.png" style="width: 576px; height: 218px; " /><br />
<strong><span style="color:#ff0000;">图片可保存下来放大查看。</span></strong></p>
<p>以上方法在IntelliJ 12.0.4中亲测通过，对于其他版本的IntelliJ也应该类似。</p>
<p><span style="background-color:#00ff00;">【补充】</span><br />
12.1.4版的IntelliJ与上面的图略有不同。在点击了&ldquo;+&rdquo;号之后，会弹出一个菜单：</p>
<div style="text-align: center;">
	<img decoding="async" alt="add menu" src="http://www.codelast.com/wp-content/uploads/2013/03/add_menu.png" style="width: 158px; height: 89px;" /></div>
<p>选择&ldquo;Jars or directories...&rdquo;，然后会弹出对话框让你选择目录或文件，选择好之后，和上面一样，要选择&ldquo;classes&rdquo;，后面的操作也一样了。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushJava.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9b%ae%e5%bd%95%e5%88%b0intellij%e9%a1%b9%e7%9b%ae%e7%9a%84classpath%e4%b8%ad/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创]（翻译）Java版的各种Thrift server实现的比较</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%ef%bc%88%e7%bf%bb%e8%af%91%ef%bc%89java%e7%89%88%e7%9a%84%e5%90%84%e7%a7%8dthrift-server%e5%ae%9e%e7%8e%b0%e7%9a%84%e6%af%94%e8%be%83/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%ef%bc%88%e7%bf%bb%e8%af%91%ef%bc%89java%e7%89%88%e7%9a%84%e5%90%84%e7%a7%8dthrift-server%e5%ae%9e%e7%8e%b0%e7%9a%84%e6%af%94%e8%be%83/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 03 Aug 2012 18:53:59 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Thrift server]]></category>
		<category><![CDATA[比较]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=4824</guid>

					<description><![CDATA[<p>
本文是我对这篇文章的翻译：<a href="https://github.com/m1ch1/mapkeeper/wiki/Thrift-Java-Servers-Compared" rel="noopener noreferrer" target="_blank"><span style="color:#0000ff;"><span style="background-color:#add8e6;">Thrift Java Servers Compared</span></span></a>，为了便于阅读，我将原文附于此处，翻译穿插在其中。此外，为了防止原链接在未来某一天失效后，文中的图片再也看不到的问题，我将原文中的图片也保存到了本站的服务器上，我不知道github或原作者是否允许这样做，但我翻译本文仅在于传播知识的目的，在此向原作者和github表示深深的感谢：感谢你们分享了这样好的文章。<br />
&#160; &#160; &#160; &#160;<br />
&#160; &#160; &#160; &#160;<br />
&#160; &#160; &#160; &#160;<br />
<span style="font-size:20px;"><span style="color:#b22222;"><strong><span style="background-color:#f0fff0;">Thrift Java Servers Compared</span></strong></span></span><br />
This article talks only about Java servers. See&#160;<a href="https://github.com/m1ch1/mapkeeper/wiki/TThreadedServer-vs.-TNonblockingServer" rel="noopener noreferrer" target="_blank">this page</a>&#160;if you are interested in C++ servers.<br />
<span style="color:#0000ff;">本文仅讨论Java版的Thrift server.如果你对C++版的感兴趣，请参考&#160;</span>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%ef%bc%88%e7%bf%bb%e8%af%91%ef%bc%89java%e7%89%88%e7%9a%84%e5%90%84%e7%a7%8dthrift-server%e5%ae%9e%e7%8e%b0%e7%9a%84%e6%af%94%e8%be%83/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
本文是我对这篇文章的翻译：<a href="https://github.com/m1ch1/mapkeeper/wiki/Thrift-Java-Servers-Compared" rel="noopener noreferrer" target="_blank"><span style="color:#0000ff;"><span style="background-color:#add8e6;">Thrift Java Servers Compared</span></span></a>，为了便于阅读，我将原文附于此处，翻译穿插在其中。此外，为了防止原链接在未来某一天失效后，文中的图片再也看不到的问题，我将原文中的图片也保存到了本站的服务器上，我不知道github或原作者是否允许这样做，但我翻译本文仅在于传播知识的目的，在此向原作者和github表示深深的感谢：感谢你们分享了这样好的文章。<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
<span style="font-size:20px;"><span style="color:#b22222;"><strong><span style="background-color:#f0fff0;">Thrift Java Servers Compared</span></strong></span></span><br />
This article talks only about Java servers. See&nbsp;<a href="https://github.com/m1ch1/mapkeeper/wiki/TThreadedServer-vs.-TNonblockingServer" rel="noopener noreferrer" target="_blank">this page</a>&nbsp;if you are interested in C++ servers.<br />
<span style="color:#0000ff;">本文仅讨论Java版的Thrift server.如果你对C++版的感兴趣，请参考&nbsp;</span><a href="https://github.com/m1ch1/mapkeeper/wiki/TThreadedServer-vs.-TNonblockingServer" rel="noopener noreferrer" target="_blank"><span style="color:#800080;">这个</span></a><span style="color:#800080;">&nbsp;</span><span style="color:#0000ff;">页面。</span><br />
<span id="more-4824"></span><br />
<a href="http://thrift.apache.org/">Thrift</a><a href="http://thrift.apache.org/" rel="noopener noreferrer" target="_blank">&nbsp;</a>is a cross-language serialization/RPC framework with three major components, protocol, transport, and server. Protocol defines how messages are serialized. Transport defines how messages are communicated between client and server. Server receives serialized messages from the transport, deserializes them according to the protocol and invokes user-defined message handlers, and serializes the responses from the handlers and writes them back to the transport. The modular architecture of Thrift allows it to offer various choices of servers. Here are the list of server available for Java:<br />
<a href="http://thrift.apache.org/"><span style="color:#b22222;">Thrift</span></a><a href="http://thrift.apache.org/" rel="noopener noreferrer" target="_blank"><span style="color:#b22222;">&nbsp;</span></a><span style="color:#0000ff;">是一个跨语言的序列化/RPC框架，它含有三个主要的组件：protocol，transport和server，其中，protocol定义了消息是怎样序列化的，transport定义了消息是怎样在客户端和服务器端之间通信的，server用于从transport接收序列化的消息，根据protocol反序列化之，调用用户定义的消息处理器，并序列化消息处理器的响应，然后再将它们写回transport。Thrift模块化的结构使得它能提供各种server实现。下面列出了Java中可用的server实现：</span><br />
&middot;&nbsp;<a href="http://svn.apache.org/viewvc/thrift/trunk/lib/java/src/org/apache/thrift/server/TSimpleServer.java?view=markup" rel="noopener noreferrer" target="_blank">TSimpleServer</a><br />
&middot;&nbsp;<a href="http://svn.apache.org/viewvc/thrift/trunk/lib/java/src/org/apache/thrift/server/TNonblockingServer.java?view=markup" rel="noopener noreferrer" target="_blank">TNonblockingServer</a><br />
&middot;&nbsp;<a href="http://svn.apache.org/viewvc/thrift/trunk/lib/java/src/org/apache/thrift/server/THsHaServer.java?view=markup" rel="noopener noreferrer" target="_blank">THsHaServer</a><br />
&middot;&nbsp;<a href="http://svn.apache.org/viewvc/thrift/trunk/lib/java/src/org/apache/thrift/server/TThreadedSelectorServer.java?view=markup" rel="noopener noreferrer" target="_blank">TThreadedSelectorServer</a><br />
&middot;&nbsp;<a href="http://svn.apache.org/viewvc/thrift/trunk/lib/java/src/org/apache/thrift/server/TThreadPoolServer.java?view=markup" rel="noopener noreferrer" target="_blank">TThreadPoolServer</a><br />
Having choices is great, but which server is right for you? In this article, I&#39;ll describe the differences among all those servers and show benchmark results to illustrate performance characteristics (the details of the benchmark is explained in Appendix B). Let&#39;s start with the simplest one: TSimpleServer.<br />
<span style="color:#0000ff;">有多个选择很好，但是哪个适合你呢？在本文中，我将描述这些server之间的区别，并展示测试结果，以说明它们的性能特点（测试的细节在附录B中）。下面，我们就从最简单的开始：TSimpleServer。</span><br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<div>
	<strong>TSimpleServer</strong><br />
	<strong>TSimpleServer</strong>&nbsp;accepts a connection, processes requests from the connection until the client closes the connection, and goes back to accept a new connection. Since&nbsp;<strong><em>it is all done in a single thread with blocking I/O</em></strong>, it can only serve one client connection, and all the other clients will have to wait until they get accepted. TSimpleServer is mainly used for testing purpose. Don&#39;t use it in production!<br />
	<span style="color:#0000ff;">TSimplerServer接受一个连接，处理连接请求，直到客户端关闭了连接，它才回去接受一个新的连接。正因为它只在一个单独的线程中以阻塞I/O的方式完成这些工作，所以它只能服务一个客户端连接，其他所有客户端在被服务器端接受之前都只能等待。TSimpleServer主要用于测试目的，不要在生产环境中使用它！</span><br />
	<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<div>
		<strong>TNonblockingServer vs. THsHaServer</strong><br />
		<strong>TNonblockingServer</strong>&nbsp;solves the problem with TSimpleServer of one client blocking all the other clients by using non-blocking I/O. It uses<a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/channels/Selector.html" rel="noopener noreferrer" target="_blank">java.nio.channels.Selector</a>, which allows you to get blocked on multiple connections instead of a single connection by calling&nbsp;<a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/channels/Selector.html#select%28%29" rel="noopener noreferrer" target="_blank">select()</a>. The select()&nbsp;call returns when one ore more connections are ready to be accepted/read/written. TNonblockingServer handles those connections either by accepting it, reading data from it, or writing data to it, and calls&nbsp;select()&nbsp;again to wait for the next available connections. This way, multiple clients can be served without one client starving others.<br />
		<span style="color:#0000ff;">TNonblockingServer使用非阻塞的I/O解决了TSimpleServer一个客户端阻塞其他所有客户端的问题。它使用了<a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/channels/Selector.html" rel="noopener noreferrer" target="_blank">java.nio.channels.Selector</a>，通过调用<a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/channels/Selector.html#select%28%29" rel="noopener noreferrer" target="_blank">select()</a>，它使得你阻塞在多个连接上，而不是阻塞在单一的连接上。当一或多个连接准备好被接受/读/写时，select()调用便会返回。TNonblockingServer处理这些连接的时候，要么接受它，要么从它那读数据，要么把数据写到它那里，然后再次调用select()来等待下一个可用的连接。通用这种方式，server可同时服务多个客户端，而不会出现一个客户端把其他客户端全部&ldquo;饿死&rdquo;的情况。</span></p>
<p>		There is a catch, however.&nbsp;<strong><em>Messages are processed by the same thread that calls&nbsp;</em></strong><strong><em>select()</em></strong>. Let&#39;s say there are 10 clients, and each message takes 100 ms to process. What would be the latency and throughput? While a message is being processed, 9 clients are waiting to be selected, so it takes 1 second for the clients to get the response back from the server, and throughput will be 10 requests / second. Wouldn&#39;t it be great if multiple messages can be processed simultaneously?<br />
		<span style="color:#0000ff;">然而，还有个棘手的问题：所有消息是被调用select()方法的同一个线程处理的。假设有10个客户端，处理每条消息所需时间为100毫秒，那么，latency和吞吐量分别是多少？当一条消息被处理的时候，其他9个客户端就等着被select，所以客户端需要等待1秒钟才能从服务器端得到回应，吞吐量就是10个请求/秒。如果可以同时处理多条消息的话，会很不错吧？</span></p>
<p>		This is where&nbsp;<strong>THsHaServer</strong>&nbsp;(Half-Sync/Half-Async server) comes into picture. It uses a single thread for network I/O, and a separate pool of worker threads to handle message processing. This way messages will get processed immediately if there is an idle worker threads, and multiple messages can be processed concurrently. Using the example above, now the latency is 100 ms and throughput will be 100 requests / sec.<br />
		<span style="color:#0000ff;">因此，THsHaServer（半同步/半异步的server）就应运而生了。它使用一个单独的线程来处理网络I/O，一个独立的worker线程池来处理消息。这样，只要有空闲的worker线程，消息就会被立即处理，因此多条消息能被并行处理。用上面的例子来说，现在的latency就是100毫秒，而吞吐量就是100个请求/秒。</span></p>
<p>		To demonstrate this, I ran a benchmark with 10 clients and a modified message handler that simply sleeps for 100 ms before returning. I used THsHaServer with 10 worker threads. The handler looks something like this:<br />
		<span style="color:#0000ff;">为了演示，我做了一个测试，有10客户端和一个修改过的消息处理器&mdash;&mdash;它的功能仅仅是在返回之前简单地sleep 100毫秒。我使用的是有10个worker线程的THsHaServer。消息处理器的代码看上去就像下面这样：</span></p>
<pre class="brush:java;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
public ResponseCode sleep() throws TException
{   
    try {
        Thread.sleep(100);
    } catch (Exception ex) {
    }
    return ResponseCode.Success;
}
</pre>
<div style="text-align: center; ">
			<a href="www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="thrift-java-servers-compared" src="http://www.codelast.com/wp-content/uploads/2012/08/thrift-java-servers-compared-1.png" style="width: 600px; height: 301px; " /></a></div>
<div style="text-align: center; ">
			<a href="www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="thrift-java-servers-compared" src="http://www.codelast.com/wp-content/uploads/2012/08/thrift-java-servers-compared-2.png" style="width: 600px; height: 301px; " /></a></div>
<p>		The results are as expected. THsHaServer is able to process all the requests concurrently, while TNonblockingServer processes requests one at a time.</p></div>
<p>	<span style="color:#0000ff;">结果正如我们想像的那样，THsHaServer能够并行处理所有请求，而TNonblockingServer只能一次处理一个请求。</span><br />
	<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<div>
		<strong>THsHaServer vs. TThreadedSelectorServer</strong></div>
<p>	Thrift 0.8 introduced yet another server,&nbsp;<strong>TThreadedSelectorServer</strong>. The main difference between TThreadedSelectorServer and THsHaServer is that&nbsp;<strong><em>TThreadedSelectorServer allows you to have multiple threads for network I/O</em></strong>. It maintains 2 thread pools, one for handling network I/O, and one for handling request processing. TThreadedSelectorServer performs better than THsHaServer when the network io is the bottleneck. To show the difference, I ran a benchmark with a handler that returns immediately without doing anything, and measured the average latency and throughput with varying number of clients. I used 32 worker threads for THsHaServer, and 16 worker threads/16 selector threads for TThreadedSelectorServer.<br />
	<span style="color:#0000ff;">Thrift 0.8引入了另一种server实现，即TThreadedSelectorServer。它与THsHaServer的主要区别在于，TThreadedSelectorServer允许你用多个线程来处理网络I/O。它维护了两个线程池，一个用来处理网络I/O，另一个用来进行请求的处理。当网络I/O是瓶颈的时候，TThreadedSelectorServer比THsHaServer的表现要好。为了展现它们的区别，我进行了一个测试，令其消息处理器在不做任何工作的情况下立即返回，以衡量在不同客户端数量的情况下的平均latency和吞吐量。对THsHaServer，我使用32个worker线程；对TThreadedSelectorServer，我使用16个worker线程和16个selector线程。</span></p>
<div style="text-align: center; ">
		<a href="www.codelast.com" rel="noopener noreferrer" style="text-align: center; " target="_blank"><img decoding="async" alt="thrift-java-servers-compared" src="http://www.codelast.com/wp-content/uploads/2012/08/thrift-java-servers-compared-3.png" style="width: 600px; height: 301px; " /></a></div>
<div style="text-align: center; ">
		<a href="www.codelast.com" rel="noopener noreferrer" style="text-align: center; " target="_blank"><img decoding="async" alt="thrift-java-servers-compared" src="http://www.codelast.com/wp-content/uploads/2012/08/thrift-java-servers-compared-4.png" style="width: 600px; height: 301px; " /></a></div>
<p>	The result shows that TThreadedSelectorServer has much higher throughput than THsHaServer while maintaining lower latency.<br />
	<span style="color:#0000ff;">结果显示，TThreadedSelectorServer比THsHaServer的吞吐量高得多，并且维持在一个更低的latency上。</span><br />
	<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<div>
		<strong>TThreadedSelectorServer vs. TThreadPoolServer</strong></div>
<p>	Finally, there is&nbsp;<strong>TThreadPoolServer</strong>. TThreadPoolServer is different from the other 3 servers in that:<br />
	<span style="color:#0000ff;">最后，还剩下 TThreadPoolServer。TThreadPoolServer与其他三种server不同的是：</span><br />
	&middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; There is a dedicated thread for&nbsp;<em>accepting</em>&nbsp;connections.<br />
	&middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">有一个专用的线程用来接受连接。</span><br />
	&middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Once a connection is accepted, it gets scheduled to be processed by a worker thread in&nbsp;<a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html">ThreadPoolExecutor</a>.<br />
	&middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">一旦接受了一个连接，它就会被放入ThreadPoolExecutor中的一个worker线程里处理。</span><br />
	&middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><em>The worker thread is tied to the specific client connection until it&#39;s closed</em></strong>. Once the connection is closed, the worker thread goes back to the thread pool.<br />
	&middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;"><strong><em>worker</em></strong><strong><em>线程被绑定到特定的客户端连接上，直到它关闭</em></strong>。一旦连接关闭，该worker线程就又回到了线程池中。</span><br />
	&middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; You can configure both minimum and maximum number of threads in the thread pool. Default values are 5 and&nbsp;Integer.MAX_VALUE, respectively.<br />
	&middot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">你可以配置线程池的最小、最大线程数，默认值分别是5（最小）和Integer.MAX_VALUE（最大）。</span></p>
<p>	This means that if there are 10000 concurrent client connections, you need to run 10000 threads. As such, it is not as resource friendly as other servers. Also, if the number of clients exceeds the maximum number of threads in the thread pool, requests will be blocked until a worker thread becomes available.<br />
	<span style="color:#0000ff;">这意味着，如果有1万个并发的客户端连接，你就需要运行1万个线程。所以它对系统资源的消耗不像其他类型的server一样那么&ldquo;友好&rdquo;。此外，如果客户端数量超过了线程池中的最大线程数，在有一个worker线程可用之前，请求将被一直阻塞在那里。</span></p>
<p>	Having said that, TThreadPoolServer performs very well; on the box I&#39;m using it&#39;s able to support 10000 concurrent clients without any problem. If you know the number of clients that will be connecting to your server in advance and you don&#39;t mind running a lot of threads, TThreadPoolServer might be a good choice for you.<br />
	<span style="color:#0000ff;">我们已经说过，TThreadPoolServer的表现非常优异。在我正在使用的计算机上，它可以支持1万个并发连接而没有任何问题。如果你提前知道了将要连接到你服务器上的客户端数量，并且你不介意运行大量线程的话，TThreadPoolServer对你可能是个很好的选择。</span></p>
<div style="text-align: center; ">
		<a href="www.codelast.com" rel="noopener noreferrer" style="text-align: center; " target="_blank"><img decoding="async" alt="thrift-java-servers-compared" src="http://www.codelast.com/wp-content/uploads/2012/08/thrift-java-servers-compared-5.png" style="width: 600px; height: 301px; " /></a></div>
<div style="text-align: center; ">
		<a href="www.codelast.com" rel="noopener noreferrer" style="text-align: center; " target="_blank"><img decoding="async" alt="thrift-java-servers-compared" src="http://www.codelast.com/wp-content/uploads/2012/08/thrift-java-servers-compared-6.png" style="width: 600px; height: 301px; " /></a></div>
<p>	<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<div>
		<strong>Conclusion</strong><br />
		<span style="color:#0000ff;"><strong>结论</strong></span></div>
<p>	I hope this article helps you decide which Thrift server is right for you. I think TThreadedSelectorServer would be a safe choice for most of the use cases. You might also want to consider TThreadPoolServer if you can afford to run lots of concurrent threads. Feel free to send me email atmapkeeper-users@googlegroups.com&nbsp;or post your comments&nbsp;<a href="http://groups.google.com/group/mapkeeper-users">here</a>&nbsp;if you have any questions/comments.<br />
	<span style="color:#0000ff;">希望本文能帮你做出决定：哪一种Thrift server适合你。我认为TThreadedSelectorServer对大多数案例来说都是个安全之选。如果你的系统资源允许运行大量并发线程的话，你可能会想考虑使用TThreadPoolServer。</span><span style="color:#b22222;"><span style="background-color:#00ff00;">（后面的就不翻译了）</span></span></p>
<div>
		<strong>Appendix A: Hardware Configuration</strong></p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
Processors:     2 x Xeon E5620 2.40GHz (HT enabled, 8 cores, 16 threads)
Memory:         8GB
Network:        1Gb/s &lt;full-duplex&gt;
OS:             RHEL Server 5.4 Linux 2.6.18-164.2.1.el5 x86_64
</pre>
<p></p>
<div>
			<strong>Appendix B: Benchmark Details</strong><br />
			It&#39;s pretty straightforward to run the benchmark yourself. First clone the&nbsp;<a href="https://github.com/m1ch1/mapkeeper">MapKeeper</a>&nbsp;repository and compile the&nbsp;<a href="https://github.com/m1ch1/mapkeeper/blob/master/stubjava/StubServer.java">stub java server</a>:</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
git clone git://github.com/m1ch1/mapkeeper.git
cd mapkeeper/stubjava
make
</pre>
<p>			Then, start the server you like to benchmark:</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
make run mode=threadpool    # run TThreadPoolServer
make run mode=nonblocking   # run TNonblockingServer
make run mode=hsha          # run THsHaServer
make run mode=selector      # run TThreadedSelectorServer
</pre>
<p>
			Then, clone&nbsp;<a href="https://github.com/brianfrankcooper/YCSB">YCSB</a>&nbsp;repository and compile:</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
git clone git://github.com/brianfrankcooper/YCSB.git
cd YCSB
mvn clean package
</pre>
<p>			Once the compilation finishes, you can run YCSB against the stub server:</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./bin/ycsb load mapkeeper -P ./workloads/workloada
</pre>
<p>			For more detailed information about how to use YCSB, check out their&nbsp;<a href="https://github.com/brianfrankcooper/YCSB/wiki/Running-a-Workload">wiki page</a>.</p>
<p>			<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
			<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
			转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
			感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
				<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
</p></div>
</p></div>
</div>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushJava.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushPlain.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%ef%bc%88%e7%bf%bb%e8%af%91%ef%bc%89java%e7%89%88%e7%9a%84%e5%90%84%e7%a7%8dthrift-server%e5%ae%9e%e7%8e%b0%e7%9a%84%e6%af%94%e8%be%83/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
