Some features
- simple jdo(used reflection)
- HTable pool ( I already know HTablePool in habse )
- simple query classes ( insert,delete,update,select)
- table sequence generator
dependency library
hbase 0.90 libraries. (http://hbase.apache.org)
commons-beanutils.jar (http://commons.apache.org/)
commons-pool-1.5.5.jar (http://commons.apache.org/)
transactional-tableindexed for 0.90 (https://github.com/hbase-trx/hbase-transactional-tableindexed)
= Download =
Examples
1 package com.apache.hadoop.hbase.client.jdo.examples;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.InputStream;
6 import java.util.Hashtable;
7
8 import org.apache.hadoop.fs.Path;
9 import org.apache.hadoop.hbase.client.tableindexed.IndexedTable;
10
11 import com.apache.hadoop.hbase.client.jdo.AbstractHBaseDBO;
12 import com.apache.hadoop.hbase.client.jdo.HBaseBigFile;
13 import com.apache.hadoop.hbase.client.jdo.HBaseDBOImpl;
14 import com.apache.hadoop.hbase.client.jdo.query.DeleteQuery;
15 import com.apache.hadoop.hbase.client.jdo.query.HBaseOrder;
16 import com.apache.hadoop.hbase.client.jdo.query.HBaseParam;
17 import com.apache.hadoop.hbase.client.jdo.query.InsertQuery;
18 import com.apache.hadoop.hbase.client.jdo.query.QSearch;
19 import com.apache.hadoop.hbase.client.jdo.query.SelectQuery;
20 import com.apache.hadoop.hbase.client.jdo.query.UpdateQuery;
21
22 /**
23 * Hbase JDO Example.
24 *
25 * dependency library.
26 * - commons-beanutils.jar
27 * - commons-pool-1.5.5.jar
28 * - hbase0.90.0-transactionl.jar
29 *
30 * you can expand Delete,Select,Update,Insert Query classes.
31 * @author ncanis
32 *
33 */
34 public class HBaseExample {
35 public static void main(String[] args) throws Exception {
36 AbstractHBaseDBO dbo = new HBaseDBOImpl();
37
38 //*drop if table is already exist.*
39 if(dbo.isTableExist("user")){
40 dbo.deleteTable("user");
41 }
42
43 //*create table*
44 dbo.createTableIfNotExist("user",HBaseOrder.DESC,"account");
45 //dbo.createTableIfNotExist("user",HBaseOrder.ASC,"account");
46
47 //create index.
48 String[] cols={"id","name"};
49 dbo.addIndexExistingTable("user","account",cols);
50
51 //insert
52 InsertQuery insert = dbo.createInsertQuery("user");
53 UserBean bean = new UserBean();
54 bean.setFamily("account");
55 bean.setAge(20);
56 bean.setEmail("ncanis@gmail.com");
57 bean.setId("ncanis");
58 bean.setName("ncanis");
59 bean.setPassword("1111");
60 insert.insert(bean);
61
62 //select 1 row
63 SelectQuery select = dbo.createSelectQuery("user");
64 UserBean resultBean = (UserBean)select.select(bean.getRow(),UserBean.class);
65
66 // select column value.
67 String value = (String)select.selectColumn(bean.getRow(),"account","id",String.class);
68
69 // search with option (QSearch has EQUAL, NOT_EQUAL, LIKE)
70 // select id,password,name,email from account where id='ncanis' limit startRow,20
71 HBaseParam param = new HBaseParam();
72 param.setPage(bean.getRow(),20);
73 param.addColumn("id","password","name","email");
74 param.addSearchOption("id","ncanis",QSearch.EQUAL);
75 select.search("account", param, UserBean.class);
76
77 // search column value is existing.
78 boolean isExist = select.existColumnValue("account","id","ncanis".getBytes());
79
80 // update password.
81 UpdateQuery update = dbo.createUpdateQuery("user");
82 Hashtable<String, byte[]> colsTable = new Hashtable<String, byte[]>();
83 colsTable.put("password","2222".getBytes());
84 update.update(bean.getRow(),"account",colsTable);
85
86 //delete
87 DeleteQuery delete = dbo.createDeleteQuery("user");
88 delete.deleteRow(resultBean.getRow());
89
90 ////////////////////////////////////
91 // etc
92
93 // HTable pool with apache commons pool
94 // borrow and release. HBasePoolManager(maxActive, minIdle etc..)
95 IndexedTable table = dbo.getPool().borrow("user");
96 dbo.getPool().release(table);
97
98 // upload bigFile by hadoop directly.
99 HBaseBigFile bigFile = new HBaseBigFile();
100 File file = new File("doc/movie.avi");
101 FileInputStream fis = new FileInputStream(file);
102 Path rootPath = new Path("/files/");
103 String filename = "movie.avi";
104 bigFile.uploadFile(rootPath,filename,fis,true);
105
106 // receive file stream from hadoop.
107 Path p = new Path(rootPath,filename);
108 InputStream is = bigFile.path2Stream(p,4096);
109
110
111 }
112 }