HBase的eclipse操作Java API
2015-02-09 16:19
211 查看
HBase的eclipse操作Java API
1、 创建一个表:
Console显示结果:
查看操作结果:
2、 添加一条数据:
主函数操作:
其中:xiaobaozi是表名,cf是表的一个列族,AWM是行健,laobing是一个列,huaju是列的具体值。
Console显示结果:
查看操作结果:
3、 查看一行记录:
对应子方法:
主方法:
Console显示结果:
4、查看所有记录:
对应子方法:
主方法:
Console显示结果:
5、删除一行记录:
对应子方法:
主方法:
Console显示结果:
查看结果:
6、删除表:
对应子方法:
主方法:
Console显示结果:
查看结果:
完整代码:
1、 创建一个表:
Console显示结果:
查看操作结果:
2、 添加一条数据:
主函数操作:
其中:xiaobaozi是表名,cf是表的一个列族,AWM是行健,laobing是一个列,huaju是列的具体值。
Console显示结果:
查看操作结果:
3、 查看一行记录:
对应子方法:
主方法:
Console显示结果:
4、查看所有记录:
对应子方法:
主方法:
Console显示结果:
5、删除一行记录:
对应子方法:
主方法:
Console显示结果:
查看结果:
6、删除表:
对应子方法:
主方法:
Console显示结果:
查看结果:
完整代码:
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.KeyValue; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; 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 hbase{ public static Configuration conf= HBaseConfiguration.create(); //建表 public static void creatTable(String tableName, String familys) throws Exception { HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tableName)) { System.out.println("table already exists!"); } else { @SuppressWarnings("deprecation") HTableDescriptor tableDesc = new HTableDescriptor(tableName); tableDesc.addFamily(new HColumnDescriptor(familys)); admin.createTable(tableDesc); System.out.println("create table " + tableName + " ok."); } } //添加列记录 public static void addRecord (String tableName, String rowKey, String familys, String qualifier, String value) throws Exception{ HTable table = new HTable(conf, tableName); try { Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(familys),Bytes.toBytes(qualifier),Bytes.toBytes(value)); table.put(put); System.out.println("insert recored " + rowKey ); } catch (IOException e) { e.printStackTrace(); }finally{ table.close(); } } //查看一行记录 @SuppressWarnings("deprecation") public static void getRecord (String tableName, String rowKey)throws Exception{ HTable table = new HTable(conf, tableName); Get get=new Get(rowKey.getBytes()); Result rs=table.get(get); for(KeyValue kv:rs.raw()){ System.out.print(new String(kv.getRow()) + " " ); System.out.print(new String(kv.getFamily()) + ":" ); System.out.print(new String(kv.getQualifier()) + " " ); System.out.print(kv.getTimestamp() + " " ); System.out.println(new String(kv.getValue())); } } //查看所有记录 @SuppressWarnings("deprecation") public static void scanAllRecord (String tableName) { try{ HTable table = new HTable(conf, tableName); Scan s = new Scan(); ResultScanner ss = table.getScanner(s); for(Result r:ss){ for(KeyValue kv : r.raw()){ System.out.print(new String(kv.getRow()) + " "); System.out.print(new String(kv.getFamily()) + ":"); System.out.print(new String(kv.getQualifier()) + " "); System.out.print(kv.getTimestamp() + " "); System.out.println(new String(kv.getValue())); } } } catch (IOException e){ e.printStackTrace(); } } // 删除整行记录 @SuppressWarnings("unchecked") public static void deleteRecord (String tableName, String rowkey)throws Exception{ HTable table = new HTable(conf,tableName); try { @SuppressWarnings("rawtypes") List list=new ArrayList(); Delete del=new Delete(rowkey.getBytes()); list.add(del); table.delete(list); System.out.println("delete recored"+rowkey); }catch (IOException e){ e.printStackTrace(); }finally{ table.close(); } } //删除表 public static void deleteTable(String tableName)throws Exception { try{ HBaseAdmin admin = new HBaseAdmin(conf); admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println("delete table " + tableName+"ok!" ); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] agrs){ //表名 String tableName="xiaobaozi"; //列族 String familys="cf"; try{ //hbase.creatTable(tableName,familys); //行键,列族,列,列值 //hbase.addRecord(tableName, "AWM", familys, "laobing", "huaju"); //hbase.addRecord(tableName, "AK", familys, "huoqilin", "688"); //hbase.getRecord(tableName, "AWM"); //hbase.scanAllRecord(tableName); hbase.deleteRecord(tableName, "AWM"); //hbase.deleteTable(tableName); }catch(Exception e){ e.printStackTrace(); } } }
相关文章推荐
- eclipse下使用java api 进行hbase的常用的操作所需的jar包,以及如何查看java build path下的jar是否使用
- 使用Java API对HBase进行操作
- 使用Java API对HBase进行CRUD操作
- HBase在win7下eclipse中Java实现对表的操作报错解决
- 在eclipse操作hbase时,遇到的问题
- 用java API来操作HBase
- 在eclipse3.7中配置hadoop1.0.1插件 Hadoop 源码分析 使用java api操作Hadoop文件
- win7系统eclipse远程连接虚拟机中的centos6.5,对hbase进行操作
- hbase开发,hbase表操作及其java api实现
- 利用javaApI【eclipse】操作HBase时,出现异常的解决方案
- 在Eclipse中运行JAVA代码远程操作HBase的示例
- eclipse远程操作hbase
- [Hbase]eclipse下操作hbase
- JAVA API 操作HBASE(一)
- 1006-HBase操作实战(JAVA API模式)
- Eclipse 操作Hbase 代码一
- hbase开发,hbase表操作及其java api实现
- eclipse 连接并操作单机版Hbase
- HBase Java API使用操作例子
- 1006-HBase操作实战(JAVA API状态)