最近学习了下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 puts = new ArrayList();
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目录下,然后设置一系列参数配置
hbase.rootdir
hdfs://master:9000/hbase
hbase.zookeeper.quorum
master
hbase.zookeeper.property.dataDir
/home/hbase-1.2.6/data/zookeeper
hbase.cluster.distributed
true
直接运行就可以了。