HBase NoSQL database

Apache HBase is the Hadoop database, a distributed, scalable, big data store. HBase is a NoSQL database which could be installed in two modes: standalone or distributed (pseudo-distributed or fully-distributed). Here is an example of basic operations for the latest HBase release distributed in a Hadoop cluster.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
 * HBase test. Hadoop cluster has been installed on (K)Ubuntu 14.04
 * The following products are used: 
 * 
 * Hadoop     2.7.0
 * HBase    1.0.1.1
 * Thrift   1.0.1.1
 * 
 * @author Marius
 *
 */
public class HBase {
	
    /* HBase connection */
    private Connection connection;
    
    /* HBase Admin object. */
    private Admin admin;
    
    /* HBase table name */
    private final TableName tableName = TableName.valueOf("people");
    
    /**
     * Create a HBase connection.
     * 
     * @throws  IOException
     *          On I/O error.
     */
    public HBase() throws IOException {
        Configuration config = HBaseConfiguration.create();
        config.setInt("timeout", 120000);
        connection = ConnectionFactory.createConnection(config);
    }
    
    /**
     * Convert string to bytes.
     * 
     * @param 	text
     * 			Input string.
     * 
     * @return	Input string bytes.
     */
    private byte[] b(String text) {
    	return Bytes.toBytes(text);
    }

    /**
     * Create an HBase table.
     * 
     * @throws  IOException
     *          On I/O error.
     */
    public void createTable() throws IOException {
        admin = connection.getAdmin();
        HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
        tableDescriptor.addFamily(new HColumnDescriptor("personal"));
        tableDescriptor.addFamily(new HColumnDescriptor("contactinfo"));
        tableDescriptor.addFamily(new HColumnDescriptor("creditcard"));
        admin.createTable(tableDescriptor);
        admin.close();
    }

    /**
     * Delete HBase table.
     * 
     * @throws  IOException
     *          On I/O error.
     */
    public void deleteTable() throws IOException {
        admin = connection.getAdmin();
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
        admin.close();
    }

    /**
     * Write a row in the table.
     * 
     * @throws  IOException
     *          On I/O error.
     */
    public void write() throws IOException {
        Table table = connection.getTable(tableName);
        Put put = new Put(b("doe-john-m-12345"));
        put.addColumn(b("personal"), b("givenName"), b("John"));
        put.addColumn(b("personal"), b("mi"), b("M"));
        put.addColumn(b("personal"), b("surame"), b("Doe"));
        put.addColumn(b("contactinfo"), b("email"), b("john.m.doe@gmail.com"));
        table.put(put);
        table.close();
    }

    /**
     * Get a row for a given key.
     * 
     * @throws  IOException
     *          On I/O error.
     */
    public Result get() throws IOException {
        Table table = connection.getTable(tableName);
        Get get = new Get(b("doe-john-m-12345"));
        get.addFamily(b("personal"));
        get.setMaxVersions(3);
        Result result = table.get(get);
        // print row
        System.out.println(result);
        table.close();
        return result;
    }

    /**
     * Scan table.
     * 
     * @throws  IOException
     *          On I/O error.
     */
    public void scan() throws IOException {
        Table table = connection.getTable(tableName);
        Scan scan = new Scan(b("doe-"));
        scan.addColumn(b("personal"), b("givenName"));
        scan.addColumn(b("contactinfo"), b("email"));
        scan.setFilter(new PageFilter(25));
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            // print row
            System.out.println(result);
        }
        table.close();
    }

    /**
     * Test HBase basic operations.
     * 
     * @throws  IOException
     *          On I/O error.
     */
    public void testHBase() throws IOException {
        this.createTable();
        this.write();
        this.get();
        this.scan();
        this.deleteTable();
    }
    
    /**
     * Main entry.
     * 
     * @param   args
     *          Command line arguments.
     * 
     * @throws  IOException
     *          On I/O errors.
     */
    public static void main(String[] args) throws IOException {
        HBase hb = new HBase();
        hb.testHBase();
        hb.connection.close();
    }
}

HBase Thrift interface

Another way to access HBase using Java or other programming languages is to use the HBase Thrift interface. Here is an example using Java programming language. In order to access HBase from Python you can use HappyBase which uses the Python Thrift library.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * HBase Thrift interface test.
 * 
 * @author Marius
 *
 */
public class Thrift {

    /* Default Thrift server host */
    private static final String THRIFT_HOST = "localhost";

    /* Default Thrift server port */
    private static final int THRIFT_PORT = 9090;

    /* UTF-8 decoder */
    private final CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();

    /* I/O layer */
    private TTransport socket;

    /* HBase client. */
    private Hbase.Client client;

    /**
     * Default constructor.
     */
    public Thrift() {
        this(THRIFT_HOST, THRIFT_PORT);
    }

    /**
     * Parameter constructor.
     * 
     * @param   host
     *          Thrift server host.
     * @param   port
     *          Thrift server host.
     */
    public Thrift(String host, int port) {
        socket = new TSocket(host, port);// IP Host Name
        TProtocol protocol = new TBinaryProtocol(socket, true, true);
        client = new Hbase.Client(protocol);
    }

    /**
     * Open Thrift connection.
     * 
     * @throws  TTransportException
     *          On error.
     */
    private void open() throws TTransportException {
        socket.open();
    }

    /**
     * Close Thrift connection.
     */
    private void close() {
        socket.close();
    }

    /**
     * Translate byte[] into an UTF-8 string.
     * 
     * @param   data
     *          Input data.
     * 
     * @return  Translated UTF-8 string.
     * 
     * @throws  CharacterCodingException
     *          On error.
     */
    private String utf8(byte[] data) throws CharacterCodingException {
        return decoder.decode(ByteBuffer.wrap(data)).toString();
    }

    /**
     * Test HBase Thrift interface.
     * Thrift server MUST be started before doing tests.
     * 
     * $ hbase thrift start
     * 
     * @throws  TTransportException
     *          On error.
     */
    public void thrift() throws TTransportException {
        open();
        System.out.println("open");
        try {
            System.out.println("scanning tables...");
            for (ByteBuffer name : client.getTableNames()) {
                // display table name
                System.out.println(this.utf8(name.array()));
            }
        } catch (IOException | TException e) {
            e.printStackTrace();
        } finally {
            System.out.println("close");
            close();
        }
    }

    /**
     * Test HBase Thrift interface.
     * 
     * @throws  TTransportException
     *          On error.
     */
    public void testThrift() throws TTransportException {
        this.thrift();
    }

    /**
     * Main entry.
     * 
     * @param   args
     *          Command line arguments.
     * 
     * @throws  IOException
     * @throws  TTransportException
     *          On error.
     */
    public static void main(String[] args) throws IOException, TTransportException {
        Thrift hb = new Thrift();
        hb.testThrift();
    }
}

Flag Counter