1 | /** |
---|
2 | * Program: BuildHTable.java |
---|
3 | * Editor: Waue Chen |
---|
4 | * From : NCHC. Taiwn |
---|
5 | * Last Update Date: 06/10/2008 |
---|
6 | */ |
---|
7 | |
---|
8 | package tw.org.nchc.code; |
---|
9 | import java.io.IOException; |
---|
10 | import org.apache.hadoop.hbase.HBaseAdmin; |
---|
11 | import org.apache.hadoop.hbase.HBaseConfiguration; |
---|
12 | import org.apache.hadoop.hbase.HColumnDescriptor; |
---|
13 | import org.apache.hadoop.hbase.HTableDescriptor; |
---|
14 | import org.apache.hadoop.io.Text; |
---|
15 | |
---|
16 | public class BuildHTable { |
---|
17 | private String table_name ; |
---|
18 | private String[] Column_Family ; |
---|
19 | HBaseConfiguration conf = new HBaseConfiguration(); |
---|
20 | HBaseAdmin admin = new HBaseAdmin(conf); |
---|
21 | public BuildHTable(String table,String[] CF) throws IOException{ |
---|
22 | table_name = table; |
---|
23 | Column_Family = CF; |
---|
24 | } |
---|
25 | |
---|
26 | |
---|
27 | public boolean checkTableExist(String tname)throws IOException{ |
---|
28 | if(! admin.tableExists(new Text(tname))){ |
---|
29 | return false; |
---|
30 | } |
---|
31 | return true; |
---|
32 | } |
---|
33 | // create Hbase table , success = 1 ; failse = 0; Table_exist = 2; |
---|
34 | public boolean createTable() throws IOException{ |
---|
35 | // check whether Table name exite or not |
---|
36 | if(! checkTableExist(table_name)){ |
---|
37 | System.out.println("HTable : " + table_name + " creating ... please wait"); |
---|
38 | HTableDescriptor tableDesc = new HTableDescriptor(table_name); |
---|
39 | for(int i =0; i<Column_Family.length; i++){ |
---|
40 | String st = Column_Family[i]; |
---|
41 | //check name format "string:" |
---|
42 | if(! st.endsWith(":")){ |
---|
43 | Column_Family[i] = st+":"; |
---|
44 | System.out.println("normize :" + st +"->" +Column_Family[i]); |
---|
45 | } |
---|
46 | //add column family |
---|
47 | tableDesc.addFamily(new HColumnDescriptor(Column_Family[i])); |
---|
48 | } |
---|
49 | admin.createTable(tableDesc); |
---|
50 | } else { |
---|
51 | return false; |
---|
52 | } |
---|
53 | return true; |
---|
54 | } |
---|
55 | public static void main(String[] args) throws IOException { |
---|
56 | |
---|
57 | // setup Table name |
---|
58 | String Table_Name = "test_create_table2"; |
---|
59 | // setup Column Family |
---|
60 | String[] Column_Family = {"http:","url:","referrer:"}; |
---|
61 | |
---|
62 | BuildHTable bt = new BuildHTable( Table_Name , Column_Family); |
---|
63 | boolean ret = bt.createTable(); |
---|
64 | if(ret == true){ |
---|
65 | System.out.println("Create Table \"" +Table_Name +" \" Compelte !!!"); |
---|
66 | }else { |
---|
67 | System.out.println("Table Name \"" +Table_Name +" \" exit!!!"); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|