-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHFileHack.java
More file actions
263 lines (244 loc) · 7.48 KB
/
Copy pathHFileHack.java
File metadata and controls
263 lines (244 loc) · 7.48 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
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.NavigableMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.io.hfile.CacheConfig;
import org.apache.hadoop.hbase.io.hfile.HFile;
import org.apache.hadoop.hbase.io.hfile.HFileBlockIndex.BlockIndexReader;
import org.apache.hadoop.hbase.regionserver.HRegion;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.FSUtils;
import org.apache.hadoop.hbase.util.Pair;
/**
* hack the hfile
*
* @author wlu
*
*/
public class HFileHack {
static List<Path> getStoreFiles(FileSystem fs, Path regionDir)
throws IOException {
List<Path> res = new ArrayList<Path>();
final PathFilter dirFilter = new FSUtils.DirFilter(fs);
PathFilter nonHidden = new PathFilter() {
@Override
public boolean accept(Path file) {
return dirFilter.accept(file)
&& !file.getName().toString().startsWith(".");
}
};
FileStatus[] familyDirs = fs.listStatus(regionDir, nonHidden);
for (FileStatus dir : familyDirs) {
FileStatus[] files = fs.listStatus(dir.getPath());
for (FileStatus file : files) {
if (!file.isDir()) {
res.add(file.getPath());
}
}
}
return res;
}
public static List<Path> GetReionStoreFiles(Configuration conf,
HRegionInfo info) {
try {
Path rootDir = FSUtils.getRootDir(conf);
// region dir of the input regionInfo
Path regionDir = HRegion.getRegionDir(rootDir, info);
List<Path> regionFiles = getStoreFiles(FileSystem.get(conf),
regionDir);
return regionFiles;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static List<List<byte[]>> getRegionBlockKeysEachStorefile(
Configuration conf, HRegionInfo info) throws IOException {
List<Path> storefiles = GetReionStoreFiles(conf, info);
if (storefiles == null || storefiles.size() == 0) {
return null;
}
FileSystem fs = FileSystem.get(conf);
List<List<byte[]>> regionblockkeys = new ArrayList<List<byte[]>>();
for (Path sfs : storefiles) {
HFile.Reader reader = HFile.createReader(fs, sfs, new CacheConfig(
conf));
BlockIndexReader blockindexreader = reader
.getDataBlockIndexReader();
if (blockindexreader == null) {
continue;
}
List<byte[]> keysofthisfile = new ArrayList<byte[]>();
for (int i = 0; i < blockindexreader.getRootBlockCount(); i++) {
byte[] bkkey = blockindexreader.getRootBlockKey(i);
byte[] rowkey = null;
try {
// get rowkey
int rowlength = Bytes.toShort(bkkey, 0);
rowkey = new byte[rowlength];
Bytes.putBytes(rowkey, 0, bkkey, Bytes.SIZEOF_SHORT,
rowlength);
// add rowkey for return
keysofthisfile.add(rowkey);
} catch (Exception e) {
e.printStackTrace();
System.out.println(sfs);
System.out.println(Bytes.toString(rowkey));
return null;
}
}
regionblockkeys.add(keysofthisfile);
}
return regionblockkeys;
}
public static List<byte[]> mergesrot(List<List<byte[]>> keys) {
List<byte[]> mergedList = new ArrayList<byte[]>();
int[] currentIndex = new int[keys.size()];
for (int i = 0; i < currentIndex.length; i++) {
currentIndex[i] = 0;
}
while (true) {
int minIdx = 0;
// all touch finish line
boolean stop = true;
for (int i = 0; i < keys.size(); i++) {
if (currentIndex[i] < keys.get(i).size()) {
stop = false;
minIdx = i;
break;
}
}
if (stop) {
break;
}
byte[] minVal = keys.get(minIdx).get(currentIndex[minIdx]);
for (int i = 1; i < keys.size(); i++) {
// if touch end
if (currentIndex[i] >= keys.get(i).size()) {
continue;
}
if (Bytes.compareTo(keys.get(i).get(currentIndex[i]), minVal) < 0) {
minIdx = i;
minVal = keys.get(i).get(currentIndex[i]);
}
}
currentIndex[minIdx]++;
mergedList.add(minVal);
}
return mergedList;
}
/**
* get block keys of each store file, merge sort them
*
* @param conf
* @param info
* @param mainFamily
* @return
* @throws IOException
*/
public static List<byte[]> sortedAllBlockKeys(Configuration conf,
HRegionInfo info) throws IOException {
List<List<byte[]>> keys = getRegionBlockKeysEachStorefile(conf, info);
return mergesrot(keys);
}
public static List<byte[]> getAllBlockKeys(Configuration conf, String tableN)
throws IOException {
HTable table = new HTable(conf, tableN);
List<byte[]> ret = new ArrayList<byte[]>();
NavigableMap<HRegionInfo, ServerName> m = table.getRegionLocations();
for (HRegionInfo ri : m.keySet()) {
List<byte[]> mergedkeys = HFileHack.sortedAllBlockKeys(
table.getConfiguration(), ri);
ret.addAll(mergedkeys);
}
table.close();
return ret;
}
public static List<byte[]> getAllBlockKeys(HTable table) throws IOException {
List<byte[]> ret = new ArrayList<byte[]>();
NavigableMap<HRegionInfo, ServerName> m = table.getRegionLocations();
for (HRegionInfo ri : m.keySet()) {
List<byte[]> mergedkeys = HFileHack.sortedAllBlockKeys(
table.getConfiguration(), ri);
ret.addAll(mergedkeys);
}
return ret;
}
public static void main(String args[]) throws IOException {
HTable table = new HTable("NewWisdomPost");
Pair<byte[][], byte[][]> p = table.getStartEndKeys();
byte[][] st = p.getFirst();
for (int i = 0; i < st.length; i++) {
System.out.println(Bytes.toString(p.getFirst()[i]) + "\t"
+ Bytes.toString(p.getSecond()[i]));
}
table.getTableDescriptor().getFamiliesKeys();
NavigableMap<HRegionInfo, ServerName> m = table.getRegionLocations();
for (HRegionInfo ri : m.keySet()) {
// HFileHack.GetReionStoreFiles(table.getConfiguration(), ri, "F");
// print each region's block keys
// List<List<byte[]>> keys =
// HFileHack.getRegionBlockKeysEachStorefile(table.getConfiguration(),
// ri);
// merge
List<byte[]> mergedkeys = HFileHack.sortedAllBlockKeys(
table.getConfiguration(), ri);
System.out.println(Bytes.toString(mergedkeys.get(0)));
System.out
.println(Bytes.toString(mergedkeys.get(mergedkeys.size() - 1)));
// System.out.println(ri.toString());
// for (List<byte[]> ks : keys) {
// for (byte[] k : ks) {
// Get p = new Get(k);
// System.out.println(table.get(p));
// return;
// }
// }
}
// List<List<byte[]>> t = new ArrayList<List<byte[]>>();
// //////////1
// byte[] b11 = Bytes.toBytes("a");
// byte[] b12 = Bytes.toBytes("h");
// byte[] b13 = Bytes.toBytes("k");
// List<byte[]> g1 = new ArrayList<byte[]>();
// g1.add(b11);
// g1.add(b12);
// g1.add(b13);
// //////////2
// byte[] b21 = Bytes.toBytes("b");
// byte[] b22 = Bytes.toBytes("i");
// byte[] b23 = Bytes.toBytes("l");
// List<byte[]> g2 = new ArrayList<byte[]>();
// g2.add(b21);
// g2.add(b22);
// g2.add(b23);
// //////////3
// byte[] b31 = Bytes.toBytes("m");
// byte[] b32 = Bytes.toBytes("n");
// byte[] b33 = Bytes.toBytes("p");
// List<byte[]> g3 = new ArrayList<byte[]>();
// g3.add(b31);
// g3.add(b32);
// g3.add(b33);
//
// t.add(g2);
// t.add(g3);
// t.add(g1);
//
// List<byte[]> mg = HFileHack.mergesrot(t);
// for(byte[] b:mg){
// System.out.println(Bytes.toString(b));
// }
}
}