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

org.apache.parquet.hadoop.metadata.ParquetMetadata Maven / Gradle / Ivy

There is a newer version: 1.14.4
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.parquet.hadoop.metadata;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.codehaus.jackson.map.SerializationConfig.Feature;

/**
 * Meta Data block stored in the footer of the file
 * contains file level (Codec, Schema, ...) and block level (location, columns, record count, ...) meta data
 *
 * @author Julien Le Dem
 *
 */
public class ParquetMetadata {

  private static final ObjectMapper objectMapper = new ObjectMapper();

  /**
   *
   * @param parquetMetaData
   * @return the json representation
   */
  public static String toJSON(ParquetMetadata parquetMetaData) {
    return toJSON(parquetMetaData, false);
  }

  /**
   *
   * @param parquetMetaData
   * @return the pretty printed json representation
   */
  public static String toPrettyJSON(ParquetMetadata parquetMetaData) {
    return toJSON(parquetMetaData, true);
  }

  private static String toJSON(ParquetMetadata parquetMetaData, boolean isPrettyPrint) {
    StringWriter stringWriter = new StringWriter();
    try {
      if (isPrettyPrint) {
        objectMapper.writerWithDefaultPrettyPrinter().writeValue(stringWriter, parquetMetaData);
      } else {
        objectMapper.writeValue(stringWriter, parquetMetaData);
      }
    } catch (JsonGenerationException e) {
      throw new RuntimeException(e);
    } catch (JsonMappingException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return stringWriter.toString();
  }

  /**
   *
   * @param json the json representation
   * @return the parsed object
   */
  public static ParquetMetadata fromJSON(String json) {
    try {
      return objectMapper.readValue(new StringReader(json), ParquetMetadata.class);
    } catch (JsonParseException e) {
      throw new RuntimeException(e);
    } catch (JsonMappingException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  private final FileMetaData fileMetaData;
  private final List blocks;

  /**
   *
   * @param fileMetaData file level metadata
   * @param blocks block level metadata
   */
  public ParquetMetadata(FileMetaData fileMetaData, List blocks) {
    this.fileMetaData = fileMetaData;
    this.blocks = blocks;
  }

  /**
   *
   * @return block level metadata
   */
  public List getBlocks() {
    return blocks;
  }

  /**
   *
   * @return file level meta data
   */
  public FileMetaData getFileMetaData() {
    return fileMetaData;
  }


  @Override
  public String toString() {
    return "ParquetMetaData{"+fileMetaData+", blocks: "+blocks+"}";
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy