欢迎投稿

今日深度:

hbase api,

hbase api,


Configuration config = HbaseConfiguration.create();  // 创建一个Hbase的实例  
HbaseAdmin admin = new HbaseAdmin(config);           // 获取一个Hbase实例的句柄  
HTableDescriptor htd = new HTableDescriptor("tableName");// 创建一个表名  
HColumnDescriptor hcd = new HColumnDescriptor("columnCell"); //创建一个列簇  
hcd.addFamily(htd); //把列簇放到表中  
admin.createTable(htd);//创建一个表  
byte [] tableName= htd.getName(); // 把表名转成一个byte 型  
HTableDescriptor [] tables = admin.listTables(); //获取此Hbase实例句柄下所有的表名  
//如果此Hbase 下有表,而且,和需要创建的表名一样,创建失败,抛出异常。  
if(tables.length!=1 && Bytes.equals(tableName,tables[0].getName())){   
  throw new IOException("that table is exits,created faild");  
}  
HTable table = new HTable (config.tableName);  // 从Hbase句柄下获取指定的表名  
byte [] row1 = Bytes.toBytes("row1");   
Put p1 = new Put(row1); // 创建一个key 为row1 的值  
byte [] dataBytes = Bytes.toBytes("data"); //表名下创建的列簇  
//把列名为data:1,值为values1 的值 放到一个key中  
p1.add(dataBytes,Bytes.toBytes("1"),Bytes.toBytes("values1"));  
table.put(p1); //把key 放到table 中   
Get g = new Get(row1);//创建一个键值  
Result result = table.get(g); //表通过键值 获取一条记录  
System.out.pringtln("get:"+result);//把记录打印出来  
Scan scan = new Scan();    // 创建一个扫描指针  
ResultResult scanner = table.getScanner(scan);//表名通过指定来扫描每一条记录  
for(Result scannerResult:scanner){  
  System.out.println("scan:"+scannerResult);  
}  
scanner.close();// 关闭 扫描 ,释放资源  
admin.disableTable(tableName);//把表设为不可用  
admin.deleteTable(tableName);//删除表  

www.htsjk.Com true http://www.htsjk.com/hbase/39239.html NewsArticle hbase api, Configuration config = HbaseConfiguration. create (); // 创建一个Hbase的实例 HbaseAdmin admin = new HbaseAdmin(config); // 获取一个Hbase实例的句柄 HTableDescriptor htd = new HTableDescriptor( "tableName" ) ;// 创...
相关文章
    暂无相关文章
评论暂时关闭