use of org.apache.samza.container.TaskName in project samza by apache.
the class GroupByContainerCount method buildContainerModels.
/**
* Translates the list of TaskGroup instances to a set of ContainerModel instances, using the
* set of TaskModel instances.
*
* @param tasks the TaskModels to assign to the ContainerModels.
* @param containerTasks the TaskGroups defining how the tasks should be grouped.
* @return a mutable set of ContainerModels.
*/
private Set<ContainerModel> buildContainerModels(Set<TaskModel> tasks, List<TaskGroup> containerTasks) {
// Map task names to models
Map<String, TaskModel> taskNameToModel = new HashMap<>();
for (TaskModel model : tasks) {
taskNameToModel.put(model.getTaskName().getTaskName(), model);
}
// Build container models
Set<ContainerModel> containerModels = new HashSet<>();
for (TaskGroup container : containerTasks) {
Map<TaskName, TaskModel> containerTaskModels = new HashMap<>();
for (String taskName : container.taskNames) {
TaskModel model = taskNameToModel.get(taskName);
containerTaskModels.put(model.getTaskName(), model);
}
containerModels.add(new ContainerModel(container.containerId, Integer.valueOf(container.containerId), containerTaskModels));
}
return Collections.unmodifiableSet(containerModels);
}
use of org.apache.samza.container.TaskName in project samza by apache.
the class SamzaObjectMapper method getObjectMapper.
/**
* @return Returns a new ObjectMapper that's been configured to (de)serialize
* Samza's job data model, and simple data types such as TaskName,
* Partition, Config, and SystemStreamPartition.
*/
public static ObjectMapper getObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("SamzaModule", new Version(1, 0, 0, ""));
// Setup custom serdes for simple data types.
module.addSerializer(Partition.class, new PartitionSerializer());
module.addSerializer(SystemStreamPartition.class, new SystemStreamPartitionSerializer());
module.addKeySerializer(SystemStreamPartition.class, new SystemStreamPartitionKeySerializer());
module.addSerializer(TaskName.class, new TaskNameSerializer());
module.addDeserializer(Partition.class, new PartitionDeserializer());
module.addDeserializer(SystemStreamPartition.class, new SystemStreamPartitionDeserializer());
module.addKeyDeserializer(SystemStreamPartition.class, new SystemStreamPartitionKeyDeserializer());
module.addDeserializer(Config.class, new ConfigDeserializer());
// Setup mixins for data models.
mapper.getSerializationConfig().addMixInAnnotations(TaskModel.class, JsonTaskModelMixIn.class);
mapper.getDeserializationConfig().addMixInAnnotations(TaskModel.class, JsonTaskModelMixIn.class);
mapper.getSerializationConfig().addMixInAnnotations(ContainerModel.class, JsonContainerModelMixIn.class);
mapper.getSerializationConfig().addMixInAnnotations(JobModel.class, JsonJobModelMixIn.class);
mapper.getDeserializationConfig().addMixInAnnotations(JobModel.class, JsonJobModelMixIn.class);
module.addDeserializer(ContainerModel.class, new JsonDeserializer<ContainerModel>() {
@Override
public ContainerModel deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
int containerId = node.get("container-id").getIntValue();
if (node.get("container-id") == null) {
throw new SamzaException("JobModel did not contain a container-id. This can never happen. JobModel corrupt!");
}
String processorId;
if (node.get("processor-id") == null) {
processorId = String.valueOf(containerId);
} else {
processorId = node.get("processor-id").getTextValue();
}
Map<TaskName, TaskModel> tasksMapping = OBJECT_MAPPER.readValue(node.get("tasks"), new TypeReference<Map<TaskName, TaskModel>>() {
});
return new ContainerModel(processorId, containerId, tasksMapping);
}
});
// Convert camel case to hyphenated field names, and register the module.
mapper.setPropertyNamingStrategy(new CamelCaseToDashesStrategy());
mapper.registerModule(module);
return mapper;
}
use of org.apache.samza.container.TaskName in project samza by apache.
the class MockTaskProxy method getJobModel.
@Override
protected JobModel getJobModel(JobInstance jobInstance) {
if (jobInstance.getJobId().contains("Bad") || jobInstance.getJobName().contains("Bad")) {
throw new IllegalArgumentException("No tasks found.");
}
TaskModel task1Model = new TaskModel(new TaskName(TASK_1_NAME), SYSTEM_STREAM_PARTITIONS, CHANGE_LOG_PARTITION);
TaskModel task2Model = new TaskModel(new TaskName(TASK_2_NAME), SYSTEM_STREAM_PARTITIONS, CHANGE_LOG_PARTITION);
ContainerModel task1ContainerModel = new ContainerModel(TASK_1_CONTAINER_ID, 1, ImmutableMap.of(new TaskName(TASK_1_NAME), task1Model));
ContainerModel task2ContainerModel = new ContainerModel(TASK_2_CONTAINER_ID, 2, ImmutableMap.of(new TaskName(TASK_2_NAME), task2Model));
return new JobModel(new MapConfig(), ImmutableMap.of(TASK_1_CONTAINER_ID, task1ContainerModel, TASK_2_CONTAINER_ID, task2ContainerModel));
}
Aggregations