Search in sources :

Example 1 with JsonPathAccess

use of org.zalando.nakadi.util.JsonPathAccess in project nakadi by zalando.

the class HashPartitionStrategy method calculatePartition.

@Override
public String calculatePartition(final EventType eventType, final JSONObject event, final List<String> partitions) throws InvalidPartitionKeyFieldsException {
    final List<String> partitionKeyFields = eventType.getPartitionKeyFields();
    if (partitionKeyFields.isEmpty()) {
        throw new RuntimeException("Applying " + this.getClass().getSimpleName() + " although event type " + "has no partition key fields configured.");
    }
    try {
        final JsonPathAccess traversableJsonEvent = new JsonPathAccess(event);
        final int hashValue = partitionKeyFields.stream().map(pkf -> EventCategory.DATA.equals(eventType.getCategory()) ? DATA_PATH_PREFIX + pkf : pkf).map(Try.wrap(okf -> {
            final String fieldValue = traversableJsonEvent.get(okf).toString();
            return stringHash.hashCode(fieldValue);
        })).map(Try::getOrThrow).mapToInt(hc -> hc).sum();
        int partitionIndex = abs(hashValue) % partitions.size();
        partitionIndex = hashPartitioningCrutch.adjustPartitionIndex(partitionIndex, partitions.size());
        final List<String> sortedPartitions = partitions.stream().sorted().collect(Collectors.toList());
        return sortedPartitions.get(partitionIndex);
    } catch (NakadiRuntimeException e) {
        final Exception original = e.getException();
        if (original instanceof InvalidPartitionKeyFieldsException) {
            throw (InvalidPartitionKeyFieldsException) original;
        } else {
            throw e;
        }
    }
}
Also used : EventCategory(org.zalando.nakadi.domain.EventCategory) EventType(org.zalando.nakadi.domain.EventType) JsonSchemaEnrichment(org.zalando.nakadi.validation.JsonSchemaEnrichment) Autowired(org.springframework.beans.factory.annotation.Autowired) Try(org.zalando.nakadi.exceptions.Try) Math.abs(java.lang.Math.abs) Collectors(java.util.stream.Collectors) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) Component(org.springframework.stereotype.Component) List(java.util.List) JSONObject(org.json.JSONObject) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException) JsonPathAccess(org.zalando.nakadi.util.JsonPathAccess) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) JsonPathAccess(org.zalando.nakadi.util.JsonPathAccess) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException) Try(org.zalando.nakadi.exceptions.Try) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException)

Aggregations

Math.abs (java.lang.Math.abs)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 JSONObject (org.json.JSONObject)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1 EventCategory (org.zalando.nakadi.domain.EventCategory)1 EventType (org.zalando.nakadi.domain.EventType)1 InvalidPartitionKeyFieldsException (org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException)1 NakadiRuntimeException (org.zalando.nakadi.exceptions.NakadiRuntimeException)1 Try (org.zalando.nakadi.exceptions.Try)1 JsonPathAccess (org.zalando.nakadi.util.JsonPathAccess)1 JsonSchemaEnrichment (org.zalando.nakadi.validation.JsonSchemaEnrichment)1