8

カスタムパーティショナーを作成しました。削減タスクの数が1より大きい場合、ジョブは失敗します。これは私が得ている例外です:

 java.io.IOException: Illegal partition for weburl_compositeKey@804746b1 (-1)
 at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:930)
 at org.apache.hadoop.mapred.MapTask$OldOutputCollector.collect(MapTask.java:499)

私が書いたコードは

public int getPartition(weburl_compositeKey key, Text value, int numPartitions)
{
    return (key.hashCode()) % numPartitions;
}

これはkey.hashCode()equals-719988079であり、この値のmodはを返し-1ます。

これについてのあなたの助けに感謝します。ありがとう。

4

3 に答える 3

23

カスタムによって計算されたパーティション番号は、Partitioner負でない必要があります。試す:

public int getPartition(weburl_compositeKey key, Text value, int numPartitions)
{
    return (key.hashCode() & Integer.MAX_VALUE) % numPartitions;
}
于 2013-02-22T19:38:39.680 に答える
4

使用に関する警告:

public int getPartition(weburl_compositeKey key, Text value, int numPartitions)
{
    return Math.abs(key.hashCode()) % numPartitions;
}

key.hashCode()が等しい場合にヒットした場合Integer.MIN_VALUEでも、負のパーティション値が得られます。これはJavaの奇妙な点ですが、 (-2147483648のように)Math.abs(Integer.MIN_VALUE)戻りますInteger.MIN_VALUE。次のように、係数の絶対値を取得する方が安全です。

public int getPartition(weburl_compositeKey key, Text value, int numPartitions)
{
    return Math.abs(key.hashCode() % numPartitions);
}
于 2015-07-15T19:56:27.823 に答える
2

またはあなたは使用することができます

public int getPartition(weburl_compositeKey key, Text value, int numPartitions)
{
    return (key.hashCode() & Integer.MAX_VALUE) % numPartitions;
}
于 2015-01-22T09:25:36.470 に答える