随着大数据的兴起,我也随大流时尚一把。
做功课的时候一搜索Hbase涉及到一堆别的知识点,Hadoop、zookeeper...。我就想,我只是要入门,如果一次性学这么多,怎么消化得了,而且关于Hbase的使用一般是linux系统下。平时开发用的基本是windows系统,感觉学习这门技术任重而道远。不管路途多遥远,也不能放弃,于是不停百度,终于把代码给实现了,把这曲折的过程记录一下,能记住的先记录,不能记住的过去就过去了。因为中间调试代码的时候出了很多报错,所以一些弱智的问题就不汇报了。
首先上官网(http://hbase.apache.org/)上下载一个最新的安装包
hbase-1.2.6-bin.tar.gz 然后在cygwin下用tar -zxvf hbase-1.2.6-bin.tar.gz命令解压到安装目录下(E:\ProgramFiles\hbase-1.2.6),打开conf文件夹下的hbase-site.xml文件配置数据库的物理存储路径(H:/Database)。
再到bin文件夹下运行start-hbase.cmd,没有报错说明hbase服务正常启动。然后用eclipse创建了一个maven项目,上网上找了代码,测试数据库表的创建,增加,读取等基本功能。最终在H:\Database\data\default文件夹下看到了创建的表,不过数据看不了,估计要用命令查看。把网上的数据库操作代码也贴一下来记录。
package HbaseDemo.hbaseDemo; 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 App { //声明静态配置 HBaseConfiguration static Configuration cfg=HBaseConfiguration.create(); //创建一张表,通过HBaseAdmin HTableDescriptor来创建 @SuppressWarnings("resource") public static void creat(String tablename,String columnFamily) throws Exception { @SuppressWarnings("deprecation") HBaseAdmin admin = new HBaseAdmin(cfg); if (admin.tableExists(tablename)) { System.out.println("table Exists!"); System.exit(0); } else{ @SuppressWarnings("deprecation") 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"; System.out.println("start table:"+tablename); try { App.creat(tablename, columnFamily); App.put(tablename, "row1", columnFamily, "cl1", "data"); App.get(tablename, "row1"); App.scan(tablename); /* if(true==HBaseTestCase.delete(tablename)) System.out.println("Delete table:"+tablename+"success!"); */ System.out.println("creat table:"+tablename+"success!"); } catch (Exception e) { e.printStackTrace(); } } }