2017-07-07 51 views
3

更新HBase的數據我試圖寫一個函數從保存在HBase的表更新數據。我有一個將被調用,以更新其功能和我有一個很好的開始,但我是在完成它的最後一點點喪失。我可以到其他更新單行基於一個字符串,但比較日誌時候,我似乎無法弄清楚如何做到這一點,因爲沒有設置日誌時間。我將表中的所有值存儲到字典中。這裏是我的代碼:與HappyBase

def updateHBase(row): 

dict = row.asDict() #create a dictionary from Row 

columns = dict.keys() 
    for col in columns: #creates the colfamily:colname for happybase format 
     dict["DriveInfo:" +col] = dict.pop(col) #adds data into rows 

del dict[DriveInfo:serialnumb] #removes the row key that HBase uses, serialnum 

x = table.row(row.serialnum) 

if (len(x) == 0) #length of row is zero, row key does not exist 
    table.put(row.serialnum, dict) #add new row is row key does not exist 

else #check for health 
    if (dict['health'].lower() != 'healthy') #if the row isnt healthy... next steps 

     if (dict['health'].lower() != 'archived' and x['health'] == 'archived' and dict['logtime'] < x['logtime']) #update a row with a health status of archived 
      table.put(row.serialnum, dict) 

     else #if the row that is being replaced isn't archived, just replace the row 
      table.put(row.serialnum, dict) 
     return 

    elif (dict['logtime'] > x['logtime'] and dict['health'].lower() == 'healthy') # new log time > old log time and health is healthy, replace the row with new data 
     table.put(row.serialnum, dict) 

    else 
     return 

編輯:在我所有的if語句... dict['health'] ...是x['health']

回答

0

想通了......

def updateHBase(row): 

dict = row.asDict() #create a dictionary from Row 

columns = dict.keys() 
    for col in columns: #creates the colfamily:colname for happybase format 
     dict["DriveInfo:" +col] = dict.pop(col) #adds data into rows 

del dict[DriveInfo:serialnumb] #removes the row key that HBase uses, serialnum 

x = table.row(row.serialnum) 

if (len(x) == 0) #length of row is zero, row key does not exist 
    table.put(row.serialnum, dict) #add new row is row key does not exist 

else #check for health 
    if (x['health'].lower() != 'healthy') #if the row isnt healthy... next steps 

     if (x['health'].lower() != 'archived' and x['health'] == 'archived') #update a row with a health status of archived 
      table.put(row.serialnum, dict) 

     else #if the row that is being replaced isn't archived, just replace the row 
      table.put(row.serialnum, dict) 
     return 

    elif (x['logtime'] > row.logtime and x['health'].lower() == 'healthy') # new log time > old log time and health is healthy, replace the row with new data 
     table.put(row.serialnum, dict) 

    else 
     return