Search in sources :

Example 6 with IntString

use of org.apache.hadoop.hive.serde2.proto.test.Complexpb.IntString in project hive by apache.

the class TestThriftObjectInspectors method testThriftSetObjectInspector.

@SuppressWarnings("unchecked")
public void testThriftSetObjectInspector() throws Throwable {
    try {
        ObjectInspector oi1 = ObjectInspectorFactory.getReflectionObjectInspector(SetIntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
        ObjectInspector oi2 = ObjectInspectorFactory.getReflectionObjectInspector(SetIntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
        assertEquals(oi1, oi2);
        // metadata
        assertEquals(Category.STRUCT, oi1.getCategory());
        StructObjectInspector soi = (StructObjectInspector) oi1;
        List<? extends StructField> fields = soi.getAllStructFieldRefs();
        assertEquals(2, fields.size());
        assertEquals(fields.get(0), soi.getStructFieldRef("sIntString"));
        assertEquals(fields.get(1), soi.getStructFieldRef("aString"));
        // null
        for (int i = 0; i < fields.size(); i++) {
            assertNull(soi.getStructFieldData(null, fields.get(i)));
        }
        // real object
        IntString s1 = new IntString();
        s1.setMyint(1);
        s1.setMyString("test");
        s1.setUnderscore_int(2);
        Set<IntString> set1 = new HashSet<IntString>();
        set1.add(s1);
        SetIntString s = new SetIntString();
        s.setSIntString(set1);
        s.setAString("setString");
        assertEquals(set1, soi.getStructFieldData(s, fields.get(0)));
        assertEquals("setString", soi.getStructFieldData(s, fields.get(1)));
        // sub fields
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getReflectionObjectInspector(IntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT)), fields.get(0).getFieldObjectInspector());
        assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector, fields.get(1).getFieldObjectInspector());
        // compare set fields
        ListObjectInspector loi = (ListObjectInspector) fields.get(0).getFieldObjectInspector();
        assertEquals(1, loi.getListLength(set1));
        List<IntString> list = (List<IntString>) loi.getList(set1);
        assertEquals(1, list.size());
        s1 = (IntString) loi.getListElement(list, 0);
        assertEquals(1, s1.getMyint());
        assertEquals("test", s1.getMyString());
        assertEquals(2, s1.getUnderscore_int());
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : SetIntString(org.apache.hadoop.hive.serde2.thrift.test.SetIntString) IntString(org.apache.hadoop.hive.serde2.thrift.test.IntString) SetIntString(org.apache.hadoop.hive.serde2.thrift.test.SetIntString) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 7 with IntString

use of org.apache.hadoop.hive.serde2.proto.test.Complexpb.IntString in project hive by apache.

the class TestHCatInputFormat method setUp.

/**
   * Create an input sequence file with 100 records; every 10th record is bad.
   * Load this table into Hive.
   */
@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    if (setUpComplete) {
        return;
    }
    Path intStringSeq = new Path(TEST_DATA_DIR + "/data/intString.seq");
    LOG.info("Creating data file: " + intStringSeq);
    SequenceFile.Writer seqFileWriter = SequenceFile.createWriter(intStringSeq.getFileSystem(hiveConf), hiveConf, intStringSeq, NullWritable.class, BytesWritable.class);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    TIOStreamTransport transport = new TIOStreamTransport(out);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    for (int i = 1; i <= 100; i++) {
        if (i % 10 == 0) {
            seqFileWriter.append(NullWritable.get(), new BytesWritable("bad record".getBytes()));
        } else {
            out.reset();
            IntString intString = new IntString(i, Integer.toString(i), i);
            intString.write(protocol);
            BytesWritable bytesWritable = new BytesWritable(out.toByteArray());
            seqFileWriter.append(NullWritable.get(), bytesWritable);
        }
    }
    seqFileWriter.close();
    // Now let's load this file into a new Hive table.
    Assert.assertEquals(0, driver.run("drop table if exists test_bad_records").getResponseCode());
    Assert.assertEquals(0, driver.run("create table test_bad_records " + "row format serde 'org.apache.hadoop.hive.serde2.thrift.ThriftDeserializer' " + "with serdeproperties ( " + "  'serialization.class'='org.apache.hadoop.hive.serde2.thrift.test.IntString', " + "  'serialization.format'='org.apache.thrift.protocol.TBinaryProtocol') " + "stored as" + "  inputformat 'org.apache.hadoop.mapred.SequenceFileInputFormat'" + "  outputformat 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'").getResponseCode());
    Assert.assertEquals(0, driver.run("load data local inpath '" + intStringSeq.getParent() + "' into table test_bad_records").getResponseCode());
    setUpComplete = true;
}
Also used : Path(org.apache.hadoop.fs.Path) IntString(org.apache.hadoop.hive.serde2.thrift.test.IntString) SequenceFile(org.apache.hadoop.io.SequenceFile) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) BytesWritable(org.apache.hadoop.io.BytesWritable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Before(org.junit.Before)

Aggregations

IntString (org.apache.hadoop.hive.serde2.thrift.test.IntString)6 ArrayList (java.util.ArrayList)5 Path (org.apache.hadoop.fs.Path)3 Complex (org.apache.hadoop.hive.serde2.thrift.test.Complex)3 BytesWritable (org.apache.hadoop.io.BytesWritable)3 SequenceFile (org.apache.hadoop.io.SequenceFile)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 SetIntString (org.apache.hadoop.hive.serde2.thrift.test.SetIntString)2 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)2 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)2 Before (org.junit.Before)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 Complex (org.apache.hadoop.hive.serde2.proto.test.Complexpb.Complex)1 IntString (org.apache.hadoop.hive.serde2.proto.test.Complexpb.IntString)1 PropValueUnion (org.apache.hadoop.hive.serde2.thrift.test.PropValueUnion)1 Writable (org.apache.hadoop.io.Writable)1