標籤: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程式