首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HappyBase更新HBase数据

使用HappyBase更新HBase数据
EN

Stack Overflow用户
提问于 2017-07-08 00:19:42
回答 1查看 808关注 0票数 4

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

代码语言:javascript
复制
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'] ...be x['health']

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-08 03:34:22

弄清楚了..。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44975549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档