HBase在MyEclispe上执行各种API的操作
如:命名空间的创建,删除,和查询表空间
表的创建,插入数据(分为单行和多行),删除,查询(分为单行和多行,和区间查询),
切分:分为regionname 和table
过滤:过滤满足条件的数据
public class HBaseDemo {
private static Configuration conf;
public static Connection conn;
static {
conf=HBaseConfiguration.create();
try {
conn=ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建命名空间
* @param args
*/
public static void createNS(String namespace){
Admin admin = null;
try {
admin=conn.getAdmin();
NamespaceDescriptor nsdesc=NamespaceDescriptor.create(namespace).build();
admin.createNamespace(nsdesc);
System.out.println("---创建成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---创建失败");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除命名空间
* @param args
*/
public static void deleteNS(String namespace){
Admin admin = null;
try {
admin=conn.getAdmin();
admin.deleteNamespace(namespace);
System.out.println("---删除成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---删除失败");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 查询命名空间
* @param args
*/
public static void listNS(){
Admin admin = null;
try {
admin=conn.getAdmin();
NamespaceDescriptor[] nss=admin.listNamespaceDescriptors();
for (NamespaceDescriptor ns : nss) {
System.out.println(ns.getName());
}
System.out.println("---查询成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---查询失败");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 创建表
*
*/
public static void creatTable(String tablename,String columnfamily){
Admin admin=null;
try {
admin=conn.getAdmin();
TableName tname=TableName.valueOf(tablename);
HTableDescriptor htdesc=new HTableDescriptor(tname);
HColumnDescriptor hbdesc=new HColumnDescriptor(columnfamily);
htdesc.addFamily(hbdesc);
admin.createTable(htdesc);
System.out.println("---创建表成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---创建表失败");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除表
* @param tablename
*/
public static void deleteTable(String tablename){
Admin admin=null;
try {
admin=conn.getAdmin();
TableName tname=TableName.valueOf(tablename);
admin.disableTable(tname);
HTableDescriptor htdesc=new HTableDescriptor(tname);
admin.deleteTable(tname);
System.out.println("---删除表成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---删除表失败");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 单行插入put 到表中
* @param tablename
*/
public static void put(String tablename,String rowkey,String family,String qualifier,String value){
try {
Table _table= conn.getTable(TableName.valueOf(tablename));
Put _put=new Put(Bytes.toBytes(rowkey));
_put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
_table.put(_put);
System.out.println("---单行插入成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---单行插入失败");
}
}
/**
* 多行插入puts
* @param tablename
*/
public static void puts(String tablename,Put put){
try {
Table _table= conn.getTable(TableName.valueOf(tablename));
Put _put=new Put(Bytes.toBytes("row-2"));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes("28"));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("sex"), Bytes.toBytes("男"));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("class"), Bytes.toBytes("DB1702"));
HBaseDemo.puts("ns1:tt", _put);
_table.put(put);
System.out.println("---多行插入成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---多行插入失败");
}
}
public static void close(){
try {
conn.close();
System.out.println("---关闭成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---关闭失败");
}
}
/**
* 多行插入,循环插入 putsList
* @param tablename
*/
public static void putsList(String tablename){
try {
Table _table= conn.getTable(TableName.valueOf(tablename));
List<Put> list=new ArrayList<Put>();
for (int i=0;i<10000;i++){
Put _put=new Put(Bytes.toBytes("row-"+i));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"+i), Bytes.toBytes("lisi"+i));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"+i), Bytes.toBytes("28"+i));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("sex"+i), Bytes.toBytes("男"+i));
_put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("class"+i), Bytes.toBytes("DB1702"+i));
list.add(_put);
}
_table.put(list);
System.out.println("---多行插入成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("---多行插入失败");
}
}
/**
* 得到单行记录:
* @param tablename
* @param rowkey
*/
public static void getone(String tablename,String rowkey){
try {
Table _table=conn.getTable(TableName.valueOf(tablename));
Get _get=new Get(Bytes.toBytes(rowkey));
Result rs=_table.get(_get);
List<Cell> cells=rs.listCells();
for(Cell cell:cells){
System.out.println("列簇伪:"+Bytes.toString( CellUtil.cloneFamily(cell)));
System.out.println("列限定符伪:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("rowkey伪:"+Bytes.toString(CellUtil.cloneRow (cell)));
System.out.println("value伪:"+Bytes.toString(CellUtil.cloneValue(cell)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void printOut(Result rs){
List<Cell> cells=rs.listCells();
for(Cell cell:cells){
String family= Bytes.toString( CellUtil.cloneFamily(cell));
String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));
String rowkey= Bytes.toString(CellUtil.cloneRow (cell));
String value= Bytes.toString(CellUtil.cloneValue(cell));
long timestamp=cell.getTimestamp();
System.out.println(rowkey+"column="+family+":"+qualifier+",timestamp="+timestamp+",value="+value);
}
}
/**
* 得到多行记录:
* @param tablename
* @param rowkey
*/
public static void getlsit(String tablename,String rowkey){
try {
Table _table=conn.getTable(TableName.valueOf(tablename));
Get _get=new Get(Bytes.toBytes(rowkey));
Result rs=_table.get(_get);
printOut(rs);
System.out.println("---get得到成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("---get失败");
}
}
/**
* 查找全部,运用缓冲
* @param tablename
*/
public static void findAll(String tablename) {
try {
Table _table=conn.getTable(TableName.valueOf(tablename));
Scan scan=new Scan();
scan.setBatch(1000); //3704 1000 2945
scan.setCaching(100); //3020
long starttime=System.currentTimeMillis();
ResultScanner rsscan=_table.getScanner(scan);
Iterator<Result> its=rsscan.iterator();
while(its.hasNext()){
printOut(its.next());
}
System.out.println("---查询成功");
System.out.println( System.currentTimeMillis()-starttime); //不设置 3303
} catch (Exception e) {
e.printStackTrace();
System.out.println("---查询失败");
}
}
/**
* 查找区间
* @param tablename
*/
public static void findByRowkey(String tablename,String startrow,String stopRow) {
try {
Table _table=conn.getTable(TableName.valueOf(tablename));
Scan scan=new Scan(Bytes.toBytes(startrow), Bytes.toBytes(stopRow));
ResultScanner rsscan=_table.getScanner(scan);
Iterator<Result> its=rsscan.iterator();
while(its.hasNext()){
printOut(its.next());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 切分table
* @param regionname
* 数据太少的话 ,不能切,得指定以什么切了
*/
public static void spilttable(String tablename){
Admin admin=null;
try {
admin=conn.getAdmin();
admin.split(TableName.valueOf(tablename),Bytes.toBytes("5")) ;
System.out.println("---切分成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("---切分失败");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}
}
}
/**
* 切分region
* @param regionname
*/
@Test
public static void spiltregion( String regionname){
Admin admin=null;
try {
admin=conn.getAdmin();
admin.splitRegion(Bytes.toBytes(regionname) );
System.out.println("---切分成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("---切分失败");
}finally{
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 过滤器:
* @throws IOException
*
*/
public static void myFilter(String tablename,String colfam1) throws IOException{
try{
Table table=conn.getTable(TableName.valueOf(tablename));
Scan scan=new Scan(); //列族
scan.addColumn(Bytes.toBytes(colfam1), Bytes.toBytes("name"));
Filter filter1=new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row-22")));
scan.setFilter(filter1);
ResultScanner scanner1=table.getScanner(scan);
for(Result res:scanner1){
System.out.println(res);
}
System.out.println("---过滤成功");
scanner1.close();
}catch (Exception e){
System.out.println("---过滤失败");
}
}
/*
*
*/
public static void batch() throws IOException, InterruptedException{
Table _table=HBaseDemo.conn.getTable(TableName.valueOf("ns1:t1"));
List<Row> action=new ArrayList<Row>();
Put _put=new Put(Bytes.toBytes("row-10000"));
_put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("name"),Bytes.toBytes("asa"));
action.add(_put);
Delete _delete=new Delete(Bytes.toBytes("row-1"));
_delete.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"));
action.add(_delete);
Get _get=new Get(Bytes.toBytes("row-1"));
action.add(_get);
Object[] obj=_table.batch(action);
Result rs=(Result)obj[2]; //插入,删除,查询get随时切换
HBaseDemo.printOut(rs);
}
}