Search in sources :

Example 1 with InvalidPartitionKeyFieldsException

use of org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException in project nakadi by zalando.

the class JsonPathAccess method get.

public Object get(final String path) throws InvalidPartitionKeyFieldsException {
    final JsonPathTokenizer pathTokenizer = new JsonPathTokenizer(path);
    Object curr = this.jsonObject;
    String field;
    while ((field = pathTokenizer.nextToken()) != null) {
        if (!(curr instanceof JSONObject)) {
            throw new InvalidPartitionKeyFieldsException("field " + field + " doesn't exist.");
        }
        try {
            curr = ((JSONObject) curr).get(field);
        } catch (JSONException e) {
            throw new InvalidPartitionKeyFieldsException("field " + field + " doesn't exist.");
        }
    }
    return curr;
}
Also used : JSONObject(org.json.JSONObject) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 2 with InvalidPartitionKeyFieldsException

use of org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException 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

JSONObject (org.json.JSONObject)2 InvalidPartitionKeyFieldsException (org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException)2 Math.abs (java.lang.Math.abs)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 JSONException (org.json.JSONException)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 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