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

Eclipse远程连接HBase

时间:2017-04-16 16:58:00      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:ast   family   程序   cto   代码   logs   val   blog   print   

在Eclipse下新建一个Map/Reduce项目,并将以下jar添加到Build path:

技术分享

程序代码:

package thathbase;

import java.io.IOException;
import java.util.Random;

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.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;


public class HelloHbase {
    static Configuration conf;
    static HBaseAdmin admin;
    static Connection conn;  
    static HTableDescriptor tableDescriptor;
    static HTable table;
    static Put putRow1;
    static Put putRow2;
    static Random rand =new Random(25);

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        init();
        createTable();
        insertTable();

    }
    
    private static void init() throws Exception {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.zookeeper.quorum", "master,slave03,slave04");
        conf.set("hbase.master", "master:60000");                 
        conn = ConnectionFactory.createConnection(conf);
        admin = (HBaseAdmin) conn.getAdmin();
    }
    
    @SuppressWarnings("deprecation")
    private static void createTable() throws Exception{
        if (!admin.tableExists("scores")){
            tableDescriptor = new HTableDescriptor("scores".getBytes());
            tableDescriptor.addFamily(new HColumnDescriptor("fam1"));
            admin.createTable(tableDescriptor);    
        }
        else{
            System.out.println("Table already exists!");
        }
        table = new HTable(conf, "scores");                
    }
    
    @SuppressWarnings("deprecation")
    private static void insertTable() throws IOException{
        putRow1 = new Put("row1".getBytes());
        putRow2 = new Put("row2".getBytes());
        
        for (int i =0; i<1000; i++){
            if (rand1() == 1){
                putRow1.add("fam1".getBytes(),"col1".getBytes(),String.valueOf(rand2()).getBytes());
                table.put(putRow1);
                System.out.println(i + ":Insert into col1.");
            }
            else{
                putRow2.add("fam1".getBytes(),"col2".getBytes(),String.valueOf(rand2()).getBytes());
                table.put(putRow2);
                System.out.println(i + ":Insert into col2.");
            }
        }
    }
    
    private static int rand1(){
        int r = rand.nextInt(100);
        if (r < 50)
            return 1;
        else
            return 2;
    }
    
    private static int rand2(){
        int r = rand.nextInt(10000);
        return r;
    }
}

以上程序调用HBAse的API,实现了新建一张表,并随机向表里插入数据。

Eclipse远程连接HBase

标签:ast   family   程序   cto   代码   logs   val   blog   print   

原文地址:http://www.cnblogs.com/mstk/p/6719164.html

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