ITeye博客

HBASE的java api简介

wuai · 2015-08-06 09:47 · HBase · 开源软件 · Markdown

一:创建表 //获取配置 Configuration conf = new Configuration(); //填写配置信息 conf.set("hbase.zookeeper.quorum", "it04:2181,it05:2181,it06:2181"); //获得操作hbase的admin对象 HBaseAdmin admin = new HBaseAdmin(conf); //获取HTableDescriptor对象,用于表的描述,如表名,列族等等 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("person")); //通过HColumnDescriptor获取对列族、列标示符进行标注的对象 HColumnDescriptor hcd_info = new HColumnDescriptor("info"); HColumnDescriptor hcd_data = new HColumnDescriptor("data"); //对列族进行设置 hcd_info.setMaxVersions(3); //把整理好的HColumnDescriptor对象信息添加的HTableDescriptor对象上 desc.addFamily(hcd_info); desc.addFamily(hcd_data); //使用admin对象创建表create 参数desc为HTableDescriptor对象 admin.createTable(desc); //最后千万不要忘了关闭,想想jdbc admin.close();

二:HBASE操作表之put增加数据:
	//获取配置
	Configuration conf = new Configuration();
	//填写配置信息
	conf.set("hbase.zookeeper.quorum", "it04:2181,it05:2181,it06:2181");
	//获得要操作的表,参数是配置信息,和要获取的表的表名
	HTable table = new HTable(conf, "person");
	//添加数据的就需要获取Put对象,通过Put对象添加数据,参数为字节数组
	Put put = new Put(Bytes.toBytes("zhaoliu"));
	//第Put对象添加数据,第一个参数:列族 。第二个参数:列名。第三个参数:值  ,此外还有一个参数ts:timestamp时间戳
	put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhaoliu"));
	put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(40));
	put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(10));
	//表对象开始put操作
	table.put(put);
	//不要忘了关闭
	table.close();
	
三、HBASE操作表之一次插入100万条
	public void testPutAll() throws IOException{
//		HTablePool poll = new HTablePool(conf, 10);
		HTable table = new HTable(conf, "person");
		List<Put> puts = new ArrayList<Put>();
		for(int i=0;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);
			if(i%10000==0){
				table.put(puts);
				puts = new ArrayList<Put>();
			}
		}
		table.put(puts);
		table.close();
	}
四、HBASE 操作之get取数据:
	public void testGet() throws IOException{
		HTable table = new HTable(conf,"person");
		Get get = new Get(Bytes.toBytes("zhangsan"));
		Result result = table.get(get);
		String res = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")));
		System.out.println(res);
		table.close();
	}
五、HBASE操作之scan扫描表:  <注>:注意返回结果
	/**
	 * scan表
	 * 返回结果:
	 * 		299990、299991、299992、299993、299994、299995、299996、299997、299998、299999
	 * 		3、30、300、3000、30000
	 * @throws IOException 
	 */
	@Test
	public void testScan() throws IOException{
		HTable table = new HTable(conf,"p erson");
		Scan scan = new Scan(Bytes.toBytes("kr299990"),Bytes.toBytes("kr300000"));
		ResultScanner scanner = table.getScanner(scan);
		for(Result result:scanner){
			String res = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")));
			System.out.println(res);
		}
		table.close();
	}
六、HBASE操作之delete删除数据:
	public void testDelete() throws IOException{
		HTable table = new HTable(conf,"person");
		Delete delete = new Delete(Bytes.toBytes("lisi"));
//		delete.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
		delete.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("money"));
		table.delete(delete);
	}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。