Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public Optional<Converter> visit(LogicalTypeAnnotation.DecimalLogicalTypeAnnotat
}
}).orElse(new SimplePrimitiveConverter(field.getName()));
}
return new SimplePrimitiveConverter(field.getName());
}

GroupType groupType = field.asGroupType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.tools.read;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.parquet.column.ParquetProperties;
import org.apache.parquet.example.data.Group;
import org.apache.parquet.example.data.simple.SimpleGroupFactory;
import org.apache.parquet.hadoop.ParquetReader;
import org.apache.parquet.hadoop.ParquetWriter;
import org.apache.parquet.hadoop.example.GroupWriteSupport;
import org.apache.parquet.hadoop.metadata.CompressionCodecName;
import org.apache.parquet.io.api.Binary;
import org.apache.parquet.schema.MessageType;
import org.apache.parquet.schema.PrimitiveType;
import org.apache.parquet.schema.Type;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;


public class TestSimpleRecordConverter {

private static final String INT32_FIELD = "int32_field";
private static final String INT64_FIELD = "int64_field";
private static final String FLOAT_FIELD = "float_field";
private static final String DOUBLE_FIELD = "double_field";
private static final String BINARY_FIELD = "binary_field";
private static final String FIXED_LEN_BYTE_ARRAY_FIELD = "flba_field";


private File testFile;

@Test
public void testConverter() throws IOException {
ParquetReader<SimpleRecord> reader =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reader is not being closed, I would use a try-with-resources here.

ParquetReader.builder(new SimpleReadSupport(), new Path(this.testFile.getAbsolutePath())).build();
for (SimpleRecord record = reader.read(); record != null; record = reader.read()) {
for (SimpleRecord.NameValue value : record.getValues()) {
switch(value.getName()) {
case INT32_FIELD:
Assert.assertEquals(32, value.getValue());
break;
case INT64_FIELD:
Assert.assertEquals(64L, value.getValue());
break;
case FLOAT_FIELD:
Assert.assertEquals(1.0f, value.getValue());
break;
case DOUBLE_FIELD:
Assert.assertEquals(2.0d, value.getValue());
break;
case BINARY_FIELD:
Assert.assertArrayEquals("foobar".getBytes(), (byte[])value.getValue());
break;
case FIXED_LEN_BYTE_ARRAY_FIELD:
Assert.assertArrayEquals(new byte[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, (byte[])value.getValue());
break;
}
}
}
}

@Before
public void setUp() throws IOException {
this.testFile = createTempFile();
MessageType schema = createSchema();
write(schema, testFile);
}

@After
public void tearDown() {
this.testFile.delete();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using the junit TemporaryFolder we don't need this anymore.

}

private MessageType createSchema() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make this static.

return new MessageType("schema",
new PrimitiveType(Type.Repetition.REQUIRED, PrimitiveType.PrimitiveTypeName.INT32, INT32_FIELD),
new PrimitiveType(Type.Repetition.REQUIRED, PrimitiveType.PrimitiveTypeName.INT64, INT64_FIELD),
new PrimitiveType(Type.Repetition.REQUIRED, PrimitiveType.PrimitiveTypeName.FLOAT, FLOAT_FIELD),
new PrimitiveType(Type.Repetition.REQUIRED, PrimitiveType.PrimitiveTypeName.DOUBLE, DOUBLE_FIELD),
new PrimitiveType(Type.Repetition.REQUIRED, PrimitiveType.PrimitiveTypeName.BINARY, BINARY_FIELD),
new PrimitiveType(Type.Repetition.REQUIRED,
PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY, 12, FIXED_LEN_BYTE_ARRAY_FIELD)
);
}

private void write(MessageType schema, File f) throws IOException {
Path fsPpath = new Path(f.getPath());
Configuration conf = new Configuration();
SimpleGroupFactory fact = new SimpleGroupFactory(schema);
GroupWriteSupport.setSchema(schema, conf);

ParquetWriter<Group> writer = new ParquetWriter<Group>(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would wrap an try-with-resource around the writer.

fsPpath,
new GroupWriteSupport(),
CompressionCodecName.UNCOMPRESSED,
1024,
1024,
512,
true,
false,
ParquetProperties.WriterVersion.PARQUET_2_0,
conf);
try {
writer.write(fact.newGroup()
.append(INT32_FIELD, 32)
.append(INT64_FIELD, 64L)
.append(FLOAT_FIELD, 1.0f)
.append(DOUBLE_FIELD, 2.0d)
.append(BINARY_FIELD, Binary.fromString("foobar"))
.append(FIXED_LEN_BYTE_ARRAY_FIELD,
Binary.fromConstantByteArray(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })));
} finally {
writer.close();
}
}

private File createTempFile() throws IOException {
File tmp = File.createTempFile(getClass().getSimpleName(), ".tmp");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@masayuki038 masayuki038 Feb 11, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fokko I did not know about TemporaryFolder. Thanks !!

tmp.deleteOnExit();
tmp.delete();
return tmp;
}
}