标签:des style blog http color io os 使用 ar
Eclipse,HBase版本目前没有发现需要特别指定
1:从HBase集群中复制一份Hbase部署文件,放置在开发端某一目录下(如在/app/hadoop/hbase096目录下)。
2:在eclipse里新建一个java项目HBase,然后选择项目属性,在Libraries->Add External JARs...,然后选择/app/hadoop/hbase096/lib下相关的JAR包,如果只是测试用的话,就简单一点,将所有的JAR选上。4:在HBase项目中增加一个chapter12的package,然后增加一个HBaseTestCase的class,然后将陆嘉恒老师的《Hadoop实战第2版》12章的代码复制进去,做适当的修改,代码如下:
package chapter12; import java.io.IOException; 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.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 HBaseTestCase { //声明静态配置 HBaseConfiguration static Configuration cfg=HBaseConfiguration.create(); //创建一张表,通过HBaseAdmin HTableDescriptor来创建 public static void creat(String tablename,String columnFamily) 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); tableDesc.addFamily(new HColumnDescriptor(columnFamily)); admin.createTable(tableDesc); System.out.println("create table success!"); } } //添加一条数据,通过HTable Put为已经存在的表来添加数据 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)); p1.add(Bytes.toBytes(columnFamily), 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 IOException{ HTable table=new HTable(cfg,tablename); Get g=new Get(Bytes.toBytes(row)); Result result=table.get(g); System.out.println("Get: "+result); } //显示所有数据,通过HTable Scan来获取已有表的信息 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); } } 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 { HBaseTestCase.creat(tablename, columnFamily); HBaseTestCase.put(tablename, "row1", columnFamily, "cl1", "data"); HBaseTestCase.get(tablename, "row1"); HBaseTestCase.scan(tablename); /* if(true==HBaseTestCase.delete(tablename)) System.out.println("Delete table:"+tablename+"success!"); */ } catch (Exception e) { e.printStackTrace(); } } }
期间自己遇到点问题:
1.引入jar包,有两个包没有读取权限,导致项目出现红色感叹号
2.因为我是虚拟机,每次都要重新启动hadoop和HBase,所以也不要忘了设置hadoop为非安全模式
HBase学习(十四)LINUX下用Eclipse构建HBase开发环境
标签:des style blog http color io os 使用 ar
原文地址:http://blog.csdn.net/lifuxiangcaohui/article/details/40048635