All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.druid.data.input.impl.JSONParseSpec Maven / Gradle / Ivy

There is a newer version: 31.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.druid.data.input.impl;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.druid.java.util.common.parsers.JSONPathParser;
import org.apache.druid.java.util.common.parsers.JSONPathSpec;
import org.apache.druid.java.util.common.parsers.Parser;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 */
public class JSONParseSpec extends NestedDataParseSpec
{
  private final ObjectMapper objectMapper;
  private final Map featureSpec;
  private final boolean keepNullColumns;

  @JsonCreator
  public JSONParseSpec(
      @JsonProperty("timestampSpec") TimestampSpec timestampSpec,
      @JsonProperty("dimensionsSpec") DimensionsSpec dimensionsSpec,
      @JsonProperty("flattenSpec") JSONPathSpec flattenSpec,
      @JsonProperty("featureSpec") Map featureSpec,
      @JsonProperty("keepNullColumns") Boolean keepNullColumns
  )
  {
    super(timestampSpec, dimensionsSpec, flattenSpec != null ? flattenSpec : JSONPathSpec.DEFAULT);
    this.objectMapper = new ObjectMapper();
    this.keepNullColumns = (keepNullColumns == null) ? false : keepNullColumns;
    this.featureSpec = (featureSpec == null) ? new HashMap<>() : featureSpec;
    for (Map.Entry entry : this.featureSpec.entrySet()) {
      Feature feature = Feature.valueOf(entry.getKey());
      objectMapper.configure(feature, entry.getValue());
    }
  }

  public JSONParseSpec(TimestampSpec ts, DimensionsSpec dims)
  {
    this(ts, dims, null, null, false);
  }

  @Override
  public Parser makeParser()
  {
    return new JSONPathParser(getFlattenSpec(), objectMapper, getKeepNullColumns());
  }

  @Override
  public ParseSpec withTimestampSpec(TimestampSpec spec)
  {
    return new JSONParseSpec(spec, getDimensionsSpec(), getFlattenSpec(), getFeatureSpec(), getKeepNullColumns());
  }

  @Override
  public ParseSpec withDimensionsSpec(DimensionsSpec spec)
  {
    return new JSONParseSpec(getTimestampSpec(), spec, getFlattenSpec(), getFeatureSpec(), getKeepNullColumns());
  }

  @JsonProperty
  public Map getFeatureSpec()
  {
    return featureSpec;
  }

  @JsonProperty
  public boolean getKeepNullColumns()
  {
    return keepNullColumns;
  }

  @Override
  public boolean equals(final Object o)
  {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    if (!super.equals(o)) {
      return false;
    }
    final JSONParseSpec that = (JSONParseSpec) o;
    return Objects.equals(featureSpec, that.featureSpec) && Objects.equals(keepNullColumns, that.keepNullColumns);
  }

  @Override
  public int hashCode()
  {
    return Objects.hash(super.hashCode(), featureSpec, getKeepNullColumns());
  }

  @Override
  public String toString()
  {
    return "JSONParseSpec{" +
           "timestampSpec=" + getTimestampSpec() +
           ", dimensionsSpec=" + getDimensionsSpec() +
           ", flattenSpec=" + getFlattenSpec() +
           ", featureSpec=" + featureSpec +
           ", keepNullColumns=" + keepNullColumns +
           '}';
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy