@Override public Tuple bytesToTuple(byte[] b, ResourceFieldSchema fieldSchema) throws IOException { if(b == null) return null; Tuple t; try { ByteArrayInputStream bis = new ByteArrayInputStream(b); PushbackInputStream in = new PushbackInputStream(bis); t = consumeTuple(in, fieldSchema); } catch (IOException e) { LogUtils.warn(this, "Unable to interpret value " + Arrays.toString(b) + " in field being " + "converted to type tuple, caught ParseException <" + e.getMessage() + "> field discarded", PigWarning.FIELD_DISCARDED_TYPE_CONVERSION_FAILED, mLog); return null; } return t; }
private Object consumeComplexType(PushbackInputStream in, ResourceFieldSchema complexFieldSchema) throws IOException { Object field; switch (complexFieldSchema.getType()) { case DataType.BAG: field = consumeBag(in, complexFieldSchema); break; case DataType.TUPLE: field = consumeTuple(in, complexFieldSchema); break; case DataType.MAP: field = consumeMap(in, complexFieldSchema); break; default: throw new IOException("Unknown complex data type"); } return field; }
private DataBag consumeBag(PushbackInputStream in, ResourceFieldSchema fieldSchema) throws IOException { if (fieldSchema==null) { throw new IOException("Schema is null"); } ResourceFieldSchema[] fss=fieldSchema.getSchema().getFields(); Tuple t; int buf; while ((buf=in.read())!='{') { if (buf==-1) { throw new IOException("Unexpect end of bag"); } } if (fss.length!=1) throw new IOException("Only tuple is allowed inside bag schema"); ResourceFieldSchema fs = fss[0]; DataBag db = DefaultBagFactory.getInstance().newDefaultBag(); while (true) { t = consumeTuple(in, fs); if (t!=null) db.add(t); while ((buf=in.read())!='}'&&buf!=',') { if (buf==-1) { throw new IOException("Unexpect end of bag"); } } if (buf=='}') break; } return db; }