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

io.druid.data.input.parquet.GenericRecordAsMap Maven / Gradle / Ivy

There is a newer version: 0.12.3
Show newest version
/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets 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 io.druid.data.input.parquet;

import com.google.common.base.Preconditions;
import io.druid.java.util.common.StringUtils;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.util.Utf8;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

public class GenericRecordAsMap implements Map
{
  private final GenericRecord record;
  private final boolean binaryAsString;

  public GenericRecordAsMap(
      GenericRecord record,
      boolean binaryAsString
  )
  {
    this.record = Preconditions.checkNotNull(record, "record");
    this.binaryAsString = binaryAsString;
  }

  @Override
  public int size()
  {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean isEmpty()
  {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean containsKey(Object key)
  {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean containsValue(Object value)
  {
    throw new UnsupportedOperationException();
  }

  /**
   * When used in MapBasedRow, field in GenericRecord will be interpret as follows:
   * 
    *
  • avro schema type -> druid dimension:
  • *
      *
    • null, boolean, int, long, float, double, string, Records, Enums, Maps, Fixed -> String, using String.valueOf
    • *
    • bytes -> Arrays.toString() or new String if binaryAsString is true
    • *
    • Arrays -> List<String>, using Lists.transform(<List>dimValue, TO_STRING_INCLUDING_NULL)
    • *
    *
  • avro schema type -> druid metric:
  • *
      *
    • null -> 0F/0L
    • *
    • int, long, float, double -> Float/Long, using Number.floatValue()/Number.longValue()
    • *
    • string -> Float/Long, using Float.valueOf()/Long.valueOf()
    • *
    • boolean, bytes, Arrays, Records, Enums, Maps, Fixed -> ParseException
    • *
    *
*/ @Override public Object get(Object key) { Object field = record.get(key.toString()); if (field instanceof ByteBuffer) { if (binaryAsString) { return StringUtils.fromUtf8(((ByteBuffer) field).array()); } else { return Arrays.toString(((ByteBuffer) field).array()); } } if (field instanceof Utf8) { return field.toString(); } return field; } @Override public Object put(String key, Object value) { throw new UnsupportedOperationException(); } @Override public Object remove(Object key) { throw new UnsupportedOperationException(); } @Override public void putAll(Map m) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public Set keySet() { throw new UnsupportedOperationException(); } @Override public Collection values() { throw new UnsupportedOperationException(); } @Override public Set> entrySet() { throw new UnsupportedOperationException(); } }