Search in sources :

Example 6 with GenericRecord

use of org.apache.iceberg.data.GenericRecord in project hive by apache.

the class TestHiveIcebergTypes method testStructOfMapsInTable.

@Test
public void testStructOfMapsInTable() throws IOException {
    Schema schema = new Schema(required(1, "structofmaps", Types.StructType.of(required(2, "map1", Types.MapType.ofRequired(3, 4, Types.StringType.get(), Types.StringType.get())), required(5, "map2", Types.MapType.ofRequired(6, 7, Types.StringType.get(), Types.IntegerType.get())))));
    List<Record> records = testTables.createTableWithGeneratedRecords(shell, "structtable", schema, fileFormat, 1);
    // access a map entry inside a struct
    for (int i = 0; i < records.size(); i++) {
        GenericRecord expectedStruct = (GenericRecord) records.get(i).getField("structofmaps");
        Map<?, ?> expectedMap = (Map<?, ?>) expectedStruct.getField("map1");
        for (Map.Entry<?, ?> entry : expectedMap.entrySet()) {
            List<Object[]> queryResult = shell.executeStatement(String.format("SELECT structofmaps.map1[\"%s\"] from default.structtable LIMIT 1 OFFSET %d", entry.getKey(), i));
            Assert.assertEquals(entry.getValue(), queryResult.get(0)[0]);
        }
        expectedMap = (Map<?, ?>) expectedStruct.getField("map2");
        for (Map.Entry<?, ?> entry : expectedMap.entrySet()) {
            List<Object[]> queryResult = shell.executeStatement(String.format("SELECT structofmaps.map2[\"%s\"] from default.structtable LIMIT 1 OFFSET %d", entry.getKey(), i));
            Assert.assertEquals(entry.getValue(), queryResult.get(0)[0]);
        }
    }
}
Also used : Schema(org.apache.iceberg.Schema) Record(org.apache.iceberg.data.Record) GenericRecord(org.apache.iceberg.data.GenericRecord) GenericRecord(org.apache.iceberg.data.GenericRecord) Map(java.util.Map) Test(org.junit.Test)

Example 7 with GenericRecord

use of org.apache.iceberg.data.GenericRecord in project hive by apache.

the class TestHiveIcebergTypes method testStructOfStructsInTable.

@Test
public void testStructOfStructsInTable() throws IOException {
    Schema schema = new Schema(required(1, "structofstructs", Types.StructType.of(required(2, "struct1", Types.StructType.of(required(3, "key", Types.StringType.get()), required(4, "value", Types.IntegerType.get()))))));
    List<Record> records = testTables.createTableWithGeneratedRecords(shell, "structtable", schema, fileFormat, 1);
    // access a struct element inside a struct
    for (int i = 0; i < records.size(); i++) {
        GenericRecord expectedStruct = (GenericRecord) records.get(i).getField("structofstructs");
        GenericRecord expectedInnerStruct = (GenericRecord) expectedStruct.getField("struct1");
        List<Object[]> queryResult = shell.executeStatement(String.format("SELECT structofstructs.struct1.key, structofstructs.struct1.value FROM default.structtable " + "LIMIT 1 OFFSET %d", i));
        Assert.assertEquals(expectedInnerStruct.getField("key"), queryResult.get(0)[0]);
        Assert.assertEquals(expectedInnerStruct.getField("value"), queryResult.get(0)[1]);
    }
}
Also used : Schema(org.apache.iceberg.Schema) Record(org.apache.iceberg.data.Record) GenericRecord(org.apache.iceberg.data.GenericRecord) GenericRecord(org.apache.iceberg.data.GenericRecord) Test(org.junit.Test)

Aggregations

Schema (org.apache.iceberg.Schema)7 GenericRecord (org.apache.iceberg.data.GenericRecord)7 Record (org.apache.iceberg.data.Record)7 Test (org.junit.Test)7 List (java.util.List)3 Map (java.util.Map)3 ImmutableList (org.apache.iceberg.relocated.com.google.common.collect.ImmutableList)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Table (org.apache.iceberg.Table)1 TestHelper (org.apache.iceberg.mr.TestHelper)1 ImmutableMap (org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap)1 Type (org.apache.iceberg.types.Type)1 Types (org.apache.iceberg.types.Types)1 NestedField.required (org.apache.iceberg.types.Types.NestedField.required)1 Assert (org.junit.Assert)1