Search code examples
jrubyhbase

HBase Shell iterating over & truncating tables


Trying to truncate tables like this

list.each{|name| truncate(name) if name.end_with?('abc123')}

won't work. How would one approach this?


Solution

  • You can use Python's Happybase and the script below to do this (you need to enable a Thrift connection to HBase as well):

    import happybase
    
    # String that the table name ends with
    string = "abc123"
    
    # Connect to HBase
    c = happybase.Connection()
    
    # For each table
    for t in c.tables():
      # If the table ends with this string
      if t.endswith(string):
        # Disable and delete the table
        c.disable_table(t)
        c.delete_table(t)
        # Recreate it
        # Make sure to edit the below line with your own column-family structure
        c.create_table(t, {'cf':{}})
        # Print the name of the table
        print (t + " truncated")