HBase(0.96)新的Java API操作
package test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
public class HBaseCommons {
//声明静态配置
static Configuration conf=null;
static HConnection conn=null;
static{
conf=HBaseConfiguration.create();
// conf.set("hbase.zookeeper.quorum","Master.Hadoop");
try {
conn=HConnectionManager.createConnection(conf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 创建表
*
* @tableName 表名
*
* @family 列簇列表
*/
public static void createTable(String tableName,String[] family)
throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);
HTableDescriptor desc=new HTableDescriptor(TableName.valueOf(tableName));//新的语法
for(int i=0;i<family.length;i++)
{
desc.addFamily(new HColumnDescriptor(family[i]));
}
if(admin.tableExists(tableName))
{
System.out.println("table Exists!");
System.exit(0);
} else{
admin.createTable(desc);
System.out.println("create table Sucess!");
}
}
/*
* 为表添加数据(适合知道有多少列簇的固定表)
*
* @rowKey rowKey
*
* @tableName 表名
*
* @column1 第一个列簇列表
*
* @value1 第一个列的值的列表
*
* @column2 第二个列簇列表
*
* @value2 第二个列的值的列表
*/
public static void addData(String rowKey, String tableName, String[] column1,
String[] value1, String[] column2, String[] value2)throws IOException{
Put put=new Put(Bytes.toBytes(rowKey)); //设置rowKey
HTableInterface table=conn.getTable(tableName); //获取表
HColumnDescriptor[] columnFamilies=table.getTableDescriptor().getColumnFamilies();//获取所有的列簇
for(int i=0;i<columnFamilies.length;i++){
String familyName=columnFamilies[i].getNameAsString();//获取列簇名
if(familyName.equals("article")){ //article列簇put数据
for(int j=0;j<column1.length;j++){
put.add(Bytes.toBytes(familyName),
Bytes.toBytes(column1[j]),
Bytes.toBytes(value1[j])
);
}
}
if(familyName.equals("author")){ //article列簇put数据
for(int j=0;j<column2.length;j++){
put.add(Bytes.toBytes(familyName),
Bytes.toBytes(column2[j]),
Bytes.toBytes(value2[j])
);
}
}
}
table.put(put);
table.close();
System.out.println("add data Sucess!");
}
/*
* 根据rowkey查询
*
* @rowKey rowKey
*
* @tableName 表名
*/
public static Result getResult(String tableName,String rowKey)
throws IOException{
Get get=new Get(Bytes.toBytes(rowKey));
HTableInterface table=conn.getTable(tableName); //获取表
Result result=table.get(get);
for(Cell kv:result.rawCells())
{
System.out.println("family:"+new String(CellUtil.cloneFamily(kv)));
System.out.println("qualifier:"+new String(CellUtil.cloneQualifier(kv)));
System.out.println("value:"+new String(CellUtil.cloneValue(kv)));
System.out.println("Timestamp:"+kv.getTimestamp());
System.out.println("------------------------------------------");
/* System.out.println("value:"+new String(CellUtil.cloneValue(kv)));*/
}
table.close();
return result;
}
/*
* 遍历查询hbase表
*
* @tableName表名
*/
public static void getResultScan(String tableName)throws IOException{
Scan scan=new Scan();
ResultScanner rs=null;
// HTable table=(HTable)tablePool.getTable(tableName);
HTableInterface table=conn.getTable(tableName);
// Configuration hbaseConf=HBaseConfiguration.create();
// HTable table=new HTable(hbaseConf,tableName);
try {
rs=table.getScanner(scan);
for(Result r:rs)
{
for(Cell kv:r.rawCells())
{
System.out.println("family:"+new String(CellUtil.cloneFamily(kv)));
System.out.println("qualifier:"+new String(CellUtil.cloneQualifier(kv)));
System.out.println("value:"+new String(CellUtil.cloneValue(kv)));
System.out.println("Timestamp:"+kv.getTimestamp());
System.out.println("------------------------------------------");
/* System.out.println("value:"+new String(CellUtil.cloneValue(kv)));*/
}
}
} catch (Exception e) {
// TODO: handle exception
}finally{
rs.close();
table.close();
}
}
/*
* 查询表中的某一列
*
* @tableName 表名
*
* @rowKey rowKey
*
* @familyName 列簇
*
* @columnName 列名
*/
public static void getResultByColumn(String tableName,String rowKey,
String familyName,String columnName)throws IOException{
HTableInterface table=conn.getTable(tableName);
Get get=new Get(Bytes.toBytes(rowKey));
//获取指定列簇和列修饰符对应的列
get.addColumn(Bytes.toBytes(familyName),Bytes.toBytes(columnName));
Result result=table.get(get);
for(Cell kv:result.rawCells())
{
System.out.println("family:"+new String(CellUtil.cloneFamily(kv)));
System.out.println("qualifier:"+new String(CellUtil.cloneQualifier(kv)));
System.out.println("value:"+new String(CellUtil.cloneValue(kv)));
System.out.println("Timestamp:"+kv.getTimestamp());
System.out.println("------------------------------------------");
/* System.out.println("value:"+new String(CellUtil.cloneValue(kv)));*/
}
table.close();
}
/*
* 更新表中的某一列
*
* @tableName 表名
*
* @rowKey rowKey
*
* @familyName 列簇名
*
* @columnName 列名
*
* @value 更新后的值
*/
public static void updateTable(String tableName,String rowKey,
String familyName,String columnName,String value)
throws IOException{
HTableInterface table=conn.getTable(tableName);
Put put=new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(familyName),Bytes.toBytes(columnName),
Bytes.toBytes(value));
table.put(put);
table.close();
System.out.println("update table Success!");
}
/*
* 查询某列数据的多个版本
*
* @tableName 表名
*
* @rowKey rowKey
*
* @familyName 列簇名
*
* @columnName 列名
*/
public static void getResultByVersion(String tableName,String rowKey,
String familyName,String columnName)throws IOException{
HTableInterface table=conn.getTable(tableName);
Get get=new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName),Bytes.toBytes(columnName));
get.setMaxVersions(5);
Result result=table.get(get);
for(Cell kv:result.rawCells())
{
System.out.println("family:"+new String(CellUtil.cloneFamily(kv)));
System.out.println("qualifier:"+new String(CellUtil.cloneQualifier(kv)));
System.out.println("value:"+new String(CellUtil.cloneValue(kv)));
System.out.println("Timestamp:"+kv.getTimestamp());
System.out.println("------------------------------------------");
}
table.close();
}
/*
* 删除指定的列
*
* @tableName 表名
*
* @rowKey rowKey
*
* @familyName 列簇名
*
* @columnName 列名
*/
public static void deleteColumn(String tableName,String rowKey,
String familyName,String columnName)throws IOException{
HTableInterface table=conn.getTable(tableName);
Delete deleteColumn=new Delete(Bytes.toBytes(rowKey));
deleteColumn.deleteColumns(Bytes.toBytes(familyName),Bytes.toBytes(columnName));
table.delete(deleteColumn);
table.close();
System.out.println(familyName+":"+columnName+"is deleted!");
}
/*
* 删除指定的列
*
* @tableName 表名
*
* @rowKey rowKey
*/
public static void deleteAllColumn(String tableName,String rowKey)
throws IOException{
HTableInterface table=conn.getTable(tableName);
Delete deleteAll=new Delete(Bytes.toBytes(rowKey));
table.delete(deleteAll);
table.close();
System.out.println("all columns are deleted!");
}
/*
* 删除表
*
* @tableName 表名
*/
public static void deleteTable(String tableName)throws IOException{
HBaseAdmin admin=new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+" is deleted!");
}
public static void main(String[] args) throws Exception{
getResultScan("Movie");
getResult("Movie","7065187");
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。