2727import com .google .protobuf .ByteString ;
2828import java .util .concurrent .ExecutionException ;
2929import org .apache .avro .Schema ;
30+ import org .apache .avro .generic .GenericDatumReader ;
31+ import org .apache .avro .generic .GenericRecord ;
3032
3133public class BigTableSchemaRegistry {
3234 private final BigtableDataClient client ;
33- private final LoadingCache <SchemaReference , Schema > cache ;
35+ private final LoadingCache <SchemaReference , GenericDatumReader < GenericRecord > > cache ;
3436
3537 private static String COLUMN_FAMILY = "metadata" ;
3638 private static String QUALIFIER = "avro" ;
@@ -52,34 +54,54 @@ public String getTableName() {
5254 public ByteString getSchemaHash () {
5355 return schemaHash ;
5456 }
57+
58+ @ Override
59+ public int hashCode () {
60+ int result = tableName .hashCode ();
61+ result = 31 * result + schemaHash .hashCode ();
62+ return result ;
63+ }
64+
65+ @ Override
66+ public boolean equals (Object o ) {
67+ if (this == o ) return true ;
68+ if (o == null || getClass () != o .getClass ()) return false ;
69+
70+ SchemaReference that = (SchemaReference ) o ;
71+
72+ if (!tableName .equals (that .tableName )) return false ;
73+ return schemaHash .equals (that .schemaHash );
74+ }
5575 }
5676
5777 public BigTableSchemaRegistry (BigtableDataClient client ) {
5878 this .client = client ;
5979
60- CacheLoader <SchemaReference , Schema > schemaCacheLoader = CacheLoader .from (this ::loadSchema );
80+ CacheLoader <SchemaReference , GenericDatumReader <GenericRecord >> schemaCacheLoader =
81+ CacheLoader .from (this ::loadReader );
6182
6283 cache = CacheBuilder .newBuilder ().build (schemaCacheLoader );
6384 }
6485
65- public Schema getSchema (SchemaReference reference ) {
66- Schema schema ;
86+ public GenericDatumReader < GenericRecord > getReader (SchemaReference reference ) {
87+ GenericDatumReader < GenericRecord > reader ;
6788 try {
68- schema = this .cache .get (reference );
89+ reader = this .cache .get (reference );
6990 } catch (ExecutionException | CacheLoader .InvalidCacheLoadException e ) {
7091 throw new RuntimeException (String .format ("Unable to find Schema" ), e );
7192 }
72- return schema ;
93+ return reader ;
7394 }
7495
75- private Schema loadSchema (SchemaReference reference ) {
96+ private GenericDatumReader < GenericRecord > loadReader (SchemaReference reference ) {
7697 Row row =
7798 client .readRow (
7899 reference .getTableName (),
79100 ByteString .copyFrom (KEY_PREFIX .getBytes ()).concat (reference .getSchemaHash ()),
80101 Filters .FILTERS .family ().exactMatch (COLUMN_FAMILY ));
81102 RowCell last = Iterables .getLast (row .getCells (COLUMN_FAMILY , QUALIFIER ));
82103
83- return new Schema .Parser ().parse (last .getValue ().toStringUtf8 ());
104+ Schema schema = new Schema .Parser ().parse (last .getValue ().toStringUtf8 ());
105+ return new GenericDatumReader <>(schema );
84106 }
85107}
0 commit comments