hbase connexion distant sur mon cluster linux, avec eclipse sous W7. pas de difficultés.

j'ai trouvé un exemple sympa que j'ai du adapter à la version hbase 98.4, la plupart des méthodes étaient deprecated, mais
il fait le tour des actions de base, creation, insert, delete, list.

Pour accéder à une cellule d'un enregistrement hbase, il faut tenir compte de:

clé,
famille,
qualificateur,
valeur,
timestamp ( pour gérer la version des données si c'est activée)


Si rien de défini il tiendra compte du fichier hbase-site.xml pour la connection, sinon on peut redéfinir au niveau du programme java,
attention aux adresse ip, il y a une résolution de nom et il faut alors ajouter le nom des serveurs dans le fichier ./etc/hosts, sinon ca gèle
d'ou la mise en place de timeout

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<configuration>
	<property>
		<name>hbase.master</name>
		<value>192.168.0.11:60000</value>
	</property>
	<property>
		<name>hbase.cluster.distributed</name>
		<value>true</value>
	</property>

	<property>
		<name>hbase.rootdir</name>
		<value>hdfs://192.168.0.11:9000/hbase</value>
	</property>
	<property>
		<name>hbase.zookeeper.property.clientPort</name>
		<value>2181</value>
	</property>
	<property>
		<name>hbase.zookeeper.quorum</name>
		<value>192.168.0.11,192.168.0.31,192.168.0.32</value>
	</property>
</configuration>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
	import java.io.IOException;
	import java.util.ArrayList;
	import java.util.List;
	 
import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
	import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
	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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
	import org.apache.hadoop.hbase.ZooKeeperConnectionException;
	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.Result;
	import org.apache.hadoop.hbase.client.ResultScanner;
	import org.apache.hadoop.hbase.client.Scan;
	import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import com.google.protobuf.ServiceException;
	 
	public class TestHbaseRemote {
	 
		
	    private static Configuration conf = null;
	    /**
	     * Initialization
	     */
	    static {
	        conf = HBaseConfiguration.create();
	    }
	 
	    /**
	     * Create a table
	     */
	    public static void creatTable(String tableName, String[] familys)
	            throws Exception {
	        HBaseAdmin admin = new HBaseAdmin(conf);
	        if (admin.tableExists(tableName)) {
	            System.out.println("la table existe déjà !");
	        } 
	        else {
	        	  HTableDescriptor tableDesc = new  HTableDescriptor(TableName.valueOf(tableName));
	            for (int i = 0; i < familys.length; i++) {
	                tableDesc.addFamily(new HColumnDescriptor(familys[i]));
	            }
	            admin.createTable(tableDesc);
	            System.out.println("create table " + tableName + " ok.");
	        }
	    }
	 
	    /**
	     * Delete a table
	     */
	    public static void deleteTable(String tableName) throws Exception {
	        try {
	            HBaseAdmin admin = new HBaseAdmin(conf);
	            admin.disableTable(tableName);
	            admin.deleteTable(tableName);
	            System.out.println("delete table " + tableName + " ok.");
	        } catch (MasterNotRunningException e) {
	            e.printStackTrace();
	        } catch (ZooKeeperConnectionException e) {
	            e.printStackTrace();
	        }
	    }
	 
	    /**
	     * Put (or insert) a row
	     */
	    public static void addRecord(String tableName, String rowKey,
	            String family, String qualifier, String value) throws Exception {
	        try {
	            HTable table = new HTable(conf, tableName);
	            Put put = new Put(Bytes.toBytes(rowKey));
	            put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes
	                    .toBytes(value));
	            table.put(put);
	            System.out.println("insert recored " + rowKey + " to table "
	                    + tableName + " ok.");
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	 
	    /**
	     * Delete a row
	     */
	    public static void delRecord(String tableName, String rowKey)
	            throws IOException {
	        HTable table = new HTable(conf, tableName);
	        List<Delete> list = new ArrayList<Delete>();
	        Delete del = new Delete(rowKey.getBytes());
	        list.add(del);
	        table.delete(list);
	        System.out.println("del recored " + rowKey + " ok.");
	    }
	 
	    /**
	     * Get a row
	     */
	    public static void getOneRecord (String tableName, String rowKey) throws IOException{
	        HTable table = new HTable(conf, tableName);
	        Get get = new Get(rowKey.getBytes());
	        Result rs = table.get(get);
	        
	        
	        
	        for(Cell kv : rs.rawCells()){
	        	System.out.print(new String(CellUtil.cloneRow(kv)) + " ");
                System.out.print(new String(CellUtil.cloneFamily(kv)) + ":");
                System.out.print(new String(CellUtil.cloneQualifier(kv)) + " ");
                System.out.print(kv.getTimestamp() + " ");
                System.out.println(new String(CellUtil.cloneValue(kv)));
	        }
	    }
	    /**
	     * Scan (or list) a table
	     */
	    public static void getAllRecord (String tableName) {
	        try{
	             HTable table = new HTable(conf, tableName);
	             Scan s = new Scan();
	             ResultScanner ss = table.getScanner(s);
	             for(Result r:ss){
	                 for(Cell kv : r.rawCells() ){
	                	 
	                    System.out.print(new String(CellUtil.cloneRow(kv)) + " ");
	                    System.out.print(new String(CellUtil.cloneFamily(kv)) + ":");
	                    System.out.print(new String(CellUtil.cloneQualifier(kv)) + " ");
	                    System.out.print(kv.getTimestamp() + " ");
	                    System.out.println(new String(CellUtil.cloneValue(kv)));
	                 }
	             }
	        } catch (IOException e){
	            e.printStackTrace();
	        }
	    }
	 
	    public static void main(String[] agrs) {
	    	
	    	 Configuration config = HBaseConfiguration.create();
	    	  config.set("hbase.zookeeper.quorum", "192.168.0.11,192.168.0.31,192.168.0.32");
	          config.set("hbase.master", "192.168.0.11:60000");
	          config.set("hbase.zookeeper.property.clientport", "2181");
	    	 
	    	 config.set("hbase.client.retries.number", Integer.toString(0));
	    	 config.set("zookeeper.session.timeout", Integer.toString(60000));
	    	 config.set("zookeeper.recovery.retry", Integer.toString(0));

     
	        try {
	            String tablename = "scores";
	            String[] familys = { "degre", "lesson" };
	            TestHbaseRemote.creatTable(tablename, familys);
				
				System.out.println("Table create table!");
	            
				// add record tivoli
	            TestHbaseRemote.addRecord(tablename, "tivoli", "degre", "", "5");
	            TestHbaseRemote.addRecord(tablename, "tivoli", "lesson", "francais", "90");
	            TestHbaseRemote.addRecord(tablename, "tivoli", "lesson", "sport", "97");
	            TestHbaseRemote.addRecord(tablename, "tivoli", "lesson", "dessin", "87");
	            System.out.println("Table add records!");
	            
	            // add record lescure
	            TestHbaseRemote.addRecord(tablename, "lescure", "degre", "", "4");
	            TestHbaseRemote.addRecord(tablename, "lescure", "lesson", "math", "89");
	            TestHbaseRemote.addRecord(tablename, "lescure", "lesson", "physique", "29");
	            System.out.println("Table add records!");
	            
	            // add record bordeaux
	            TestHbaseRemote.addRecord(tablename, "bordeaux", "degre", "", "93");
	            TestHbaseRemote.addRecord(tablename, "bordeaux", "lesson", "math", "3");
	            TestHbaseRemote.addRecord(tablename, "bordeaux", "lesson", "dessin", "2");
	            System.out.println("Table add records!");
	            
	            // add record adeleter test
	            TestHbaseRemote.addRecord(tablename, "adeleter", "degre", "", "33");
	            TestHbaseRemote.addRecord(tablename, "adeleter", "lesson", "math", "3");
	            TestHbaseRemote.addRecord(tablename, "adeleter", "lesson", "musique", "2");
	            System.out.println("Table add records!");
	            
	            System.out.println("===========get one record========");
	            TestHbaseRemote.getOneRecord(tablename, "tivoli");
	 	            
	            System.out.println("===========show all record========");
	            TestHbaseRemote.getAllRecord(tablename);
	 
	            System.out.println("===========del one record========");
	            TestHbaseRemote.delRecord(tablename, "adeleter");
	            TestHbaseRemote.getAllRecord(tablename);
	 
	            System.out.println("===========show all record========");
	            TestHbaseRemote.getAllRecord(tablename);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	}
Sortie console
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
la table existe déhç !
Table create table!
insert recored tivoli to table scores ok.
insert recored tivoli to table scores ok.
insert recored tivoli to table scores ok.
insert recored tivoli to table scores ok.
Table add records!
insert recored lescure to table scores ok.
insert recored lescure to table scores ok.
insert recored lescure to table scores ok.
Table add records!
insert recored bordeaux to table scores ok.
insert recored bordeaux to table scores ok.
insert recored bordeaux to table scores ok.
Table add records!
insert recored adeleter to table scores ok.
insert recored adeleter to table scores ok.
insert recored adeleter to table scores ok.
Table add records!
===========get one record========
tivoli degre: 1435001972257 5
tivoli lesson: 1434999704963 90
tivoli lesson:art 1435001972288 87
tivoli lesson:francais 1435001972272 90
tivoli lesson:math 1435001972280 97
===========show all record========
adeleter degre: 1435001972340 33
adeleter lesson:art 1435001972360 2
adeleter lesson:math 1435001972351 3
bordeaux degre: 1435001972318 93
bordeaux lesson:art 1435001972332 2
bordeaux lesson:math 1435001972325 3
lescure degre: 1435001972295 4
lescure lesson: 1435000072889 89
lescure lesson:art 1435001972310 29
lescure lesson:math 1435001972303 89
tivoli degre: 1435001972257 5
tivoli lesson: 1434999704963 90
tivoli lesson:art 1435001972288 87
tivoli lesson:francais 1435001972272 90
tivoli lesson:math 1435001972280 97
===========del one record========
del recored adeleter ok.
bordeaux degre: 1435001972318 93
bordeaux lesson:art 1435001972332 2
bordeaux lesson:math 1435001972325 3
lescure degre: 1435001972295 4
lescure lesson: 1435000072889 89
lescure lesson:art 1435001972310 29
lescure lesson:math 1435001972303 89
tivoli degre: 1435001972257 5
tivoli lesson: 1434999704963 90
tivoli lesson:art 1435001972288 87
tivoli lesson:francais 1435001972272 90
tivoli lesson:math 1435001972280 97
===========show all record========
bordeaux degre: 1435001972318 93
bordeaux lesson:art 1435001972332 2
bordeaux lesson:math 1435001972325 3
lescure degre: 1435001972295 4
lescure lesson: 1435000072889 89
lescure lesson:art 1435001972310 29
lescure lesson:math 1435001972303 89
tivoli degre: 1435001972257 5
tivoli lesson: 1434999704963 90
tivoli lesson:art 1435001972288 87
tivoli lesson:francais 1435001972272 90
tivoli lesson:math 1435001972280 97
Bon, j'en ai fini avec ce sujet, je vais commencer à rebondir sur le Langage R avec la passerelle Rhadoop et Hbase.