“`” 解答:

数据倾斜:map /reduce程序执行时,reduce节点大部分执行完毕,但是有一个或者几个reduce节点运行很慢,导致整个程序的处理时间很长,这是因为某一个key的条数比其他key多很多(有时是百倍或者千倍之多),这条key所在的reduce节点所处理的数据量比其他节点就大很多,从而导致某几个节点迟迟运行不完,此称之为数据倾斜。

用hadoop程序进行数据关联时,常碰到数据倾斜的情况,这里提供一种解决方法。

自己实现partition类,用key和value相加取hash值:

方式1:

源代码:

<pre><code>public int getPartition(K key, V value,int numReduceTasks) {
return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
}
</code></pre>

修改后

<pre><code>public int getPartition(K key, V value,int numReduceTasks) {
return (((key).hashCode()+value.hashCode()) & Integer.MAX_VALUE) % numReduceTasks;
}
</code></pre>

方式2:

<pre><code>public class HashPartitioner<K, V> extends Partitioner<K, V> {

private int aa= 0;

/** Use {@link Object#hashCode()} to partition. */

public int getPartition(K key, V value,
int numReduceTasks) {

return (key.hashCode()+(aa++) & Integer.MAX_VALUE) % numReduceTasks;
}
</code></pre>

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.