@@ -191,21 +191,7 @@ def online_write_batch(
191191 table_instance = dynamodb_resource .Table (
192192 _get_table_name (online_config , config , table )
193193 )
194- with table_instance .batch_writer () as batch :
195- for entity_key , features , timestamp , created_ts in data :
196- entity_id = compute_entity_id (entity_key )
197- batch .put_item (
198- Item = {
199- "entity_id" : entity_id , # PartitionKey
200- "event_ts" : str (utils .make_tzaware (timestamp )),
201- "values" : {
202- k : v .SerializeToString ()
203- for k , v in features .items () # Serialized Features
204- },
205- }
206- )
207- if progress :
208- progress (1 )
194+ self ._write_batch_non_duplicates (table_instance , data , progress )
209195
210196 @log_exceptions_and_usage (online_store = "dynamodb" )
211197 def online_read (
@@ -299,6 +285,32 @@ def _sort_dynamodb_response(self, responses: list, order: list):
299285 _ , table_responses_ordered = zip (* table_responses_ordered )
300286 return table_responses_ordered
301287
288+ @log_exceptions_and_usage (online_store = "dynamodb" )
289+ def _write_batch_non_duplicates (
290+ self ,
291+ table_instance ,
292+ data : List [
293+ Tuple [EntityKeyProto , Dict [str , ValueProto ], datetime , Optional [datetime ]]
294+ ],
295+ progress : Optional [Callable [[int ], Any ]],
296+ ):
297+ """Deduplicate write batch request items on ``entity_id`` primary key."""
298+ with table_instance .batch_writer (overwrite_by_pkeys = ["entity_id" ]) as batch :
299+ for entity_key , features , timestamp , created_ts in data :
300+ entity_id = compute_entity_id (entity_key )
301+ batch .put_item (
302+ Item = {
303+ "entity_id" : entity_id , # PartitionKey
304+ "event_ts" : str (utils .make_tzaware (timestamp )),
305+ "values" : {
306+ k : v .SerializeToString ()
307+ for k , v in features .items () # Serialized Features
308+ },
309+ }
310+ )
311+ if progress :
312+ progress (1 )
313+
302314
303315def _initialize_dynamodb_client (region : str , endpoint_url : Optional [str ] = None ):
304316 return boto3 .client ("dynamodb" , region_name = region , endpoint_url = endpoint_url )
0 commit comments