/** {@inheritDoc} */ @Override public DecodedCell<T> decodeCell(byte[] encodedBytes) throws IOException { return new DecodedCell(DecodedCell.NO_SCHEMA, encodedBytes); }
/** * Get the content of this cell. * * @return the content of this cell. */ public T getData() { return mDecodedCell.getData(); }
/** * Determines whether the data contained in this DecodedCell is equivalent to another. The * data is equivalent if they have the same schema and the same data, regardless of location. * * @param obj The object to compare. * @return Whether this contains the same data as the other DecodedCell. */ @Override public boolean equals(Object obj) { if (!(obj instanceof DecodedCell<?>)) { return false; } final DecodedCell<?> other = (DecodedCell<?>) obj; if (!Objects.equal(this.getWriterSchema(), other.getWriterSchema())) { return false; } if (!Objects.equal(this.getReaderSchema(), other.getReaderSchema())) { return false; } Object data = getData(); Object otherData = other.getData(); // UTF8 strings don't compare well with other CharSequences: if ((data instanceof Utf8) ^ (otherData instanceof Utf8)) { data = data.toString(); otherData = otherData.toString(); } return data.equals(otherData); }
/** {@inheritDoc} */ @Override public int hashCode() { return Objects.hashCode(getWriterSchema(), getData()); }
final DatumWriter<U> writer = new GenericDatumWriter<U>(original.getWriterSchema()); writer.write(original.getData(), encoder); encoder.flush(); new GenericDatumReader<T>(original.getWriterSchema(), schema); final T data = reader.read(null, decoder); return new DecodedCell<T>(schema, data);
/** {@inheritDoc} */ @Override public void produce(final FijiRowData row, final FijiTableContext context) throws IOException { final Iterable<FijiCell<Object>> cells; if (mColumn.isFullyQualified()) { cells = row.asIterable(mColumn.getFamily(), mColumn.getQualifier()); } else { cells = row.asIterable(mColumn.getFamily()); } for (FijiCell<Object> cell : cells) { context.incrementCounter(Counters.CELLS_PROCESSED); final DecodedCell<Object> original = new DecodedCell<Object>(cell.getWriterSchema(), cell.getData()); final DecodedCell<Object> rewritten = rewriteCell(original); if (rewritten != original) { context.put( row.getEntityId(), mColumn.getFamily(), mColumn.getQualifier(), cell.getTimestamp(), rewritten.getData()); context.incrementCounter(Counters.CELLS_REWRITTEN); } } }
/** * Get this cell's encoding type. * * @return this cell's encoding type. */ public CellType getType() { if (mDecodedCell.getWriterSchema() == null) { return CellType.COUNTER; } else { return CellType.AVRO; } }
/** * Get the Avro Schema used to decode this cell, or null for non-Avro values. * * @return the Avro Schema used to decode this cell, or null for non-Avro values. */ public Schema getReaderSchema() { return mDecodedCell.getReaderSchema(); }
/** {@inheritDoc} */ @Override public boolean equals(Object object) { if (object instanceof FijiCell) { final FijiCell<?> that = (FijiCell<?>) object; return this.mColumn.equals(that.mColumn) && (this.mTimestamp == that.mTimestamp) && this.mDecodedCell.equals(that.mDecodedCell); } else { return false; } }
/** {@inheritDoc} */ @Override protected JsonNode toJsonNode() { final ObjectNode root = JsonNodeFactory.instance.objectNode(); root.put(FAMILY_NODE, mFamily); root.put(QUALIFIER_NODE, mQualifier); final ObjectNode value = root.with(VALUE_NODE); // Schema's documentation for toString says it is rendered as JSON. value.put(SCHEMA_NODE, mValue.getWriterSchema().toString()); try { value.put(DATA_NODE, ToJson.toAvroJsonString(mValue.getData(), mValue.getWriterSchema())); } catch (IOException ioe) { throw new FijiIOException(ioe); } return root; }
/** * Get the Avro Schema used to encode this cell, or null for non-Avro values. * * @return the Avro Schema used to encode this cell, or null for non-Avro values. */ public Schema getWriterSchema() { return mDecodedCell.getWriterSchema(); }
/** {@inheritDoc} */ @Override public DecodedCell<T> decodeCell(byte[] encodedBytes) throws IOException { try { return new DecodedCell<T>( DecodedCell.NO_SCHEMA, (T) mParseFromMethod.invoke(mProtoClass, encodedBytes)); } catch (InvocationTargetException ite) { throw new IOException(ite); } catch (IllegalAccessException iae) { throw new IOException(iae); } }
/** {@inheritDoc} */ @Override public byte[] encode(final DecodedCell<?> cell) throws IOException { return encode(cell.getData()); }
/** * Rewrites a cell. * * <p> * This method is meant to be overloaded in case custom cell rewriting rules are necessary. * </p> * * @param cell Original value of the cell. * @return the new rewritten value of the cell, * or the original cell value if no translation rules apply. * @throws IOException on I/O error. * * @param <U> type of the input cell to rewrite. * @param <T> type to rewrite the input cell into. */ protected <T, U> DecodedCell<T> rewriteCell(final DecodedCell<U> cell) throws IOException { // Apply conversion rules as long as some rule matches: DecodedCell<U> rewritten = cell; while (true) { final Schema newSchema = mRules.get(rewritten.getWriterSchema()); if (newSchema == null) { // No rule apply, we are done. break; } else { rewritten = convertAvro(rewritten, newSchema); } } return (DecodedCell<T>) rewritten; }
/** {@inheritDoc} */ @Override public DecodedCell<Long> decodeCell(byte[] bytes) throws IOException { return new DecodedCell<Long>(DecodedCell.NO_SCHEMA, Bytes.toLong(bytes)); }
/** {@inheritDoc} */ @Override public byte[] encode(DecodedCell<?> cell) throws IOException { return encode(cell.getData()); }
switch(type) { case BOOLEAN: return new DecodedCell<Boolean>(type.getSchema(), Bytes.toBoolean(bytes)); case BYTES: return new DecodedCell<ByteBuffer>(type.getSchema(), ByteBuffer.wrap(bytes)); case DOUBLE: return new DecodedCell<Double>(type.getSchema(), Bytes.toDouble(bytes)); case FLOAT: return new DecodedCell<Float>(type.getSchema(), Bytes.toFloat(bytes)); case INT: return new DecodedCell<Integer>(type.getSchema(), Bytes.toInt(bytes)); case LONG: return new DecodedCell<Long>(type.getSchema(), Bytes.toLong(bytes)); case SHORT: return new DecodedCell<Integer>(type.getSchema(), (int) Bytes.toShort(bytes)); case STRING: return new DecodedCell<CharSequence>(type.getSchema(), Bytes.toString(bytes)); default: throw new IOException("Unsupported HBase encoding type: " + type.toString());
/** {@inheritDoc} */ @Override public byte[] encode(final DecodedCell<?> cell) throws IOException { return encode(cell.getData()); }
/** * Decodes the serialized bytes into a FijiCell. If reuse is non-null, the implementation may fill * it and return it as the FijiCell data payload. * * @param bytes The bytes from an HBase table cell. * @param reuse If non-null, may be filled with the decoded data and used as the data payload in * the return value. * @return The decoded FijiCell. * @throws IOException If there is an error. */ private DecodedCell<T> decode(byte[] bytes, T reuse) throws IOException { final ByteStreamArray byteStream = new ByteStreamArray(bytes); final Schema writerSchema = mSchemaDecoder.decode(byteStream); final Schema readerSchema = (mReaderSchema != null) ? mReaderSchema : writerSchema; final ByteBuffer binaryData = ByteBuffer.wrap(bytes, byteStream.getOffset(), bytes.length - byteStream.getOffset()); final T data = decodeAvro(binaryData, writerSchema, readerSchema, reuse); return new DecodedCell<T>(writerSchema, readerSchema, data); }
/** {@inheritDoc} */ @Override public T decodeValue(byte[] bytes) throws IOException { return decodeCell(bytes).getData(); }