我如何开发一个Hbase Web应用程序? [英] How can I Develop a Hbase Web Application?

查看:159
本文介绍了我如何开发一个Hbase Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我开发一个使用HBase作为后端数据库的Web应用程序,因为我是HBase的新手,我没有任何关于开发HBase Web应用程序的想法。我正在像JSP程序那样在JSP中编写代码,但它没有得到。请帮助我找出程序或任何示例,以便我可以尝试它。



预先致谢

解决方案

您可以使用HBase作为后端或数据库层到您的应用程序,就像您使用任何RDBMS一样。

您需要使用连接器通过Java连接到HBase,并且您可以执行插入/更新/删除/选择(CRUD)操作。



要连接到HBase数据库,您需要使用 hbase-client.jar ,您可以使用

<$添加maven依赖项p $ p> <依赖性>
< groupId> org.apache.hbase< / groupId>
< artifactId> hbase-client< artifactId>
< version> 1.1.0.1< / version>
< dependency>

然后,您需要通过添加hbase-site.xml和core-site.xml文件来创建配置对象路径作为资源

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

然后创建与HBase的连接并创建Table对象并在此表上执行扫描操作

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

扫描扫描=新扫描();
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));

一旦您执行了扫描仪操作,您将得到ResultScanner,它更像JDBC中的ResultSet。循环并检索行创建所需bean的pojo或执行任何您想要执行的操作。

  resultScanner = table .getScanner(扫描); 
$ b $ 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));

字符串firstName = Bytes.toString(firstNameValue);
String lastName = Bytes.toString(lastNameValue);
String email = Bytes.toString(emailValue);
字符串性别= Bytes.toString(genderValue);
String age = Bytes.toString(ageValue);

System.out.println(First Name:+ firstName +--- Last Name:+ lastName +--- Email:+ email +--- Gender: +性别+---年龄:+年龄);
}

有关更多详情,请查看我的博客文章

完整的代码,你可以检查我的github


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

解决方案

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

这篇关于我如何开发一个Hbase Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆