io.prestosql.plugin.hive.parquet.ParquetFileWriter Maven / Gradle / Ivy
/*
* 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.prestosql.plugin.hive.parquet;
import com.google.common.collect.ImmutableList;
import io.prestosql.parquet.writer.ParquetWriter;
import io.prestosql.parquet.writer.ParquetWriterOptions;
import io.prestosql.plugin.hive.FileWriter;
import io.prestosql.spi.Page;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.Block;
import io.prestosql.spi.block.BlockBuilder;
import io.prestosql.spi.block.RunLengthEncodedBlock;
import io.prestosql.spi.type.Type;
import org.apache.parquet.hadoop.metadata.CompressionCodecName;
import org.apache.parquet.schema.MessageType;
import org.openjdk.jol.info.ClassLayout;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import static com.google.common.base.MoreObjects.toStringHelper;
import static io.prestosql.plugin.hive.HiveErrorCode.HIVE_WRITER_CLOSE_ERROR;
import static io.prestosql.plugin.hive.HiveErrorCode.HIVE_WRITER_DATA_ERROR;
import static java.util.Objects.requireNonNull;
public class ParquetFileWriter
implements FileWriter
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(ParquetFileWriter.class).instanceSize();
private final ParquetWriter parquetWriter;
private final Callable rollbackAction;
private final int[] fileInputColumnIndexes;
private final List nullBlocks;
public ParquetFileWriter(
OutputStream outputStream,
Callable rollbackAction,
List fileColumnTypes,
MessageType messageType,
Map, Type> primitiveTypes,
ParquetWriterOptions parquetWriterOptions,
int[] fileInputColumnIndexes,
CompressionCodecName compressionCodecName)
{
requireNonNull(outputStream, "outputStream is null");
this.parquetWriter = new ParquetWriter(
outputStream,
messageType,
primitiveTypes,
parquetWriterOptions,
compressionCodecName);
this.rollbackAction = requireNonNull(rollbackAction, "rollbackAction is null");
this.fileInputColumnIndexes = requireNonNull(fileInputColumnIndexes, "fileInputColumnIndexes is null");
ImmutableList.Builder nullBlocks = ImmutableList.builder();
for (Type fileColumnType : fileColumnTypes) {
BlockBuilder blockBuilder = fileColumnType.createBlockBuilder(null, 1, 0);
blockBuilder.appendNull();
nullBlocks.add(blockBuilder.build());
}
this.nullBlocks = nullBlocks.build();
}
@Override
public long getWrittenBytes()
{
return parquetWriter.getWrittenBytes();
}
@Override
public long getSystemMemoryUsage()
{
return INSTANCE_SIZE + parquetWriter.getRetainedBytes();
}
@Override
public void appendRows(Page dataPage)
{
Block[] blocks = new Block[fileInputColumnIndexes.length];
for (int i = 0; i < fileInputColumnIndexes.length; i++) {
int inputColumnIndex = fileInputColumnIndexes[i];
if (inputColumnIndex < 0) {
blocks[i] = new RunLengthEncodedBlock(nullBlocks.get(i), dataPage.getPositionCount());
}
else {
blocks[i] = dataPage.getBlock(inputColumnIndex);
}
}
Page page = new Page(dataPage.getPositionCount(), blocks);
try {
parquetWriter.write(page);
}
catch (IOException | UncheckedIOException e) {
throw new PrestoException(HIVE_WRITER_DATA_ERROR, e);
}
}
@Override
public void commit()
{
try {
parquetWriter.close();
}
catch (IOException | UncheckedIOException e) {
try {
rollbackAction.call();
}
catch (Exception ignored) {
// ignore
}
throw new PrestoException(HIVE_WRITER_CLOSE_ERROR, "Error committing write parquet to Hive", e);
}
}
@Override
public void rollback()
{
try {
try {
parquetWriter.close();
}
finally {
rollbackAction.call();
}
}
catch (Exception e) {
throw new PrestoException(HIVE_WRITER_CLOSE_ERROR, "Error rolling back write parquet to Hive", e);
}
}
@Override
public long getValidationCpuNanos()
{
return 0;
}
@Override
public String toString()
{
return toStringHelper(this)
.add("writer", parquetWriter)
.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy