• HBase连接数据库(集群)


    一.使用java接口对hbase进行表的创建
    1.引入需要的jar包
    2.代码:

    public static void main(String[] args) throws Exception {
            //得到配置
            Configuration  conf= HBaseConfiguration.create();
            //连接zookeeper,就可以对hbase进行操作
            conf.set("hbase.zookeeper.quorum", "itcast04:2181,itcast05:2181,itcast06:2181");
           //使用java接口创建表
            HBaseAdmin admin=new HBaseAdmin(conf);
            //指定表名
            HTableDescriptor htd=new HTableDescriptor(TableName.valueOf("peoples"));
            //添加列族(info,data)
            HColumnDescriptor hcd_info=new HColumnDescriptor("info");
            hcd_info.setMaxVersions(3);
            HColumnDescriptor hcd_data=new HColumnDescriptor("data");
            htd.addFamily(hcd_info);
            htd.addFamily(hcd_data);
            //创建表
            admin.createTable(htd);
            //关闭
            admin.close();
        }

    二.使用java接口对hbase中的表进行crud操作

    package cn.itcast.hbase;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.hadoop.conf.Configuration;
    //import org.apache.hadoop.fs.shell.CopyCommands.Get;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HColumnDescriptor;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.MasterNotRunningException;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.ZooKeeperConnectionException;
    import org.apache.hadoop.hbase.client.HBaseAdmin;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hdfs.DFSClient.Conf;
    import org.junit.Before;
    import org.junit.Test;
    
    import org.apache.hadoop.hbase.util.Bytes;
    
    
    
    public class HBaseDemo {
    
        
        private Configuration conf=null;
        
        //在所有方法之间初始化
        @Before
        public void init(){
            conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "itcast04:2181,itcast05:2181,itcast06:2181");
        }
        
        //-------------一次插入一条数据------------------
        //插入数据
        @Test
        public void  testPut() throws Exception{
            //得到一个表对象
            HTable table =new HTable(conf, "peoples");
            //得到一个Put对象
            //将字符串转换为字符数组
            Put put=new Put(Bytes.toBytes("kr0001"));
            put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsanfeng"));
            put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("300"));
            put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(3000));
            //在表中放入put对象   
            table.put(put);
            table.close();
        }
        
        //插入100万条,速度会很快(服务器几秒,自己的电脑1分半)对比Oracle,mysql
        //-------------一次插入海量数据------------------
        @Test
        public void testPutAll() throws IOException{
            
            //HTablePool pool=new HTablePool(config,10);
            HTable table =new HTable(conf, "peoples");
            //得到list对象
            List<Put> puts=new ArrayList<Put>(10000);
    //      //第一种方式
    //        //将put放入list中
    //        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);
    //        }
    //        //在表中放入List
    //        table.put(puts);
    //        table.close();
            
            //第二种方式
        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);
            //每隔1w条放一次
            if(i%10000==0){
                table.put(puts);
                puts=new ArrayList<Put>(10000);//相当于清空
            }
        }
        table.put(puts);
        table.close();
            
            
        }
        //--------查询一个(不到1s)-----------------
        @Test
        public void testGet() throws IOException{
            HTable table =new HTable(conf, "peoples");
            Get get =new Get(Bytes.toBytes("kr999999"));
            //传get对象
            //返回result对象
            Result result=table.get(get);
            String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
            System.out.println(r);
            table.close();
        }
        
        //--------查询多个-----------------
        @Test
        public void testScan() throws IOException{
            HTable table =new HTable(conf, "peoples");
            //创建scan对象(按照字典顺序排[))
            Scan scan=new Scan(Bytes.toBytes("kr299990"), Bytes.toBytes("kr300000"));
            //返回结果集
            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();
        }
        //--------更新(put将老版本覆盖,查询最新的)-----------------
        
        //--------删除成功--------------------
        @Test
        public void testDel() throws IOException{
             HTable table =new HTable(conf, "peoples");
            //创建delete对象
             Delete delete = new Delete(Bytes.toBytes("kr999999"));
             table.delete(delete);
             table.close();
        }
        
        
        
        //---------创建表---------------------------
        public static void main(String[] args) throws Exception {
    
           //使用java接口创建表
            HBaseAdmin admin=new HBaseAdmin(conf);
            //指定表名
            HTableDescriptor htd=new HTableDescriptor(TableName.valueOf("peoples"));
            //添加列族(info,data)
            HColumnDescriptor hcd_info=new HColumnDescriptor("info");
            hcd_info.setMaxVersions(3);
            HColumnDescriptor hcd_data=new HColumnDescriptor("data");
            htd.addFamily(hcd_info);
            htd.addFamily(hcd_data);
            //创建表
            admin.createTable(htd);
            //关闭
            admin.close();
        }
    
    }
  • 相关阅读:
    Node.js、vue.js的使用
    windows配置环境变量
    http常见状态码及其解析
    AWS使用教程
    免费服务器集锦 免费服务器大全 免费使用服务器
    免费服务器AWS免费使用一年详细教程
    PHP实现RSA2加密
    dcoker安装redis
    ES,kibana通过nginx添加访问权限
    docker快速安装kibana
  • 原文地址:https://www.cnblogs.com/sunnyCx/p/7543712.html
Copyright © 2020-2023  润新知