forked from bd-group/dservice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
386 lines (341 loc) · 12.4 KB
/
Test.java
File metadata and controls
386 lines (341 loc) · 12.4 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.apache.hadoop.hive.metastore.api.FileOperationException;
import org.apache.hadoop.hive.metastore.api.Index;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
import org.apache.hadoop.hive.metastore.api.Node;
import org.apache.hadoop.hive.metastore.api.Partition;
import org.apache.hadoop.hive.metastore.api.SFile;
import org.apache.hadoop.hive.metastore.api.SFileLocation;
import org.apache.hadoop.hive.metastore.api.SplitValue;
import org.apache.hadoop.hive.metastore.api.UnknownTableException;
import org.apache.hadoop.hive.metastore.model.MFile;
import org.apache.hadoop.hive.metastore.model.MetaStoreConst;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.LongField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.sandbox.queries.regex.RegexQuery;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.thrift.TException;
import iie.index.lucene.SFDirectory;
import iie.metastore.MetaStoreClient;
import devmap.DevMap;
public class Test {
/** Index all text files under a directory. */
public static void IndexFileTest(SFDirectory dir) {
int numDocs = 100;
boolean create = true;
Date start = new Date();
try {
System.out.println("Indexing to directory '" + dir.getFullPath()
+ "'...");
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
IndexWriterConfig iwc = new IndexWriterConfig(
Version.LUCENE_CURRENT, analyzer);
if (create) {
// Create a new index in the directory, removing any
// previously indexed documents:
iwc.setOpenMode(OpenMode.CREATE);
} else {
// Add new documents to an existing index:
iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
}
// Optional: for better indexing performance, if you
// are indexing many documents, increase the RAM
// buffer. But if you do this, increase the max heap
// size to the JVM (eg add -Xmx512m or -Xmx1g):
//
// iwc.setRAMBufferSizeMB(256.0);
IndexWriter writer = new IndexWriter(dir, iwc);
indexDocs(writer, numDocs);
// NOTE: if you want to maximize search performance,
// you can optionally call forceMerge here. This can be
// a terribly costly operation, so generally it's only
// worth it when your index is relatively static (ie
// you're done adding documents to it):
//
// writer.forceMerge(1);
writer.close();
Date end = new Date();
System.out.println(end.getTime() - start.getTime()
+ " total milliseconds");
} catch (IOException e) {
System.out.println(" caught a " + e.getClass()
+ "\n with message: " + e.getMessage());
}
}
static void indexDocs(IndexWriter writer, int numDocs)
throws IOException {
if (numDocs <= 0)
return;
Random rand = new Random();
System.out.println("Generate " + numDocs + " KV pairs...");
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
IntField nf = new IntField("foo", rand.nextInt(),
Field.Store.YES);
// doc.add(new NumericField("foo").setIntValue(rand.nextInt()));
doc.add(nf);
doc.add(new Field("bar", "hello, world!", Field.Store.YES,
Field.Index.ANALYZED));
String vv = "\"" + nf.stringValue() + "\",\"hello, world!\"";
doc.add(new Field("content", vv, Field.Store.YES,
Field.Index.NO));
writer.addDocument(doc);
}
}
/** Simple command-line based search demo. */
public static void ReadFiles(SFDirectory dir) throws Exception {
String field = "contents";
System.out.println("----- Scan it -----");
IndexReader reader = IndexReader.open(dir);
for (int i = 0; i < reader.maxDoc(); i++) {
Document doc = reader.document(i);
System.out.println("Key: " + doc.get("foo") + ", Value: " + doc.get("bar") + ", Content: " + doc.get("content"));
}
reader.close();
System.out.println("-----Search it------(foo>0)");
IndexSearcher searcher = new IndexSearcher(IndexReader.open(dir));
Query q = NumericRangeQuery.newIntRange("foo", new Integer("0"), null, false, false);
ScoreDoc[] hits = searcher.search(q, 100).scoreDocs;
System.out.println("Hits -> " + hits.length);
for (int i = 0; i < hits.length; i++) {
Document doc = searcher.doc(hits[i].doc);
System.out.println("Key: " + doc.get("foo") + ", Value: " + doc.get("bar"));
}
System.out.println("-----Regex it------(bar like hello.*)");
searcher = new IndexSearcher(DirectoryReader.open(dir));
q = new RegexQuery(new Term("bar", "hello.*"));
hits = searcher.search(q, 100).scoreDocs;
System.out.println("Hits -> " + hits.length);
for (int i = 0; i < hits.length; i++) {
Document doc = searcher.doc(hits[i].doc);
System.out.println("Key: " + doc.get("foo") + ", Value: " + doc.get("bar"));
}
System.out.println("-----BooleanQuery it ------(bar: hello and foo<=0)");
searcher = new IndexSearcher(DirectoryReader.open(dir));
BooleanQuery bq = new BooleanQuery();
q = new TermQuery(new Term("bar", "hello"));
bq.add(q, BooleanClause.Occur.SHOULD);
q = NumericRangeQuery.newIntRange("foo", 0, null, false, false);
bq.add(q, BooleanClause.Occur.MUST_NOT);
hits = searcher.search(bq, 100).scoreDocs;
System.out.println("Hits -> " + hits.length);
for (int i = 0; i < hits.length; i++) {
Document doc = searcher.doc(hits[i].doc);
System.out.println("Key: " + doc.get("foo") + ", Value: " + doc.get("bar"));
}
}
public static void main(String[] args) {
System.out.println("Begin DevMap Test ...");
if (DevMap.isValid()) {
System.out.println("DevMap is valid!");
} else {
System.out.println("Invalid!");
}
DevMap dm = new DevMap();
dm.refreshDevMap();
System.out.print(dm.dumpDevMap());
System.out.println("End DevMap Test.");
System.out.println("Begin IIE Test ...");
MetaStoreClient cli = null;
String node = null;
String dbName = "default";
String tableName = "pokes";
int repnr = 3;
SFile file = null, r = null;
Node thisNode = null;
Partition p = null;
Index idx = null;
try {
if (args.length > 0) {
cli = new MetaStoreClient(args[0]);
} else {
cli = new MetaStoreClient();
}
} catch (MetaException e) {
e.printStackTrace();
System.exit(0);
}
try {
node = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (thisNode == null) {
try {
thisNode = cli.client.get_node(node);
} catch (MetaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
try {
p = cli.client.getPartition("default", "pokes", "A");
System.out.println("getPartition() success!" + p.getPartitionName());
} catch (MetaException e) {
e.printStackTrace();
return;
} catch (UnknownTableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
idx = cli.client.getIndex("default", "pokes", "idx_foo");
System.out.println("getIndex() success!" + idx.getIndexName());
cli.client.add_partition_index(idx, p);
System.out.println("add_partition_index() success!");
} catch (MetaException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (UnknownTableException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (NoSuchObjectException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (TException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
file = cli.client.create_file(node, repnr, dbName, tableName, new ArrayList<SplitValue>());
System.out.println("Create file: " + MetaStoreClient.toStringSFile(file));
} catch (FileOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
try {
List<SFile> lf = new ArrayList<SFile>();
List<Long> ll = new ArrayList<Long>();
lf.add(file);
ll.add(file.getFid());
cli.client.add_partition_files(p, lf);
cli.client.add_subpartition_files(p.getSubpartitions().get(0), lf);
System.out.println("Add file to partition: done!");
// reget the partition object
p = cli.client.getPartition("default", "pokes", "A");
for (long id : p.getFiles()) {
System.out.println("Get partition files (FID): " + id);
}
System.out.println("Get files'size = " + p.getFilesSize());
cli.client.add_partition_index_files(idx, p, lf, ll);
System.out.println("Add file to partition_index_store: done!");
} catch (TException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
SFDirectory dir = new SFDirectory(file, thisNode, SFDirectory.DirType.AUTO, null);
IndexFileTest(dir);
ReadFiles(dir);
dir.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
long fid = file.getFid();
file.setDigest("DIGESTED!");
file.setLength(64 * 1024 * 1024);
cli.client.close_file(file);
System.out.println("Closed file: " + MetaStoreClient.toStringSFile(file));
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
r = cli.client.get_file_by_id(fid);
System.out.println("Read 1 file: " + MetaStoreClient.toStringSFile(r));
while (r.getStore_status() != MetaStoreConst.MFileStoreStatus.REPLICATED) {
try {
Thread.sleep(10000);
r = cli.client.get_file_by_id(fid);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
// delete it logically
if (cli.client.rm_file_logical(r) != 0) {
System.out.println("ERROR rm_file_logical!");
}
r = cli.client.get_file_by_id(fid);
System.out.println("Read 2 file: " + MetaStoreClient.toStringSFile(r));
if (r.getStore_status() == MetaStoreConst.MFileStoreStatus.RM_LOGICAL) {
// restore it
cli.client.restore_file(r);
}
r = cli.client.get_file_by_id(fid);
System.out.println("Read 3 file: " + MetaStoreClient.toStringSFile(r));
// delete it physically
List<SFile> lf = new ArrayList<SFile>();
lf.add(r);
cli.client.drop_partition_files(p, lf);
System.out.println("Del file from partition: done!");
cli.client.drop_partition_index_files(idx, p, lf);
System.out.println("Del file from partition_index_store: done!");
cli.client.drop_partition_index(idx, p);
System.out.println("drop_partition_index() success!");
cli.client.rm_file_physical(r);
r = cli.client.get_file_by_id(fid);
System.out.println("Read 4 file: " + MetaStoreClient.toStringSFile(r));
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
r = cli.client.get_file_by_id(fid);
System.out.println("Read 5 file: " + MetaStoreClient.toStringSFile(r));
} catch (FileOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("End IIE Test ...");
}
}