简体   繁体   English

使用HappyBase更新HBase数据

[英]Updating HBase data with HappyBase

I am trying to write a function to update Data from a table saved in HBase. 我正在尝试编写一个函数来更新保存在HBase中的表中的数据。 I have a function that will get called to Update it and I have a pretty good start but I am a little bit lost on the end of finishing it. 我有一个函数将被调用来更新它,我有一个非常好的开始,但我有点迷失在完成它的结束。 I can update single rows based one string to another, but when comparing log times, I can not seem to figure out how to do that, since there are no set log times. 我可以根据一个字符串更新单行到另一个字符串,但是在比较日志时间时,我似乎无法弄清楚如何做到这一点,因为没有设置日志时间。 I am storing all of values from the table into a dictionary. 我将表中的所有值存储到字典中。 Here is my code: 这是我的代码:

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

EDIT: In all of my if statements should ... dict['health'] ... be x['health'] ? 编辑:在我的所有if语句中应该... dict['health'] ...是x['health']

Figured it out... 弄清楚了...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM