Hadoop-06-使用Eclipse開發HBase程式

來源:互聯網
上載者:User

標籤:des   style   ar   io   color   使用   sp   for   java   

使用Eclipse開發HBase程式的配置步驟

 

1.建立一個普通的java project.

2.在項目的 屬性--java build path--libraries--Add External Jars,添加hadoop安裝目錄下的hbase-0.90.5.jar和hbase-0.90.5-tests.jar,以及hbase安裝目錄下的lib目錄下的所有jar檔案。

3.在項目根目錄下建立conf目錄,然後拷貝hbase安裝目錄下的conf目錄下的hbase-site.xml到該檔案夾下。

4.在項目的 屬性--java build path--libraries--Add class folder,將剛剛建立的conf目錄添加進去。至此,開發環境搭建完成。

5.建立一個java類,添加如下代碼:

package com.hadoop.hbase.test;

 

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.client.Get;

import org.apache.hadoop.hbase.client.HBaseAdmin;

import org.apache.hadoop.hbase.client.HTable;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.util.Bytes;

 

public class HbaseTest {

 

public static void main(String[] args) throws Exception {

 

Configuration config = HBaseConfiguration.create();

 

// create table

HBaseAdmin admin = new HBaseAdmin(config);

HTableDescriptor htd = new HTableDescriptor("student");

HColumnDescriptor hcd = new HColumnDescriptor("address");

HColumnDescriptor hcd2 = new HColumnDescriptor("info");

htd.addFamily(hcd);

htd.addFamily(hcd2);

admin.createTable(htd);

byte[] tableName = htd.getName();

HTableDescriptor[] tables = admin.listTables();

if (tables.length != 1 && Bytes.equals(tableName, tables[0].getName())) {

throw new Exception("Failed create table !!!");

}

 

// run some operations--a put, a get, a scan --against the table

HTable table = new HTable(config, tableName);

byte[] row1 = Bytes.toBytes("John");

Put p1 = new Put(row1);

byte[] databytes = Bytes.toBytes("address");

p1.add(databytes, Bytes.toBytes("city"), Bytes.toBytes("WuHan"));

p1.add(databytes, Bytes.toBytes("province"), Bytes.toBytes("HuBei"));

table.put(p1);

 

Get g = new Get(row1);

Result result = table.get(g);

System.out.println("Result: " + result);

 

Scan scan = new Scan();

ResultScanner scanner = table.getScanner(scan);

for (Result scannerResult : scanner) {

System.out.println("Scan: " + scannerResult);

}

 

// drop the table

// admin.disableTable(tableName);

// admin.deleteTable(tableName);

System.out.println("----done----");

 

}

 

}

6.進入hbase shell,使用 list命令即可以看見建立的表,使用scan ‘student’ 即可以看見插入的資料。

 

至此,第一個hbase程式開發完成。

 

 

Hadoop-06-使用Eclipse開發HBase程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.