Eclipse远程连接HBase

Posted 代码空间

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Eclipse远程连接HBase相关的知识,希望对你有一定的参考价值。

在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{
        admin.disableTable("scores");
        admin.deleteTable("scores");
        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++){
            String row = String.valueOf(i);
            if (rand1() == 1){
                putRow1 = new Put(row.getBytes());
                putRow1.add("fam1".getBytes(),"col1".getBytes(),String.valueOf(rand2()).getBytes());
                table.put(putRow1);
                System.out.println(i + ":Insert into col1.");
            }
            else{
                putRow2 = new Put(row.getBytes());
                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的主要内容,如果未能解决你的问题,请参考以下文章

eclipse+hbase开发环境部署

eclipse远程调用hbase遇到问题

windows下用Eclipse连接大数据环境得hbase

Eclipse连接HBase 报错:org.apache.hadoop.hbase.PleaseHoldException: Master is initializing

hbase单机模式下,使用java API远程连接hbase的问题。

如何从不同计算机的配置单元远程/连接 hbase 表