码迷,mamicode.com
首页 > 系统相关 > 详细

eclipse开发hbase程序

时间:2017-02-09 13:39:56      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:获取   apache   admin   catch   str   pack   trace   success   print   

首先安装好hbase

创建一个java project引入hbase中lib的jar包

然后创建一个class

package sun_test_pig;

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 HBaseTest {           

    static Configuration cfg=HBaseConfiguration.create();

    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!");
        }
    }

    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 {                     
            	HBaseTest.creat(tablename, columnFamily);
            	HBaseTest.put(tablename, "row1", columnFamily, "cl1", "data");
            	HBaseTest.get(tablename, "row1");
            	HBaseTest.scan(tablename);
            if(true==HBaseTest.delete(tablename))
                    System.out.println("Delete table:"+tablename+"success!");
       
        }
        catch (Exception e) {
            e.printStackTrace();
        }    
}
}

 

eclipse开发hbase程序

标签:获取   apache   admin   catch   str   pack   trace   success   print   

原文地址:http://www.cnblogs.com/sunyaxue/p/6381410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!