首页 技术 正文
技术 2022年11月9日
0 收藏 846 点赞 4,525 浏览 2847 个字

java api操作 导入开发包 将hbase安装包中lib下包导入java项目  创建表  Configuration conf = HBaseConfiguration.create(); conf.set(“hbase.zookeeper.quorum”, “CentOS01:2181,CentOS02:2181,CentOS03:2181”);  HBaseAdmin admin = new HBaseAdmin(conf);  HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(“tabe”)); HColumnDescriptor hcd_fam1 = new HColumnDescriptor(“fam1”); hcd_fam1.setMaxVersions(3); HColumnDescriptor hcd_fam2 = new HColumnDescriptor(“fam2”); htd.addFamily(hcd_fam1); htd.addFamily(hcd_fam2);  admin.createTable(htd);  admin.close();   插入数据 Configuration conf = HBaseConfiguration.create(); conf.set(“hbase.zookeeper.quorum”,”CentOS01:2181,CentOS02:2181,CentOS03:2181″);  HTable table = new HTable(conf,”tabe”); Put put = new Put(Bytes.toBytes(“row1”)); put.add(Bytes.toBytes(“fam1”),Bytes.toBytes(“col1”),Bytes.toBytes(“val1”)); put.add(Bytes.toBytes(“fam1”),Bytes.toBytes(“col2”),Bytes.toBytes(“val2”)); put.add(Bytes.toBytes(“fam2”),Bytes.toBytes(“col3”),Bytes.toBytes(“val3″)); table.put(put);  table.close();    **javaapi操作hbase时,入口类为HTable,此对象创建时需要扫描.META表,以及其他操作,这非常耗时,所以,应该将该对象设置为单例,复用该对象,如果需要多个HTable对象,应该使用HTable Pool,通过对象池复用对象。 HTablePool pool = new HTablePool(conf,10);//不知道为什么过时了? **hbase所有修改数据的操作都保证了行级别的原子性,  试验:一次插入100万条数据 HTable table = new HTable(conf,”tabx”); List<Put> puts = new ArrayList<Put>(); for(int i=1;i<=1000000;i++){ Put put = new Put(Bytes.toBytes(“row”+i)); put.add(Bytes.toBytes(“fam1”),Bytes.toBytes(“col1”),Bytes.toBytes(“val”+i)) puts.add(put);  if(i % 10000 == 0){ table.put(puts); puts = new ArrayList<Put>(); } } table.put(puts); table.close();   获取数据 Configuration conf = HBaseConfiguration.create(); conf.set(“hbase.zookeeper.quorum”,”CentOS01:2181,CentOS02:2181,CentOS03:2181″);  HTable table = new HTable(conf,”tabe”); Get get = new Get(Bytes.toBytes(“row1”)); Result result = table.get(get); byte [] bs = result.getValue(Bytes.toBytes(“fam1”),Bytes.toBytes(“col1”)); String str = Bytes.toString(bs); System.out.println(str);  table.close();   获取数据集 Configuration conf = HBaseConfiguration.create(); conf.set(“hbase.zookeeper.quorum”,”CentOS01:2181,CentOS02:2181,CentOS03:2181″);  HTable table = new HTable(conf,”tabe”); Scan scan = new Scan(Bytes.toBytes(“row1”)); ResultScanner scanner = table.getScanner(scan); Iterator it = scanner.iterator(); while(it.hasNext()){ Result result = (Result) it.next(); byte [] bs = result.getValue(Bytes.toBytes(“fam1”),Bytes.toBytes(“col1”)); String str = Bytes.toString(bs); System.out.println(str); } table.close();   删除数据 Configuration conf = HBaseConfiguration.create(); conf.set(“hbase.zookeeper.quorum”,”CentOS01:2181,CentOS02:2181,CentOS03:2181″);  HTable table = new HTable(conf,”tabe”); Delete delete = new Delete(Bytes.toBytes(“row1”)); table.delete(delete); table.close();   删除表  //1.创建配置对象 HBaseConfiguration conf = new HBaseConfiguration(); conf.set(“hbase.zookeeper.quorum”, “CentOS01”); //2.创建HBaseAdmin对象 HBaseAdmin admin = new HBaseAdmin(conf); //3.删除表 admin.disableTable(Bytes.toBytes(“tab1”)); admin.deleteTable(Bytes.toBytes(“tab1”)); //4.关闭连接 admin.close();

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903