2015-05-09 74 views
2

所以我想實現使用SparkJava自定義分區,我發現瞭如何做到這一點網上一個很好的例子,但它使用Scala,我不能爲生命我弄清楚它是如何正確轉換成Java的,所以我可以嘗試實現它。誰能幫忙?下面是示例代碼我在Scala找到了它:翻譯Scala代碼與Java的星火分區程序

所有的
class DomainNamePartitioner(numParts: Int) extends Partitioner { 
    override def numPartitions: Int = numParts 
    override def getPartition(key: Any): Int = { 
    val domain = new Java.net.URL(key.toString).getHost() 
    val code = (domain.hashCode % numPartitions) 
    if (code < 0) { 
     code + numPartitions // Make it non-negative 
    } else { 
     code 
    } 
    } 
    // Java equals method to let Spark compare our Partitioner objects 
    override def equals(other: Any): Boolean = other match { 
    case dnp: DomainNamePartitioner => 
     dnp.numPartitions == numPartitions 
    case _ => 
     false 
    } 
} 
+0

嗯,可以模運算真的返回一個負數?或者%在scala中不是模數? – fge

+0

@fge是的,它可以像幾乎所有的C派生語言一樣。 '%'被定義爲餘數,可以是負數。 –

回答

2

首先,Scala是寫Spark的首選。

以下是相應的代碼Java(它不是唯一的版本):

多見於:https://spark.apache.org/docs/latest/api/java/index.html

class DomainNamePartitioner extends Partitioner{ 

private int numParts; 

public Partitioner() 
{ 

} 
public Partitioner(int numParts) 
{ 
    this.numParts = numParts; 
} 

@Override 
public int numPartitions() 
{ 
    return numParts; 
} 
@Override 
public int getPartition(Object key){ 

    String domain = new Java.net.URL(key.toString).getHost(); 
    int code = domain.hashCode % numPartitions; 
    if (code < 0){ 
    return code + this.numPartitions(); 
    }else{ 
    return code; 
    } 
} 

@Override 
public boolean equals(Object obj){ 

    if (obj instanceof DomainNamePartitioner){ 
     DomainNamePartitioner pObj = (DomainNamePartitioner)obj; 
     return pObj.numPartitions() == this.numPartitions; 
    }else{ 
     return false; 
    } 
} 
}