/** * Returns a reader specification for a column encoded using Avro, * and decoded with the generic API using a given Avro schema. * * @param readerSchema Avro reader schema used to decode cells. * @return a reader specification for a column encoded using Avro, * and decoded with the generic API using the specified Avro schema. */ public static ColumnReaderSpec avroReaderSchemaGeneric( final Schema readerSchema ) { return new ColumnReaderSpec( Encoding.AVRO, AvroReaderSchemaType.EXPLICIT, AvroDecoderType.GENERIC, /* avroReaderSchema = */ readerSchema, /* avroReaderSchemaClass = */ null ); }
/** * Initializes a new RawBytesCellDecoder. * * @param spec Specification of the cell encoding. */ public RawBytesCellDecoder(BoundColumnReaderSpec spec) { Preconditions.checkNotNull(spec); Preconditions.checkArgument( spec.getColumnReaderSpec().getEncoding() == Encoding.RAW_BYTES); }
/** * Get the Avro Schema to use to decode cells. * * @param layout FijiTableLayout from which to get storage information. * @param spec Specification of the cell encoding. * @return the Avro Schema to use to decode cells. * @throws IOException in case the specified column does not exist. */ private static Schema getReaderSchema(FijiTableLayout layout, BoundColumnReaderSpec spec) throws IOException { switch (spec.getColumnReaderSpec().getAvroReaderSchemaType()) { case DEFAULT: { final CellSchema cellSchema = layout.getCellSchema(spec.getColumn()); final SchemaTableAvroResolver resolver = new SchemaTableAvroResolver(layout.getSchemaTable()); return resolver.apply(cellSchema.getDefaultReader()); } case EXPLICIT: return spec.getColumnReaderSpec().getAvroReaderSchema(); case WRITER: return null; default: throw new InternalFijiError( "Unknown AvroReaderSchemaType: " + spec.getColumnReaderSpec().getAvroReaderSchemaType()); } }
/** {@inheritDoc} */ @Override public FijiDataRequest getDataRequest() { final CellRewriteSpec spec; try { spec = getSpecFromConf(getConf()); } catch (IOException ioe) { throw new FijiIOException(ioe); } final FijiColumnName column = new FijiColumnName(spec.getColumn()); final ColumnReaderSpec readerSpec; if (spec.getReaderSchema() == null) { readerSpec = ColumnReaderSpec.avroWriterSchemaGeneric(); } else { final Schema readerSchema = new Schema.Parser().parse(spec.getReaderSchema()); readerSpec = ColumnReaderSpec.avroReaderSchemaGeneric(readerSchema); } return FijiDataRequest.builder() .addColumns(ColumnsDef.create() .withMaxVersions(HConstants.ALL_VERSIONS) .add(column, readerSpec)) .build(); }
/** * Create a new {@link FijiCellDecoder} from a {@link BoundColumnReaderSpec}. * * @param layout {@code FijiTableLayout} from which storage information will be retrieved to build * decoders. * @param spec specification of column read properties from which to build a cell decoder. * @return a new cell decoder based on the specification. * @throws IOException in case of an error making the decoder. */ private static FijiCellDecoder<?> createDecoderFromSpec( final FijiTableLayout layout, final BoundColumnReaderSpec spec ) throws IOException { final AvroDecoderType decoderType = spec.getColumnReaderSpec().getAvroDecoderType(); if (null != decoderType) { switch (decoderType) { case GENERIC: { return GenericCellDecoderFactory.get().create(layout, spec); } case SPECIFIC: { return SpecificCellDecoderFactory.get().create(layout, spec); } default: throw new InternalFijiError("Unknown decoder type: " + decoderType); } } else { // If the decoder type is null, we can use the generic factory. return GenericCellDecoderFactory.get().create(layout, spec); } }
switch (spec.getColumnReaderSpec().getAvroReaderSchemaType()) { case DEFAULT: case WRITER: { return new FinalSchemaDecoder(spec.getColumnReaderSpec().getAvroReaderSchema());
/** * Initializes a ProtobufCellDecoder. * * @param layout FijiTableLayout from which to get the Protobuf class name. * @param spec Specification of the cell encoding. * @throws IOException on I/O error. */ public ProtobufCellDecoder(FijiTableLayout layout, BoundColumnReaderSpec spec) throws IOException { Preconditions.checkNotNull(layout); Preconditions.checkNotNull(spec); Preconditions.checkArgument( spec.getColumnReaderSpec().getEncoding() == Encoding.PROTOBUF); final String className = layout.getCellSchema(spec.getColumn()).getProtobufClassName(); try { mProtoClass = Class.forName(className).asSubclass(AbstractMessageLite.class); } catch (ClassNotFoundException cnfe) { throw new IOException(cnfe); } try { mParseFromMethod = mProtoClass.getMethod("parseFrom", byte[].class); } catch (NoSuchMethodException nsme) { throw new IOException(nsme); } }
/** * Returns a reader specification for a column encoded using Avro, * and decoded with the generic API using the writer schema. * * @return a reader specification for a column encoded using Avro, * and decoded with the generic API using the writer schema. */ public static ColumnReaderSpec avroWriterSchemaGeneric() { return new ColumnReaderSpec( Encoding.AVRO, AvroReaderSchemaType.WRITER, AvroDecoderType.GENERIC, /* avroReaderSchema = */ null, /* avroReaderSchemaClass = */ null ); }
/** {@inheritDoc} */ @Override public <T> FijiCellDecoder<T> create(FijiTableLayout layout, BoundColumnReaderSpec spec) throws IOException { Preconditions.checkNotNull(layout); Preconditions.checkNotNull(spec); switch (spec.getColumnReaderSpec().getEncoding()) { case RAW_BYTES: return new RawBytesCellDecoder<T>(spec); case AVRO: return new GenericCellDecoder<T>(layout, spec); case PROTOBUF: return new ProtobufCellDecoder<T>(layout, spec); case COUNTER: // purposefully forget the type (long) param of cell decoders for counters. @SuppressWarnings("unchecked") final FijiCellDecoder<T> counterCellDecoder = (FijiCellDecoder<T>) CounterCellDecoder.get(); return counterCellDecoder; default: throw new InternalFijiError( "Unhandled cell encoding in reader spec: " + spec.getColumnReaderSpec()); } } }
/** * Returns a reader specification for a column encoded using Avro, and decoded with the generic * API using the default Avro reader schema specified in the table layout. * * @return a reader specification for a column encoded using Avro, and decoded with the generic * API using the default Avro reader schema specified in the table layout. */ public static ColumnReaderSpec avroDefaultReaderSchemaGeneric() { return new ColumnReaderSpec( Encoding.AVRO, AvroReaderSchemaType.DEFAULT, AvroDecoderType.GENERIC, /* avroReaderSchema = */ null, /* avroReaderSchemaClass = */ null ); }
/** {@inheritDoc} */ @Override public <T> FijiCellDecoder<T> create(FijiTableLayout layout, BoundColumnReaderSpec spec) throws IOException { Preconditions.checkNotNull(spec); switch (spec.getColumnReaderSpec().getEncoding()) { case RAW_BYTES: return new RawBytesCellDecoder<T>(spec); case AVRO: return new SpecificCellDecoder<T>(layout, spec); case PROTOBUF: return new ProtobufCellDecoder<T>(layout, spec); case COUNTER: // purposefully forget the type (long) param of cell decoders for counters. @SuppressWarnings("unchecked") final FijiCellDecoder<T> counterCellDecoder = (FijiCellDecoder<T>) CounterCellDecoder.get(); return counterCellDecoder; default: throw new InternalFijiError( "Unhandled cell encoding in reader spec: " + spec.getColumnReaderSpec()); } }
/** * Returns a reader specification for a column encoded using Avro, * and decoded with the specific API using a given SpecificRecord generated class. * * @param readerSchemaClass Class of the specific record to decode. * @return a reader specification for a column encoded using Avro, * and decoded with the specific API using the given SpecificRecord generated class. * @throws InvalidLayoutException if the specified class is not a valid Avro container class. */ public static ColumnReaderSpec avroReaderSchemaSpecific( final Class<? extends SpecificRecord> readerSchemaClass ) throws InvalidLayoutException { final Schema readerSchema = CellSpec.getSchemaFromClass(readerSchemaClass); return new ColumnReaderSpec( Encoding.AVRO, AvroReaderSchemaType.EXPLICIT, AvroDecoderType.SPECIFIC, /* avroReaderSchema = */ readerSchema, /* avroReaderSchemaClass = */ readerSchemaClass ); }