Skip to content

Instantly share code, notes, and snippets.

@niqbal
Last active March 6, 2018 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niqbal/3e293e2bcb800d6912a250d914c9d478 to your computer and use it in GitHub Desktop.
Save niqbal/3e293e2bcb800d6912a250d914c9d478 to your computer and use it in GitHub Desktop.
Experiment with the distribution of documents using Solr composite key router
import com.niqbal.shard.ShardHash.{c, getShardNumber, router, s}
import org.apache.solr.common.cloud.{DocCollection, DocRouter, Slice}
import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.io.Source
class ShardHash {
def createCollection(nSlices: Integer, router: DocRouter): DocCollection = {
val ranges = router.partitionRange(nSlices, router.fullRange).asScala
val slices: Map[String, Slice] =
ranges.zipWithIndex
.map(x => new Slice(s"${x._2}", null, Map("range" -> x._1.asInstanceOf[AnyRef]).asJava))
.map(y => (y.getName, y)).toMap
new DocCollection("collection1", slices.asJava, null, router)
}
}
object ShardHash extends App {
val s = new ShardHash
val c = s.createCollection(117, DocRouter.DEFAULT)
val router = c.getRouter
// System.out.println(c.getSlices.asScala.toList.sortBy(_.getRange).map(x => s"${x.getName} -> ${x.getRange}\n"))
private def getShardNumber(x: String) = {
router.getTargetSlice(x, null, null, null, c).getName
}
val compositeIds = Range(0, 117).map(_.toString).map(x => (getShardNumber(x), x))
val shardGroups = compositeIds.groupBy(x => x._1)
val shardGroupSizes = shardGroups.map(shardMap => (shardMap._1, shardMap._2.size, shardMap._2.map(x=>x._2).toList))
val sortedShardGroupsBySize = shardGroupSizes.toList.sortBy(x => x._2)
val mapOutput = sortedShardGroupsBySize.map(x => s"${x._1}:${x._3}\n")
System.out.println(compositeIds)
System.out.println(shardGroups)
System.out.println(shardGroupSizes)
System.out.println(sortedShardGroupsBySize)
System.out.println(mapOutput)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment