HBase基本操作

HBase Java API 操作

Tips:

其实每一个操作都可以简化为:

1、配置并连接数据库

2、编写 Java API 的 HBase 的操作

3、使用权限 执行操作

要对一个Hbase数据库进行操作的话,首先我们要进行一系列准备操作

//创建HBase配置对象
Configuration conf=HBaseConfiguration.create();
//指定ZooKeeper集群地址
conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
//创建连接对象Connection
Connection conn=ConnectionFactory.createConnection(conf);
//得到数据库管理员对象
Admin admin=conn.getAdmin();

创建表

我们需要知道的是,一个表的结构由tableName(表名)、rowkey(行键)、columnFamily(列族)构成,timestamp(时间戳)。

在这里插入图片描述

创建一个表需要设置的 只有表名和列族。

		//创建HBase配置对象
		Configuration conf=HBaseConfiguration.create();
		//指定ZooKeeper集群地址
		conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
		//创建连接对象Connection
		Connection conn=ConnectionFactory.createConnection(conf);
		//得到数据库管理员对象
		Admin admin=conn.getAdmin();
		//创建表描述,并指定表名
		TableName tableName=TableName.valueOf("table2");
		HTableDescriptor desc=new HTableDescriptor(tableName);
		//创建列族描述
		HColumnDescriptor family=new HColumnDescriptor("familyName1");
		HColumnDescriptor family2=new HColumnDescriptor("familyName2");
		//指定列族
		desc.addFamily(family);
		desc.addFamily(family2);
		//创建表
		admin.createTable(desc);

添加数据

  		//创建HBase配置对象
		Configuration conf=HBaseConfiguration.create();
		//指定ZooKeeper集群地址
		conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
  		//创建数据库连接对象Connection
  		Connection conn=ConnectionFactory.createConnection(conf);
  		//Table负责与记录相关的操作,如增删改查等
  		TableName tableName=TableName.valueOf("table1");
  		Table table=conn.getTable(tableName);
        Put put = new Put(Bytes.toBytes("row1"));// 设置rowkey
        //添加列数据,指定列族、列名与列值
        put.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("name"),
        		Bytes.toBytes("xiaoming24"));
        put.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("age"),
        		Bytes.toBytes("33"));
        put.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("address"),
        		Bytes.toBytes("beijing"));
        
        Put put2 = new Put(Bytes.toBytes("row8"));// 设置rowkey
        //添加列数据,指定列族、列名与列值
        put2.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("name"),
        		Bytes.toBytes("xiaoming23333"));
        put2.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("age"),
        		Bytes.toBytes("30"));
        put2.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("address"),
        		Bytes.toBytes("beijing2"));
        
        Put put3 = new Put(Bytes.toBytes("row9"));// 设置rowkey
        //添加列数据,指定列族、列名与列值
        put3.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("name"),
                Bytes.toBytes("zsh"));
        put3.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("age"),
        		Bytes.toBytes("31"));
        put3.addColumn(Bytes.toBytes("familyName1"), Bytes.toBytes("address"),
        		Bytes.toBytes("beijing3"));
        
        //执行添加数据
        table.put(put);
        table.put(put2);
        table.put(put3);
        //释放资源
        table.close();

从数据库获取数据

		//创建HBase配置对象
		Configuration conf=HBaseConfiguration.create();
		//指定ZooKeeper集群地址
		conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
        //获得数据库连接
        Connection conn=ConnectionFactory.createConnection(conf);
	   //获取Table对象,指定查询表名,Table负责与记录相关的操作,如增删改查等
        Table table = conn.getTable(TableName.valueOf("table1"));
        //创建Get对象,根据rowkey查询,rowkey=row1  
        Get get = new Get("row1".getBytes());
        //查询数据,取得结果集
        Result r = table.get(get); 
        //循环输出每个单元格的数据
        for (Cell cell : r.rawCells()) {  
        	//取得当前单元格所属的列族名称
        	String family=new String(CellUtil.cloneFamily(cell));
        	//取得当前单元格所属的列名称
        	String qualifier=new String(CellUtil.cloneQualifier(cell));
        	//取得当前单元格的列值
        	String value=new String(CellUtil.cloneValue(cell));
        	//输出结果
         System.out.println("列:" + family+":"+qualifier + "--值:" + value);  
        }  

获取表的描述

        //创建HBase配置对象
        Configuration conf=HBaseConfiguration.create();
        //指定ZooKeeper集群地址
        conf.set("hbase.zookeeper.quorum", "192.168.153.130:2181,192.168.153.131:2181,192.168.153.132:2181");
        //创建连接对象Connection
        Connection conn=ConnectionFactory.createConnection(conf);
        //得到数据库管理员对象
        Admin admin=conn.getAdmin();
        TableName tableName=TableName.valueOf("table2");
        HTableDescriptor desc = admin.getTableDescriptor(tableName);

        Object[] arr = desc.getFamilies().toArray();
        for (Object o : arr) {
            System.out.println(o);
        }

SCAN 扫描

        //连接数据库
        Configuration conf=new Configuration();
        conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        //读取表
        Table mytable = connection.getTable(TableName.valueOf("table1"));

        //全表扫描
        //从第一个到最后一个 stop设置为空
        Scan scan=new Scan("row1".getBytes(),"".getBytes());
        //scan.setStartRow("row1".getBytes());
		//scan.setStopRow("".getBytes());
        //区间扫描
        //Scan scan=new Scan("row1".getBytes(),"row9".getBytes());
		//scan.setStartRow("row1".getBytes());
		//scan.setStopRow("row9".getBytes());

        ResultScanner scanner = mytable.getScanner(scan);
        //result  是与一行数据(有多个列族,多个列)
        Iterator<Result> iterator =  scanner.iterator();
        System.out.println();
        while(iterator.hasNext()){
            Result result = iterator.next();
            //获取结果 返回的是cell 用cellutils将其变为字节数组 然后再转为string
            String name = new String(CellUtil.cloneValue(result.getColumnLatestCell("familyName1".getBytes(),
            "name".getBytes())));
            String address = new String(CellUtil.cloneValue(result.getColumnLatestCell("familyName1".getBytes(), 	               "address".getBytes())));
            String age = new String(CellUtil.cloneValue(result.getColumnLatestCell("familyName1".getBytes(),                       "age".getBytes())));
            System.out.println(name+", "+age+", "+address);
        }
        //关闭连接
        connection.close();

Delete

		//创建HBase配置对象
		Configuration conf=HBaseConfiguration.create();
		//指定ZooKeeper集群地址
		conf.set("hbase.zookeeper.quorum", "centos01:2181,centos02:2181,centos03:2181");
		//获得数据库连接
        Connection conn=ConnectionFactory.createConnection(conf);
	    //获取Table对象,指定表名,Table负责与记录相关的操作,如增删改查等
	    TableName tableName=TableName.valueOf("t2");
	    Table table=conn.getTable(tableName);
	    //创建删除对象Delete,根据rowkey删除一整条
	    Delete delete=new Delete(Bytes.toBytes("row1"));
	    table.delete(delete);
        //释放资源
        table.close();

	    Table table=conn.getTable(tableName);
	    //创建删除对象Delete,根据rowkey删除一整条
	    Delete delete=new Delete(Bytes.toBytes("row1"));
	    table.delete(delete);
        //释放资源
        table.close();
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hbase 基本操作类 static { //此处可以使用hbase的配置文件,也可以通过代码来实例化hbase连接 /* * Configuration HBASE_CONFIG = new Configuration(); * HBASE_CONFIG.set("hbase.zookeeper.quorum", "10.229.171.45"); * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181"); * HBASE_CONFIG.set("hbase.zookeeper.quorum","10.233.92.85,10.233.92.86,10.233.92.88"); * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181"); * HBASE_CONFIG.set("zookeeper.znode.parent", "/hbase-unsecure"); conf = HBaseConfiguration.create(HBASE_CONFIG); */ conf = HBaseConfiguration.create(); if("kerberos".equals(conf.get("hbase.security.authentication"))){ try { //设置hadoop.security.authentication为kerberos conf.set("hadoop.security.authentication", "kerberos"); //获取kerberos配置文件路径(krb为kerberos配置文件) String krbStr=Thread.currentThread().getContextClassLoader().getResource("krb").getFile(); //获取用户票据hezhong路径(hezhong为给合众分配的用户配置文件) String keyStr=Thread.currentThread().getContextClassLoader().getResource("jdzy").getFile(); //初始化配置文件 System.setProperty("java.security.krb5.conf", krbStr); //使用用户hezhong登录 UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromKeytab("jdzy/f04345e6-70c1-448a-9bbb-4ac6b4c0109b@POLICE.COM", keyStr); } catch (IOException e) { e.printStackTrace(); } } tablePool = new HTablePool(conf, poolsize); logger.debug("create hbase connection success"); // System.out.println("create hbase connection success"); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值