def get_create_table(self,
                      table_name,
                      conn=None,
                      families={'info': dict()}):
     try:
         if conn is None:
             from happybase.connection import Connection
             conn = Connection(self.hbase_host)
         try:
             # what exception would be raised if table does not exist, actually none.
             # need to try to access families to get error
             table = conn.table(table_name)
             # this would fail if table does not exist
             _ = table.families()
             return table
         except Exception as inst:
             # TODO: act differently based on error type (connection issue or actually table missing)
             if type(inst) == TTransportException:
                 raise inst
             else:
                 print "[{}.get_create_table: info] table {} does not exist (yet): {}{}".format(
                     self.pp, table_name, type(inst), inst)
                 conn.create_table(table_name, families)
                 table = conn.table(table_name)
                 print "[{}.get_create_table: info] created table {}".format(
                     self.pp, table_name)
                 return table
     except Exception as inst:
         # May fail if families in dictionary do not match those of an existing table, or because of connection issues?
         # Should we raise it up?
         #pass
         raise inst
def get_create_table(table_name, options, families={'info': dict()}):
    try:
        from happybase.connection import Connection
        conn = Connection(options.hbase_ip)
        try:
            table = conn.table(table_name)
            # this would fail if table does not exist
            fam = table.families()
            return table
        # what exception would be raised if table does not exist, actually none.
        # need to try to access families to get error
        except Exception as inst:
            print "[get_create_table: info] table {} does not exist (yet)".format(table_name)
            conn.create_table(table_name, families)
            table = conn.table(table_name)
            print "[get_create_table: info] created table {}".format(table_name)
            return table
    except Exception as inst:
        print inst
Exemplo n.º 3
0
def get_create_table(table_name, options, families={'info': dict()}):
    try:
        from happybase.connection import Connection
        conn = Connection(options.hbase_ip)
        try:
            table = conn.table(table_name)
            # this would fail if table does not exist
            fam = table.families()
            return table
        # what exception would be raised if table does not exist, actually none.
        # need to try to access families to get error
        except Exception as inst:
            print "[get_create_table: info] table {} does not exist (yet)".format(table_name)
            conn.create_table(table_name, families)
            table = conn.table(table_name)
            print "[get_create_table: info] created table {}".format(table_name)
            return table
    except Exception as inst:
        print inst
Exemplo n.º 4
0
    def get_create_table(self, table_name, conn=None, families=None):
        """Get HBase table "table_name", creating it if it does not exist yet.

    :param table_name: name of the table to create.
    :type table_name: string
    :param conn: happybase connection
    :type conn: :class:`happybase.Connection`
    :param families: dictionary of column families (see ``get_dictcf_sha1_table`` and ``get_dictcf_update_table``)
    :type families: dict
    :return: table
    :rtype: :class:`happybase.Table`
    """
        # try:
        if conn is None:
            from happybase.connection import Connection
            conn = Connection(host=self.hbase_host, port=self.hbase_port)
        try:
            # as no exception would be raised if table does not exist...
            table = conn.table(table_name)
            # ...try to access families to get error if table does not exist yet
            _ = table.families()
            # table exist, return it
            return table
        except Exception as inst:
            # act differently based on error type (connection issue or actually table missing)
            if type(inst) == TTransportException:
                raise inst
            else:
                # we need to create the table
                msg = "[{}.get_create_table: info] table {} does not exist (yet): {}{}"
                print(msg.format(self.pp, table_name, type(inst), inst))
                # but we need to know which column families it should contain
                if families is None:
                    msg = "[{}.get_create_table: ERROR] table {} does not exist and 'families' not provided"
                    raise ValueError(msg.format(self.pp, table_name))
                # Create table...
                conn.create_table(table_name, families)
                table = conn.table(table_name)
                msg = "[{}.get_create_table: info] created table {}"
                print(msg.format(self.pp, table_name))
                # ... and return it
                return table