Skip to content

Instantly share code, notes, and snippets.

@solidpple
Created July 26, 2016 00:09
Show Gist options
  • Save solidpple/b547a24e274e9f0bdbcd6ad1597ccfff to your computer and use it in GitHub Desktop.
Save solidpple/b547a24e274e9f0bdbcd6ad1597ccfff to your computer and use it in GitHub Desktop.
// 각 URL의 도메인 네임을 해싱해 DomainNamePartitioner를 구현
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)
// 자바의 hashCode()에 기반해서 구현을 한다면, 음수를 리턴할 수 있으므로 getPartition()이 음수를 반환하지 않도록 주의해야 한다.
if (code < 0) {
code + numPartitions
} else {
code
}
}
override def equals(other: Any): Boolean = other match {
case dnp: DomainNamePartitioner =>
dnp.numPartitions == numPartitions
case _ =>
false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment