final String zkAddr = args[0];
final int numNodes = Integer.parseInt(args[1]);
final String clusterName = DEFAULT_CLUSTER_NAME;
ZkClient zkclient = null;
try
{
zkclient = new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT,
ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
// add cluster
admin.addCluster(clusterName, true);
// add state model definition
StateModelConfigGenerator generator = new StateModelConfigGenerator();
admin.addStateModelDef(clusterName, DEFAULT_STATE_MODEL,
new StateModelDefinition(generator.generateConfigForOnlineOffline()));
// addNodes
for (int i = 0; i < numNodes; i++)
{
String port = "" +(12001+ i);
String serverId = "localhost_"+ port;
InstanceConfig config = new InstanceConfig(serverId);
config.setHostName("localhost");
config.setPort(port);
config.setInstanceEnabled(true);
admin.addInstance(clusterName, config);
}
// add resource "repository" which has 1 partition
String resourceName = DEFAULT_RESOURCE_NAME;
admin.addResource(clusterName, resourceName, DEFAULT_PARTITION_NUMBER,
DEFAULT_STATE_MODEL, IdealStateModeProperty.AUTO.toString());
admin.rebalance(clusterName, resourceName, 1);
} finally
{
if (zkclient != null)
{
zkclient.close();
}
}
}