HBase的基本操作例子
2015-08-18 17:12
399 查看
package cn.itcast.hbase; //import java.io.IOException; import java.io.IOException; import java.util.ArrayList; import java.util.List; 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.KeyValue; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.HTablePool; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Before; import org.junit.Test; //import org.apache.hadoop.hbase.client.HTablePool; public class HBaseDemo { //初始化 private Configuration conf=new Configuration(); @Before public void init(){ conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "itcast04:2181,itcast05:2181,itcast06:2181"); } //插入数据put @Test public void testPut() throws Exception{ //表对像 HTable table=new HTable(conf, "peoples"); //获取新建列族和表示 Put put=new Put(Bytes.toBytes("kr001")); put.add(Bytes.toBytes("info"),Bytes.toBytes("name"), Bytes.toBytes("zhangsan")); put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("300")); put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(3000000)); //插入put table.put(put); table.close(); } @Test public void testScan() throws IOException{ HTable table = new HTable(conf, "peoples"); Scan scan=new Scan(Bytes.toBytes("kr29990"),Bytes.toBytes("kr30000")); 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(); } /* @Test public void testPutAll() throws Exception{ //HTablePool poo= HTable table=new HTable(conf, "peoples"); List<Put>puts=new ArrayList<Put>(10000); 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); if(i%10000==0) { table.put(puts); puts=new ArrayList<Put>(10000); } } table.put(puts); table.close(); * 删除 * HTable table=new HTable(conf, "peoples"); Delete del=new Delete(Bytes.toBytes("kr001")); del.deleteColumns(Bytes.toBytes("info"), Bytes.toBytes("name")); del.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("money")); table.delete(del); } */ @Test public void testGet() throws IOException{ HTable table=new HTable(conf, "peoples"); Get get=new Get(Bytes.toBytes("kr999999")); Result result = table.get(get); String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money"))); System.out.println(r); table.close(); } /* public static void main(String[] args) throws Exception { // TODO 自动生成的方法存根 Configuration conf=HBaseConfiguration.create(); //连接zookeeper conf.set("hbase.zookeeper.quorum", "itcast04:2181,itcast05:2181,itcast06:2181"); //conf.set("ha.zookeeper.quorum", "192.168.80.204:2181,192.168.80.205:2181,192.168.80.206:2181"); //获取表的对象hbaseasmin HBaseAdmin admin = new HBaseAdmin(conf); //定义表名 HTableDescriptor htd=new HTableDescriptor(TableName.valueOf("peoples")); //定义表中列族1 HColumnDescriptor hcd_info=new HColumnDescriptor("info"); hcd_info.setMaxVersions(3); //第一列族2 HColumnDescriptor hcd_data=new HColumnDescriptor("data"); htd.addFamily(hcd_info); htd.addFamily(hcd_data); admin.createTable(htd); admin.close(); }*/ //删除 @Test public void testDel() throws IOException { HTable table=new HTable(conf, "peoples"); Delete del=new Delete(Bytes.toBytes("kr999999")); /*del.deleteColumns(Bytes.toBytes("info"), Bytes.toBytes("name")); del.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("money"));*/ table.delete(del); table.close(); } }
相关文章推荐
- 23个MySQL常用查询语句
- C#使用IComparer自定义List类实现排序的方法
- hdu 1238 字符串的简单运用
- 2013年海康威视校园招聘笔试题
- 有关期刊投稿
- (转)linux下和云端通讯的例程, ubuntu和openwrt实验成功(一)
- ACM 错排
- BA-siemens-PXM液晶面板
- 前端------一个模糊不清的岗位
- CentOS 6.5 安装部署zabbix(Server端篇)
- iOS拨打电话的三种方法
- MethodInterceptor实战
- POJ 3620 Avoid The Lakes(DFS)
- 学习正则表达式:Matcher类
- 日期字符串相互转换
- Java:java+内存分配及变量存储位置的区别
- Effective C++ 条款6 若不想使用编译器自动生成的函数,就该明确拒绝
- ArcGIS教程:了解水系
- puppet进阶指南——user资源详解
- 杭电 HDU 1002 A + B Problem II Java 大数的初级用法