【6-6】HBASE的Java接口(2)
2016-02-25 21:17
477 查看
使用Java接口实现增删改查。
在这里面我们要实现多个方法,因此我们使用jUnit test比较好
在这里面我们要实现多个方法,因此我们使用jUnit test比较好
package cn.itcast.hbase; import java.io.IOException; import java.io.InterruptedIOException; 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.client.Delete; import org.apache.hadoop.hbase.client.Get; 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.RetriesExhaustedWithDetailsException; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Before; import org.junit.Test; public class HBaseDemo { private Configuration conf = null; //这个方法在我所有的junittest方法之前执行,应该加before标签【查1:junittest】 @Before public void init(){ conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "itcast04:21"); } //配置已经装好了,现在先来一个什么方法呢 插入数据 @Test public void testPut() throws Exception{ //我得先得到一个表对象table,才能往里插入数据 HTable table = new HTable(conf, "peoples");//我现在得到了想操作的这张表,接下来插入操作, Put put = new Put(Bytes.toBytes("kr0001")); put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhagnsf")); //以前只能插入一个属性,现在,我想插多少插多少,复制粘贴【查2:bytes.toBytes】 put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("35")); put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(60000)); table.put(put);//这里面put的是一个put对象,因此我们给new一个 table.close();//好借好还 //接下来插入100w条,500w条看看时间。【如果在oracle或者MySQL会很费时间】 } @Test public void testPutAll() throws Exception, IOException{ //HTablePool HTablePool = new HTablePool(config, maxSize)这个不支持了,还是用HTable吧 HTable table = new HTable(conf, "peoples"); List<Put> puts = new ArrayList<Put>(); for(int i =1;i<=10000000;i++){ Put put = new Put(Bytes.toBytes("kr"+i)); put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes("zhagnsf")); puts.add(put);// } table.put(puts); table.close(); //用这个一个一个的并不好,现在 } @Test public void testGet() throws Exception{ HTable table = new HTable(conf, "peoples");//得到一个table对象 Get get = new Get(Bytes.toBytes("kr99999")); table.get(get);//调用table对象的一个方法,它返回的是一个结果集 Result result = table.get(get); String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money"))); //getValue返回的是一个byte数组,如何转换成int或string类型?工具类,bytes.toString System.out.println(r);//打印一下 table.close(); } //查多个 @Test public void testScan(){ HTable table = new HTable(conf,"peoples"); Scan scan = new Scan(Bytes.toBytes("kr88888"), Bytes.toBytes("kr300000)); table.getScanner(scan);//它要什么我new什么,要scan我就在上面new一个scan //它返回的是一个结果集,resultscanner //它是怎么一下子弄出来resultscanner的? 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); } } //删除 @Test public void testDel() throws Exception{ HTable table = new HTable(conf,"people"); Delete delete = new Delete(Bytes.toBytes("kr9999")); table.delete(delete); table.close(); } public static void main(String[] args) { // TODO 自动生成的方法存根 } }
相关文章推荐
- java map的四种遍历
- java知识结构树
- Java中播放声音实例
- Java中的泛型方法
- Java 中基本类型和字符串之间的转换
- Java 递归与非递归实现快速排序
- Mac OSX之java环境的搭建
- myeclipse中的classpath
- Rar Java Zip
- JAVA_多态性演示a____自动向上转型、向下转型、检查对象是否为某个类型(instanceof)
- JAVA_多态性演示a____自动向上转型、向下转型、检查对象是否为某个类型(instanceof)
- springmvc 传入controller乱码
- java线程(1)--概念基础
- Java并发-类库新组件 - CyclicBarrier 理解
- spring学习小结3——bean之间的继承,依赖关系
- Java中的NIO
- java操作xml的一个小例子
- Myeclipse如何自动创建hibernate 转载
- java中Collection/Collections;List/Set/Map;ArrayList/Vector/LinkedList;HashSet/HashMap/TreeSet/TreeMap
- spring学习小结4——bean 的作用域 和引入外部属性文件