Java tutorial
/* * Druid - a distributed column store. * Copyright 2012 - 2015 Metamarkets Group Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.druid.query.extraction; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.metamx.common.StringUtils; import java.nio.ByteBuffer; import java.util.regex.Matcher; import java.util.regex.Pattern; /** */ public class RegexDimExtractionFn extends DimExtractionFn { private static final byte CACHE_TYPE_ID = 0x1; private final String expr; private final Pattern pattern; @JsonCreator public RegexDimExtractionFn(@JsonProperty("expr") String expr) { Preconditions.checkNotNull(expr, "expr must not be null"); this.expr = expr; this.pattern = Pattern.compile(expr); } @Override public byte[] getCacheKey() { byte[] exprBytes = StringUtils.toUtf8(expr); return ByteBuffer.allocate(1 + exprBytes.length).put(CACHE_TYPE_ID).put(exprBytes).array(); } @Override public String apply(String dimValue) { if (dimValue == null) { return null; } Matcher matcher = pattern.matcher(dimValue); return Strings.emptyToNull(matcher.find() ? matcher.group(1) : dimValue); } @JsonProperty("expr") public String getExpr() { return expr; } @Override public boolean preservesOrdering() { return false; } @Override public ExtractionType getExtractionType() { return ExtractionType.MANY_TO_ONE; } @Override public String toString() { return String.format("regex(%s)", expr); } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } RegexDimExtractionFn that = (RegexDimExtractionFn) o; if (!expr.equals(that.expr)) { return false; } return true; } @Override public int hashCode() { return expr.hashCode(); } }