python - 使用 HappyBase 更新 HBase 数据

标签 python hbase happybase

我正在尝试编写一个函数来更新保存在 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']?

最佳答案

想通了...

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

关于python - 使用 HappyBase 更新 HBase 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44975549/

相关文章:

python - lsof 有什么方法可以显示整个 argv 数组而不仅仅是 argv[0]

python - 如何立即运行生成器函数的初始化代码,而不是在第一次调用时运行?

python - 如何在 Linux 中跟踪所有后代进程

hbase - Spark序列化错误

hadoop - 将非 HA Hadoop 集群转换为 HA 集群

python - HBase 的 HappyBase 和原子批量插入

python - 如何使用单个 API 调用扫描 HappyBase 中的行集?

python - GttS 等待单词完整 Python

java - Phoenix 表选择联接返回尝试失败后失败= 11

python - 无法在 Python 中使用 Happybase 创建简单表