use of org.apache.hadoop.hive.serde2.thrift.test.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;
}
}
use of org.apache.hadoop.hive.serde2.thrift.test.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;
}
Aggregations