<?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>三元运算符 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E4%B8%89%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Wed, 15 Nov 2023 08:02:02 +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>[原创] 使用Apache DataFu中的Coalesce()简化Apache Pig的三元运算符</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8apache-datafu%e4%b8%ad%e7%9a%84coalesce%e7%ae%80%e5%8c%96apache-pig%e7%9a%84%e4%b8%89%e5%85%83%e8%bf%90%e7%ae%97%e7%ac%a6/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8apache-datafu%e4%b8%ad%e7%9a%84coalesce%e7%ae%80%e5%8c%96apache-pig%e7%9a%84%e4%b8%89%e5%85%83%e8%bf%90%e7%ae%97%e7%ac%a6/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 04 Dec 2020 03:15:31 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[Apache DataFu]]></category>
		<category><![CDATA[apache pig]]></category>
		<category><![CDATA[Coalesce]]></category>
		<category><![CDATA[UDF]]></category>
		<category><![CDATA[三元运算符]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13139</guid>

					<description><![CDATA[<p>查看更多Apache Pig的教程请点击<a href="https://www.codelast.com/?p=4550" rel="noopener" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>来看这个例子。有数据文件 1.txt，内容为：</p>
<blockquote>
<div>
		a<span style="color:#ff0000;">[\t]</span><span style="color: rgb(255, 0, 0);">[\t]</span>c</div>
<div>
		<span style="color: rgb(255, 0, 0);">[\t]</span>f<span style="color: rgb(255, 0, 0);">[\t]</span>g</div>
<div>
		h<span style="color: rgb(255, 0, 0);">[\t]</span>k<span style="color: rgb(255, 0, 0);">[\t]</span><br />
		<span style="color: rgb(255, 0, 0);">[\t]</span><span style="color: rgb(255, 0, 0);">[\t]</span><span style="color: rgb(255, 0, 0);">[\t]</span></div>
</blockquote>
<div>
	其中 <span style="color:#ff0000;">[\t]</span>&#160;表示制表符(tab)，并不是真的在文件中写了&#160;<span style="color: rgb(255, 0, 0);">[\t]</span>。<br />
	在Pig命令行交互模式下加载这个文件，并把其中为空（NULL）的列替换成一些数字：
<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="sql language-sql 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;">A&#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;">LOAD</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;">&#39;1.txt&#39;</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;">AS</span>&#160;(col1:&#160;chararray,&#160;col2:&#160;chararray,&#160;col3:&#160;chararray);
B&#160;=&#160;FOREACH&#160;A&#160;GENERATE&#160;(col1&#160;IS&#160;NOT&#160;NULL&#160;?&#160;col1&#160;:&#160;&#39;1&#39;)&#160;AS&#160;col1,&#160;(col2&#160;IS&#160;NOT&#160;NULL&#160;?&#160;col2&#160;:&#160;&#39;2&#39;)&#160;AS&#160;col2,&#160;(col3&#160;IS&#160;NOT&#160;NULL&#160;?&#160;col3&#160;:&#160;&#39;3&#39;)&#160;AS&#160;col3;
DUMP&#160;B;
</code></pre>
</section>
<p>输出：</p>
<blockquote>
<div>
			(a,2,c)</div>
<div>
			(1,f,g)</div>
<div>
			(h,k,3)</div>
<div>
			(1,2,3)</div>
</blockquote>
<p>	代码非常简单：如果第一列col1为空则替换为1，如果第二列为空则替换为2，如果第三列为空则替换为3。<br />
	这里使用了三元运算符 <span style="color:#0000ff;">?</span></p></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8apache-datafu%e4%b8%ad%e7%9a%84coalesce%e7%ae%80%e5%8c%96apache-pig%e7%9a%84%e4%b8%89%e5%85%83%e8%bf%90%e7%ae%97%e7%ac%a6/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>查看更多Apache Pig的教程请点击<a href="https://www.codelast.com/?p=4550" rel="noopener" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>来看这个例子。有数据文件 1.txt，内容为：</p>
<blockquote>
<div>
		a<span style="color:#ff0000;">[\t]</span><span style="color: rgb(255, 0, 0);">[\t]</span>c</div>
<div>
		<span style="color: rgb(255, 0, 0);">[\t]</span>f<span style="color: rgb(255, 0, 0);">[\t]</span>g</div>
<div>
		h<span style="color: rgb(255, 0, 0);">[\t]</span>k<span style="color: rgb(255, 0, 0);">[\t]</span><br />
		<span style="color: rgb(255, 0, 0);">[\t]</span><span style="color: rgb(255, 0, 0);">[\t]</span><span style="color: rgb(255, 0, 0);">[\t]</span></div>
</blockquote>
<div>
	其中 <span style="color:#ff0000;">[\t]</span>&nbsp;表示制表符(tab)，并不是真的在文件中写了&nbsp;<span style="color: rgb(255, 0, 0);">[\t]</span>。<br />
	在Pig命令行交互模式下加载这个文件，并把其中为空（NULL）的列替换成一些数字：</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="sql language-sql 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;">A&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;">LOAD</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;">&#39;1.txt&#39;</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;">AS</span>&nbsp;(col1:&nbsp;chararray,&nbsp;col2:&nbsp;chararray,&nbsp;col3:&nbsp;chararray);
B&nbsp;=&nbsp;FOREACH&nbsp;A&nbsp;GENERATE&nbsp;(col1&nbsp;IS&nbsp;NOT&nbsp;NULL&nbsp;?&nbsp;col1&nbsp;:&nbsp;&#39;1&#39;)&nbsp;AS&nbsp;col1,&nbsp;(col2&nbsp;IS&nbsp;NOT&nbsp;NULL&nbsp;?&nbsp;col2&nbsp;:&nbsp;&#39;2&#39;)&nbsp;AS&nbsp;col2,&nbsp;(col3&nbsp;IS&nbsp;NOT&nbsp;NULL&nbsp;?&nbsp;col3&nbsp;:&nbsp;&#39;3&#39;)&nbsp;AS&nbsp;col3;
DUMP&nbsp;B;
</code></pre>
</section>
<p>输出：</p>
<blockquote>
<div>
			(a,2,c)</div>
<div>
			(1,f,g)</div>
<div>
			(h,k,3)</div>
<div>
			(1,2,3)</div>
</blockquote>
<p>	代码非常简单：如果第一列col1为空则替换为1，如果第二列为空则替换为2，如果第三列为空则替换为3。<br />
	这里使用了三元运算符 <span style="color:#0000ff;">? :</span> 来做这个判断，写法非常丑陋。<br />
	<span id="more-13139"></span><br />
	如果要实现更麻烦的逻辑，&ldquo;取第一列、第二列、第三列中，第1个不为空的列作为结果，如果全为空则取888&rdquo;：</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="sql language-sql 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;">A&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;">LOAD</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;">&#39;1.txt&#39;</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;">AS</span>&nbsp;(col1:&nbsp;chararray,&nbsp;col2:&nbsp;chararray,&nbsp;col3:&nbsp;chararray);
B&nbsp;=&nbsp;FOREACH&nbsp;A&nbsp;GENERATE&nbsp;(col1&nbsp;IS&nbsp;NOT&nbsp;NULL&nbsp;?&nbsp;col1&nbsp;:&nbsp;(col2&nbsp;IS&nbsp;NOT&nbsp;NULL&nbsp;?&nbsp;col2&nbsp;:&nbsp;(col3&nbsp;IS&nbsp;NOT&nbsp;NULL&nbsp;?&nbsp;col3&nbsp;:&nbsp;&#39;888&#39;)))&nbsp;AS&nbsp;col;
DUMP&nbsp;B;
</code></pre>
</section>
<p>输出：</p>
<blockquote>
<div>
			(a)</div>
<div>
			(f)</div>
<div>
			(h)</div>
<div>
			(888)</div>
</blockquote>
<p>	这代码是真&bull;丑出天际。<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 />
	这时，我们就可以使用<a href="https://datafu.apache.org/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Apache DataFu</span></a>中的&nbsp;<a href="https://github.com/apache/datafu/blob/master/datafu-pig/src/main/java/datafu/pig/util/Coalesce.java" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Coalesce()</span></a>&nbsp;函数来简化代码。</div>
<blockquote>
<div>
		Apache DataFu&trade;是用于在Hadoop中处理大规模数据的库的集合。</div>
</blockquote>
<div>
	Coalesce()的功能：</div>
<blockquote>
<div>
		返回一个元组(tuple)的第一个非空值，就像SQL中的Coalesce一样。</div>
</blockquote>
<div>
	简化上面的第一段代码：</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="sql language-sql 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;">DEFINE&nbsp;Coalesce&nbsp;datafu.pig.util.Coalesce();

A&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;">LOAD</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;">&#39;1.txt&#39;</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;">AS</span>&nbsp;(col1:&nbsp;chararray,&nbsp;col2:&nbsp;chararray,&nbsp;col3:&nbsp;chararray);
B&nbsp;=&nbsp;FOREACH&nbsp;A&nbsp;GENERATE&nbsp;Coalesce(col1,&nbsp;&#39;1&#39;)&nbsp;AS&nbsp;col1,&nbsp;Coalesce(col2,&nbsp;&#39;2&#39;)&nbsp;AS&nbsp;col2,&nbsp;Coalesce(col3,&nbsp;&#39;3&#39;)&nbsp;AS&nbsp;col3;
DUMP&nbsp;B;
</code></pre>
</section>
<p>输出结果完全一样。<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>
<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="sql language-sql 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;">DEFINE&nbsp;Coalesce&nbsp;datafu.pig.util.Coalesce();

A&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;">LOAD</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;">&#39;1.txt&#39;</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;">AS</span>&nbsp;(col1:&nbsp;chararray,&nbsp;col2:&nbsp;chararray,&nbsp;col3:&nbsp;chararray);
B&nbsp;=&nbsp;FOREACH&nbsp;A&nbsp;GENERATE&nbsp;Coalesce(col1,&nbsp;col2,&nbsp;col3,&nbsp;&#39;888&#39;)&nbsp;AS&nbsp;col;
DUMP&nbsp;B;
</code></pre>
</section>
<p>三元运算符嵌套的层数越多，使用Coalesce能简化得就越多。<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>
</div>
<div>
	&nbsp;</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8apache-datafu%e4%b8%ad%e7%9a%84coalesce%e7%ae%80%e5%8c%96apache-pig%e7%9a%84%e4%b8%89%e5%85%83%e8%bf%90%e7%ae%97%e7%ac%a6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
