大数据技术 Eclipse远程连接HBase
沉沙 2018-09-21 来源 : 阅读 1219 评论 0

摘要:本篇教程探讨了大数据技术 Eclipse远程连接HBase,希望阅读本篇文章以后大家有所收获,帮助大家对大数据技术的理解更加深入。

本篇教程探讨了大数据技术 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,实现了新建一张表,并随机向表里插入数据。
   

本文由职坐标整理发布,学习更多的大数据技术相关知识,请关注职坐标大技术云计算大技术技术频道!

本文由 @沉沙 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved