最近學習了下HBase,面向列的數據庫,屬於NoSql範疇。通過編寫Java代碼來連接HBase數據庫。
一、前提
Hadoop+HBase環境已經裝好,然後利用Java HBase API操作HBase的話,最好將虛擬機中的HBase版本對應起來,我們從虛擬機中把HBase文件下載下來,利用Xftp軟件。
二、編寫代碼
將下載下來的HBase文件夾的 lib包下的所有jar包丟進項目 Library 中然後編寫代碼。
public class HBaseDBCon {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//初始化連接
static{
configuration = HBaseConfiguration.create();
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
//創建命名空間
createNameSpace("dp");
//創建表
createTable("dp:dept","info","subdept");
//批量添加數據
mulPut("dp:dept");
close();
}
/**
* 創建命名空間
* @param ns 命名空間名字
* @throws Exception
*/
public static void createNameSpace(String ns) throws Exception {
admin.createNamespace(NamespaceDescriptor.create(ns).build());
}
//批量添加數據
public static void mulPut(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
// 創建一個列表用於存放Put實例
List<Put> puts = new ArrayList<Put>();
Put put1 = new Put(Bytes.toBytes("0_001"));
put1.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("internet"));
put1.add(Bytes.toBytes("subdept"), Bytes.toBytes("1_001"), Bytes.toBytes("develop"));
put1.add(Bytes.toBytes("subdept"), Bytes.toBytes("1_002"), Bytes.toBytes("test"));
puts.add(put1);
Put put2 = new Put(Bytes.toBytes("1_001"));
put2.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("develop"));
put2.add(Bytes.toBytes("info"), Bytes.toBytes("f_pid"), Bytes.toBytes("0_001"));
put2.add(Bytes.toBytes("subdept"), Bytes.toBytes("2_001"), Bytes.toBytes("develop1"));
put2.add(Bytes.toBytes("subdept"), Bytes.toBytes("2_002"), Bytes.toBytes("develop2"));
put2.add(Bytes.toBytes("subdept"), Bytes.toBytes("2_003"), Bytes.toBytes("develop3"));
puts.add(put2);
table.put(puts);
table.close();
}
/**
* 創建表
* @param myTableName 表名
* @param colFamily 列簇
* @throws IOException
*/
public static void createTable(String myTableName,String[] colFamily) throws IOException {
TableName tableName = TableName.valueOf(myTableName);
//判斷是否存在該表
if(admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}
//close();
}
/**
* 刪除表
* @param tableName 表名
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
TableName tb_name = TableName.valueOf(tableName);
if (admin.tableExists(tb_name)) {
admin.disableTable(tb_name);
admin.deleteTable(tb_name);
}
//close();
}
/**
* 查看所有表
* @throws IOException
*/
public static void listTables() throws IOException {
//
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
//close();
}
/**
* 向表中插入數據
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列簇
* @param col 指定插入的列名
* @param val 指定插入列名的值
* @throws IOException
*/
public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
//利用Put插入數據
Put put = new Put(rowKey.getBytes());
//獲取列簇並添加值
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
//close();
}
/**
* 刪除一行數據
* @param tableName
* @param rowKey
* @param colFamily
* @param col
* @throws IOException
*/
public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
//刪除指定列族
//delete.addFamily(Bytes.toBytes(colFamily));
//刪除指定列
//delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
table.delete(delete);
table.close();
//close();
}
/**
* 根據rowkey查找數據
* @param tableName 指定表名
* @param rowKey 行鍵
* @param colFamily 列簇
* @param col 列名
* @throws IOException
*/
public static void getData(String tableName,String rowKey,String colFamily,String col)throws IOException{
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);
showCell(result);
table.close();
//close();
}
/**
* 輸出
* @param result 結果集
*/
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp());
System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
}
}
/**
* 關閉連接
*/
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
然後將下載下來的HBase文件夾中的hbase-site.xml文件放在src目錄下,然後設置一系列參數配置
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://master:9000/hbase</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>master</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/hbase-1.2.6/data/zookeeper</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
</configuration>
直接運行就可以了。