2015-11-03 80 views
0

任何人都可以幫助我開發一個使用HBase作爲後端數據庫的Web應用程序,因爲我是HBase的新手,我沒有任何想法開發HBase Web應用程序。我正在像JSP程序那樣在JSP中編寫代碼,但它沒有得到。請幫助我找出程序或任何示例,以便我可以嘗試。如何開發Hbase Web應用程序?

在此先感謝

回答

3

您可以使用HBase的作爲後端或數據庫層到應用程序,你可以使用任何RDBMS的方式相同。

您需要使用連接器通過Java連接到HBase,並且您可以執行插入/更新/刪除/選擇(CRUD)操作。

要連接到HBase的數據庫,你需要使用HBase的-client.jar中,您可以使用下面添加

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

然後,你需要通過添加HBase的-site.xml中創建配置對象Maven的依賴和核 - site.xml文件路徑作爲資源

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

然後創建連接到HBase的和創建表對象並在此表中的頂部執行掃描操作

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")); 

一旦您執行掃描儀操作,您將得到ResultScanner,它更像JDBC中的ResultSet。在它上面循環並檢索行可創建所需bean的pojo或執行任何您想要執行的操作。

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); 
} 

有關詳情,請my blog post

有關完整的代碼,你可以檢查my github

+0

我看到了你的博客文章;如果我將這段代碼寫入servlet或jsp,我有一個問題,那麼你認爲它會起作用嗎? – retiremonk

+0

它應該,在你的servlet中,因爲你試圖連接並從你的HBase表中獲取數據。確保您已經將路徑傳遞給hbase-site.xml和core-site.xml,並以相同的方式部署了ur Web應用程序 –