Koalas Dataframe Updating live as Deltalake is updated

Viewed 87

I'm working on a solution that updates a delta lake though a given index using the following code:

      dataframe = ks.read_table('data')
      subdataframe = dataframe .loc[dataframe ['status']== 1,:]


      for index,column in subdataframe.iterrows():
        
        #get values for a given row
        record= subdataframe.loc[index].to_dict()

        if(record_needs_updating):
          #update deltalake
          dataframe.loc[dataframe['file']==record['file'],'status'] = 0
          dataframe.to_delta('fileloc', partition_cols='pull',mode='overwrite')

          #update databricks table
          spark.sql("DROP TABLE  IF EXISTS data")
          spark.sql("CREATE TABLE data USING DELTA LOCATION fileloc)
          spark.sql("OPTIMIZE data")

the problem that i am running into is a key error when trying to index into the subdataframe in the for loop.

this seems to happen because the dataframe itself gets updated to not include any records with status = 0 after the delta lake is updated, meaning the index changes, thus giving a key error.

is there any way to make the subdataframe into a non live dataframe that wont be updated as the deltalake is updated?

also to note i need the update to be live as the code is running and not update just once after all the code is run.

Thanks!

0 Answers
Related