How can I Develop a Hbase Web Application?

本小妞迷上赌 提交于 2020-01-17 06:36:51

问题


Can any one help me in developing a web application that uses the HBase as its backend database, as I'm new to HBase, I'm not having any idea of developing HBase web application. I'm writing the code in JSPs like a JDBC program, but it's not getting. Please help me to find out procedure or any sample examples so that I can try it.

Thanks in advance


回答1:


You can use HBase as Backend or Database layer to your application, the same way you use any RDBMS.

You need to connect to HBase through Java using connectors and you can perform the Insert/Update/Delete/Select (CRUD) operations.

To connect to HBase database you need to use hbase-client.jar which you can add maven dependency using below

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client<artifactId>
    <version>1.1.0.1</version>
<dependency>

Then you need to Create Configuration object by adding hbase-site.xml and core-site.xml file paths as resources

Configuration config = HBaseConfiguration.create();
config.addResource(new Path("/etc/hbase/conf/hbase-site.xml"));
config.addResource(new Path("/etc/hadoop/conf/core-site.xml"));

Then create connection to HBase and create Table object and perform Scan operation on top of this table

String tableName = "peoples";
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("name"), Bytes.toBytes("first"));
scan.addColumn(Bytes.toBytes("name"), Bytes.toBytes("last"));
scan.addColumn(Bytes.toBytes("contactinfo"), Bytes.toBytes("email"));
scan.addColumn(Bytes.toBytes("personalinfo"), Bytes.toBytes("gender"));
scan.addColumn(Bytes.toBytes("personalinfo"), Bytes.toBytes("age"));

Once you perform scanner opetation you will get ResultScanner which is more like a ResultSet in JDBC. Loop over it and retrieve the rows create a pojo of your required bean or do whatever operation you want to do.

resultScanner = table.getScanner(scan);

for (Result result = resultScanner.next(); result != null; result = resultScanner.next()) {
    byte[] firstNameValue = result.getValue(Bytes.toBytes("name"), Bytes.toBytes("first"));
    byte[] lastNameValue = result.getValue(Bytes.toBytes("name"), Bytes.toBytes("last"));
    byte[] emailValue = result.getValue(Bytes.toBytes("contactinfo"), Bytes.toBytes("email"));
    byte[] genderValue = result.getValue(Bytes.toBytes("personalinfo"), Bytes.toBytes("gender"));
    byte[] ageValue = result.getValue(Bytes.toBytes("personalinfo"), Bytes.toBytes("age"));

    String firstName = Bytes.toString(firstNameValue);
    String lastName = Bytes.toString(lastNameValue);
    String email = Bytes.toString(emailValue);
    String gender = Bytes.toString(genderValue);
    String age = Bytes.toString(ageValue);

    System.out.println("First Name : " + firstName + " --- Last Name : " + lastName + " --- Email : " + email + " --- Gender : " + gender + " --- Age : " + age);
}

For more details check my blog post

For complete code you can check my github



来源:https://stackoverflow.com/questions/33491212/how-can-i-develop-a-hbase-web-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!