关于Hbase在javaAPI操作表的操作

来源:互联网 发布:数据分析预测软件 编辑:程序博客网 时间:2024/05/17 20:55
话不多说,直接看代码就能看清楚每个API是干嘛的。
package hbase;import java.io.IOException;import java.util.ArrayList;import java.util.List;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.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.HBaseAdmin;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.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.util.Bytes;public class hbaseTest {//声明静态配置 HBaseConfiguration   static Configuration cfg=HBaseConfiguration.create(); //创建一张表,通过HBaseAdmin HTableDescriptor来创建
    public static void creat(String tablename,String [] cfs) throws Exception {        HBaseAdmin admin = new HBaseAdmin(cfg);        if (admin.tableExists(tablename)) {            System.out.println("table Exists!");            System.exit(0);        }        else{            HTableDescriptor tableDesc = new HTableDescriptor(tablename);            for(int i=0;i<cfs.length;i++){            tableDesc.addFamily(new HColumnDescriptor(cfs[i]));            }            admin.createTable(tableDesc);            System.out.println("create table success!");        }    }
//插入数据    public static void put(String tablename, String row, String []columnFamily, String column, String data) throws Exception    {    HTable table = new HTable(cfg, tablename);    Put p1 = new Put(Bytes.toBytes(row));     for(int i=0;i<columnFamily.length;i++){    p1.add(Bytes.toBytes(columnFamily[i]),     Bytes.toBytes(column),    Bytes.toBytes(data));    table.put(p1);     }    System.out.println("put '"+row+"','"+columnFamily+":"+column+"','"+data+"'");        }
//查询    public static void get(String tablename, String row) throws Exception    {    HTable table = new HTable(cfg, tablename);    Get g = new Get(Bytes.toBytes(row));    Result result = table.get(g);    System.out.println("Get :" + result);    }
//scan查询,可用于filter    public static void scan(String tablename) throws Exception    {    HTable table = new HTable(cfg, tablename);    Scan s = new Scan();    ResultScanner rs = table.getScanner(s);    for (Result r : rs) {    System.out.println("Scan :" + r);    }            rs.close();    }
//删除数据   public static void  delete(String tablename ,String rowkey) throws IOException{            HTable table = new HTable(cfg, tablename);List list =new ArrayList();Delete dl =new Delete(rowkey.getBytes());list.add(dl);table.delete(list);System.out.println("delete to success");    }
//删除表   public static boolean delete(String tablename) throws IOException{              HBaseAdmin admin=new HBaseAdmin(cfg);       if(admin.tableExists(tablename)){               try               {                       admin.disableTable(tablename);                       admin.deleteTable(tablename);               }catch(Exception ex){                       ex.printStackTrace();                       return false;               }                      }       return true;}   public static void  main (String [] agrs) {       String tablename="hbase_tb";       String [] columnFamily={"cf"};            try {                            hbaseTest.creat(tablename, columnFamily);       hbaseTest.put(tablename, "row1", columnFamily, "cl1", "data");          hbaseTest.get(tablename, "row1");       hbaseTest.scan(tablename);/*           if(true==HBaseTestCase.delete(tablename))               System.out.println("Delete table:"+tablename+"success!");*/              }   catch (Exception e) {       e.printStackTrace();   }    }}


0 0
原创粉丝点击