// " where key > 10 group by key");
String sql = "select key, value from " + tableName + " where key > 10";
client.execute(sql);
// Instantiate DynamicSerDe
DynamicSerDe ds = new DynamicSerDe();
Properties dsp = new Properties();
dsp.setProperty(Constants.SERIALIZATION_FORMAT,
org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol.class
.getName());
dsp.setProperty(
org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_NAME,
"result");
String serDDL = new String("struct result { ");
List<FieldSchema> schema = client.getThriftSchema().getFieldSchemas();
for (int pos = 0; pos < schema.size(); pos++) {
if (pos != 0) {
serDDL = serDDL.concat(",");
}
serDDL = serDDL.concat(schema.get(pos).getType());
serDDL = serDDL.concat(" ");
serDDL = serDDL.concat(schema.get(pos).getName());
}
serDDL = serDDL.concat("}");
dsp.setProperty(Constants.SERIALIZATION_DDL, serDDL);
dsp.setProperty(Constants.SERIALIZATION_LIB, ds.getClass().toString());
dsp.setProperty(Constants.FIELD_DELIM, "9");
ds.initialize(new Configuration(), dsp);
String row = client.fetchOne();
Object o = ds.deserialize(new BytesWritable(row.getBytes()));
assertEquals(o.getClass().toString(), "class java.util.ArrayList");
List<?> lst = (List<?>) o;
assertEquals(lst.get(0), 238);
// TODO: serde doesn't like underscore -- struct result { string _c0}
sql = "select count(1) as c from " + tableName;
client.execute(sql);
row = client.fetchOne();
serDDL = new String("struct result { ");
schema = client.getThriftSchema().getFieldSchemas();
for (int pos = 0; pos < schema.size(); pos++) {
if (pos != 0) {
serDDL = serDDL.concat(",");
}
serDDL = serDDL.concat(schema.get(pos).getType());
serDDL = serDDL.concat(" ");
serDDL = serDDL.concat(schema.get(pos).getName());
}
serDDL = serDDL.concat("}");
dsp.setProperty(Constants.SERIALIZATION_DDL, serDDL);
// Need a new DynamicSerDe instance - re-initialization is not supported.
ds = new DynamicSerDe();
ds.initialize(new Configuration(), dsp);
o = ds.deserialize(new BytesWritable(row.getBytes()));
}