用HappyBas更新HBase数据

2024-06-18 13:03:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图编写一个函数来更新HBase中保存的表中的数据。我有一个函数,将被调用来更新它,我有一个很好的开始,但我有一点迷失在结束它。我可以将基于一个字符串的单个行更新到另一个字符串,但是在比较日志时间时,我似乎不知道如何做到这一点,因为没有设置日志时间。我正在将表中的所有值存储到字典中。这是我的代码:

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']?在


Tags: andtheforifputistablecol
1条回答
网友
1楼 · 发布于 2024-06-18 13:03:18

明白了。。。在

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

相关问题 更多 >