HBase的基本操作例子

package cn.itcast.hbase;

//import java.io.IOException;

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.TableName;
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.HTablePool;
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;
import org.junit.Before;
import org.junit.Test;

//import org.apache.hadoop.hbase.client.HTablePool;





public class HBaseDemo {

    //初始化
    private Configuration conf=new Configuration();
    @Before
    public void init(){
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "itcast04:2181,itcast05:2181,itcast06:2181");
        
    }
    //插入数据put
    @Test
    public void testPut() throws Exception{
//表对像
        HTable table=new HTable(conf, "peoples");
        //获取新建列族和表示
        Put put=new Put(Bytes.toBytes("kr001"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
        put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("300"));
        put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(3000000));
        
        //插入put
        table.put(put);
        table.close();
    }
    
    @Test
    public void testScan() throws IOException{
        HTable table = new HTable(conf, "peoples");
        Scan scan=new Scan(Bytes.toBytes("kr29990"),Bytes.toBytes("kr30000"));
        
        
        ResultScanner scanner = table.getScanner(scan);
        for(Result result:scanner){
            String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
            System.out.println(r);
        }
        table.close();
    } 
    
    
    
/*    @Test
    public void testPutAll() throws Exception{
        //HTablePool poo=

        HTable table=new HTable(conf, "peoples");
        List<Put>puts=new ArrayList<Put>(10000);
        for(int i=1; i<=1000000;i++)
        {
            Put put=new Put(Bytes.toBytes("kr"+i));
            put.add(Bytes.toBytes("info"),Bytes.toBytes("money"), Bytes.toBytes(""+i));
            puts.add(put);
            if(i%10000==0)
            {
                table.put(puts);
                puts=new ArrayList<Put>(10000);
                
            }
        }
        table.put(puts);
        table.close();
        
        
            
     * 删除
     * HTable table=new HTable(conf, "peoples");
        Delete del=new Delete(Bytes.toBytes("kr001"));
        del.deleteColumns(Bytes.toBytes("info"), Bytes.toBytes("name"));
        del.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("money"));
        
        
        table.delete(del);
        
    }
    */
    
    @Test
    public void testGet() throws IOException{
        HTable table=new HTable(conf, "peoples");
        Get get=new Get(Bytes.toBytes("kr999999"));
            
        Result result = table.get(get);
        String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
        System.out.println(r);
        table.close();
        
    }
    
    
    
    
    
/*    public static void main(String[] args) throws Exception {
        // TODO 自动生成的方法存根
        Configuration conf=HBaseConfiguration.create();
        //连接zookeeper
        conf.set("hbase.zookeeper.quorum", "itcast04:2181,itcast05:2181,itcast06:2181");
        //conf.set("ha.zookeeper.quorum", "192.168.80.204:2181,192.168.80.205:2181,192.168.80.206:2181");
        //获取表的对象hbaseasmin
        HBaseAdmin admin = new HBaseAdmin(conf);
        
        //定义表名
        HTableDescriptor htd=new HTableDescriptor(TableName.valueOf("peoples"));
        //定义表中列族1
        HColumnDescriptor hcd_info=new HColumnDescriptor("info");
        hcd_info.setMaxVersions(3);
    
        //第一列族2
        HColumnDescriptor hcd_data=new HColumnDescriptor("data");
        
        htd.addFamily(hcd_info);
        htd.addFamily(hcd_data);
        
        admin.createTable(htd);
        admin.close();
    }*/
    
    //删除
    @Test
    public void testDel() throws IOException
    {
        HTable table=new HTable(conf, "peoples");
        Delete del=new Delete(Bytes.toBytes("kr999999"));
        /*del.deleteColumns(Bytes.toBytes("info"), Bytes.toBytes("name"));
        del.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("money"));*/
        
        
        table.delete(del);
        table.close();
        
    }
    

}
原文地址:https://www.cnblogs.com/dandandeyoushangnan/p/4739971.html