package hbaseCURD;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
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.util.Bytes;
public class test {
public static void main(String[] args) throws IOException {
TableManager tm = new TableManager();
tm.getConf();
// tm.createTable("testtable","cf1","cf2");
HTable mytable=tm.getTableObj("testtable");
// Put put = new Put(Bytes.toBytes("row1"));
// put.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("cf1ddfddvalue"));
// put.add(Bytes.toBytes("cf2"), Bytes.toBytes("c2"), Bytes.toBytes("cf2v3333alue"));
// mytable.put(put);
//查詢
Get get = new Get(Bytes.toBytes("row1"));
Result result = mytable.get(get);
System.out.println("get result:" + Bytes.toString(result.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("c1"))));
//Result[] result = table.get(List<Get>);//查詢指定Rowkey的多條記錄
}
}
package hbaseCURD;
import java.io.IOException;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
public class TableManager {
private static Configuration conf;
// 設置集群的配置信息
public void getConf() {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master,slave1,slave2");
conf.set("hbase.master", "master:60000");
}
// 獲取表對象
public HTable getTableObj(String tablename) {
HTable mytable = null;
try {
mytable = new HTable(conf, tablename);
} catch (IOException e) {
e.printStackTrace();
}
return mytable;
}
//
public void createTable(String tableName, String... args) throws MasterNotRunningException, ZooKeeperConnectionException {
// args數組保存的是列族
HBaseAdmin admin = new HBaseAdmin(conf);
// 創建表
HTableDescriptor htd = new HTableDescriptor(tableName);
for (String st : args) {
htd.addFamily(new HColumnDescriptor(st));
}
try {
admin.createTable(htd);
} catch (IOException e) {
e.printStackTrace();
}
}
public void deleteTable(String tableName) {
try {
HBaseAdmin admin = new HBaseAdmin(conf);
// 創建表
HTableDescriptor htd = new HTableDescriptor(tableName);
admin.disableTable(Bytes.toBytes(tableName));
admin.deleteTable(Bytes.toBytes(tableName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void modifyTable(String tableName,String newcoloumf) {
try {
HBaseAdmin admin = new HBaseAdmin(conf);
// 創建表
HTableDescriptor htd = new HTableDescriptor(tableName);
admin.disableTable(Bytes.toBytes(tableName));
admin.modifyColumn(tableName, new HColumnDescriptor("cf1"));
admin.enableTable(tableName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Hbase的訪問方式
1、Native Java API:最常規和高效的訪問方式;
2、HBase Shell:HBase的命令行工具,最簡單的接口,適合HBase管理使用;
3、Thrift Gateway:利用Thrift序列化技術,支持C++,PHP,Python等多種語言,適合其他異構系統在線訪問HBase表數據;
4、REST Gateway:支持REST 風格的Http API訪問HBase, 解除了語言限制;
5、MapReduce:直接使用MapReduce作業處理Hbase數據;
6、使用Pig/hive處理Hbase數據。
常用Java API的用法:
1、加載配置
[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
Configuration config = HBaseConfiguration.create();
//可以自定義配置,也可以從自定義配置文件中讀取
/*config.set(“hbase.zookeeper.property.clientPort”, “4181”);
config.set(“hbase.zookeeper.quorum”, “hadoop.datanode5.com,hadoop.datanode2.com,hadoop.datanode3.com”);
config.set(“hbase.master”, “hadoop.datanode3.com\:600000”);*/
2、表的創建、表信息修改、表刪除
[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
HBaseAdmin admin = new HBaseAdmin(config);
//創建表
HTableDescriptor htd = new HTableDescriptor(tableName);
htd.addFamily(new HColumnDescriptor(“cf1”));
htd.addFamily(new HColumnDescriptor(“cf2”));
admin.createTable(htd);
//修改表信息
admin.disableTable(tableName);
// modifying existing ColumnFamily
admin.modifyColumn(tableName, new HColumnDescriptor(“cf1”));
admin.enableTable(tableName);
//刪除表
admin.disableTable(Bytes.toBytes(tableName));
admin.deleteTable(Bytes.toBytes(tableName));
3、添加記錄
[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
/** 在多次使用時,建議用HTablePool
HTable table = new HTable(config, tableName);
=>
HTablePool pool = new HTablePool(config, 1000);
HTableInterface table = pool.getTable(tableName);*/
HTable table = new HTable(config, tableName);
/**
* 在插入操作時,默認不適用任何緩存
* 可自定義使用緩存,以及緩存大小
* 每個任務最后需要手工調用 flushCommits();
*/
/*table.setAutoFlush(false);
table.setWriteBufferSize(1024);*/
Put put1 = new Put(Bytes.toBytes(rowKey));
if (ts == 0) {
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
} else {
//自定義版本時,從自定義的版本號,類型為long
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), ts,Bytes.toBytes(value));
}
table.put(put1);
//table.flushCommits();
4、查詢,根據Rowkey查詢
[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
Get get1 = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get1);
System.out.println(“get result:” + Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));
Result[] result = table.get(List);//查詢指定Rowkey的多條記錄
5、查詢,指定條件和rowkey區間查詢
[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
Scan scan = new Scan();
//默認緩存大小為1,設置成一個合理的值,可以減少scan過程中next()的時間開銷,代價是客戶端的內存
scan.setCaching(500);
scan.setCacheBlocks(false);
//根據startRowKey、endRowKey查詢
//Scan scan = new Scan(Bytes.toBytes(“startRowKey”), Bytes.toBytes(“endRowKey”));
//rowKey之外的過濾條件,在List中可以add;
/**List filters = new ArrayList();
Filter filter = new SingleColumnValueFilter(“familyName”.getBytes(),
“qualifierName”.getBytes(),
CompareOp.EQUAL,
Bytes.toBytes(“value”));
filters.add(filter);
scan.setFilter(new FilterList(filters));*/
ResultScanner scanner = table.getScanner(scan);
System.out.println(“scan result list:”);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes(“data”), Bytes.toBytes(“data1”))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes(“data”), Bytes.toBytes(“data2”))));
}
scanner.close();
參考:
1、http://www.taobaotest.com/blogs/1605
2、http://abloz.com/hbase/book.html#data_model_operations(官網示例)
版權聲明:本文為博主原創文章,未經博主允許不得轉載。