1 package org.apache.helix.tools;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.FileWriter;
26 import java.io.FilenameFilter;
27 import java.io.IOException;
28 import java.util.List;
29
30 import org.I0Itec.zkclient.exception.ZkMarshallingError;
31 import org.I0Itec.zkclient.serialize.ZkSerializer;
32 import org.apache.commons.cli.CommandLine;
33 import org.apache.commons.cli.CommandLineParser;
34 import org.apache.commons.cli.HelpFormatter;
35 import org.apache.commons.cli.Option;
36 import org.apache.commons.cli.OptionBuilder;
37 import org.apache.commons.cli.OptionGroup;
38 import org.apache.commons.cli.Options;
39 import org.apache.commons.cli.PosixParser;
40 import org.apache.helix.manager.zk.ZkClient;
41
42
43
44
45
46
47
48 @SuppressWarnings("static-access")
49 public class ZKDumper
50 {
51 private ZkClient client;
52 private FilenameFilter filter;
53 static Options options;
54 private String suffix = "";
55
56 private boolean removeSuffix=false;
57
58 public String getSuffix()
59 {
60 return suffix;
61 }
62
63 public void setSuffix(String suffix)
64 {
65 this.suffix = suffix;
66 }
67
68 public boolean isRemoveSuffix()
69 {
70 return removeSuffix;
71 }
72
73 public void setRemoveSuffix(boolean removeSuffix)
74 {
75 this.removeSuffix = removeSuffix;
76 }
77
78 static
79 {
80 options = new Options();
81 OptionGroup optionGroup = new OptionGroup();
82
83 Option d = OptionBuilder.withLongOpt("download")
84 .withDescription("Download from ZK to File System").create();
85 d.setArgs(0);
86 Option dSuffix = OptionBuilder.withLongOpt("addSuffix")
87 .withDescription("add suffix to every file downloaded from ZK").create();
88 dSuffix.setArgs(1);
89 dSuffix.setRequired(false);
90
91 Option u = OptionBuilder.withLongOpt("upload")
92 .withDescription("Upload from File System to ZK").create();
93 u.setArgs(0);
94 Option uSuffix = OptionBuilder.withLongOpt("removeSuffix")
95 .withDescription("remove suffix from every file uploaded to ZK").create();
96 uSuffix.setArgs(0);
97 uSuffix.setRequired(false);
98
99 Option del = OptionBuilder.withLongOpt("delete")
100 .withDescription("Delete given path from ZK").create();
101
102 optionGroup.setRequired(true);
103 optionGroup.addOption(del);
104 optionGroup.addOption(u);
105 optionGroup.addOption(d);
106 options.addOptionGroup(optionGroup);
107 options.addOption("zkSvr", true, "Zookeeper address");
108 options.addOption("zkpath", true, "Zookeeper path");
109 options.addOption("fspath", true, "Path on local Filesystem to dump");
110 options.addOption("h", "help", false, "Print this usage information");
111 options.addOption("v", "verbose", false, "Print out VERBOSE information");
112 options.addOption(dSuffix);
113 options.addOption(uSuffix);
114 }
115
116 public ZKDumper(String zkAddress)
117 {
118 client = new ZkClient(zkAddress, ZkClient.DEFAULT_CONNECTION_TIMEOUT);
119 ZkSerializer zkSerializer = new ZkSerializer()
120 {
121
122 @Override
123 public byte[] serialize(Object arg0) throws ZkMarshallingError
124 {
125 return arg0.toString().getBytes();
126 }
127
128 @Override
129 public Object deserialize(byte[] arg0) throws ZkMarshallingError
130 {
131 return new String(arg0);
132 }
133 };
134 client.setZkSerializer(zkSerializer);
135 filter = new FilenameFilter()
136 {
137
138 @Override
139 public boolean accept(File dir, String name)
140 {
141 return !name.startsWith(".");
142 }
143 };
144 }
145
146 public static void main(String[] args) throws Exception
147 {
148 if (args == null || args.length == 0)
149 {
150 HelpFormatter helpFormatter = new HelpFormatter();
151 helpFormatter.printHelp("java " + ZKDumper.class.getName(), options);
152 System.exit(1);
153 }
154 CommandLineParser parser = new PosixParser();
155 CommandLine cmd = parser.parse(options, args);
156 cmd.hasOption("zkSvr");
157 boolean download = cmd.hasOption("download");
158 boolean upload = cmd.hasOption("upload");
159 boolean del = cmd.hasOption("delete");
160 String zkAddress = cmd.getOptionValue("zkSvr");
161 String zkPath = cmd.getOptionValue("zkpath");
162 String fsPath = cmd.getOptionValue("fspath");
163
164 ZKDumper zkDump = new ZKDumper(zkAddress);
165 if (download)
166 {
167 if (cmd.hasOption("addSuffix"))
168 {
169 zkDump.suffix = cmd.getOptionValue("addSuffix");
170 }
171 zkDump.download(zkPath, fsPath + zkPath);
172 }
173 if (upload)
174 {
175 if (cmd.hasOption("removeSuffix"))
176 {
177 zkDump.removeSuffix = true;
178 }
179 zkDump.upload(zkPath, fsPath);
180 }
181 if (del)
182 {
183 zkDump.delete(zkPath);
184 }
185 }
186
187 private void delete(String zkPath)
188 {
189 client.deleteRecursive(zkPath);
190
191 }
192
193 public void upload(String zkPath, String fsPath) throws Exception
194 {
195 File file = new File(fsPath);
196 System.out
197 .println("Uploading " + file.getCanonicalPath() + " to " + zkPath);
198 zkPath = zkPath.replaceAll("[/]+", "/");
199 int index = -1;
200 if (removeSuffix && (index = file.getName().indexOf(".")) > -1)
201 {
202 zkPath = zkPath.replaceAll(file.getName().substring(index), "");
203 }
204 if (file.isDirectory())
205 {
206 File[] children = file.listFiles(filter);
207 client.createPersistent(zkPath, true);
208 if (children != null && children.length > 0)
209 {
210
211 for (File child : children)
212 {
213 upload(zkPath + "/" + child.getName(), fsPath + "/" + child.getName());
214 }
215 } else
216 {
217
218 }
219 } else
220 {
221 BufferedReader bfr = null;
222 try
223 {
224 bfr = new BufferedReader(new FileReader(file));
225 StringBuilder sb = new StringBuilder();
226 String line;
227 String recordDelimiter = "";
228 while ((line = bfr.readLine()) != null)
229 {
230 sb.append(recordDelimiter).append(line);
231 recordDelimiter = "\n";
232 }
233 client.createPersistent(zkPath, sb.toString());
234 } catch (Exception e)
235 {
236 throw e;
237 } finally
238 {
239 if (bfr != null)
240 {
241 try
242 {
243 bfr.close();
244 } catch (IOException e)
245 {
246 }
247 }
248 }
249 }
250 }
251
252 public void download(String zkPath, String fsPath) throws Exception
253 {
254
255 List<String> children = client.getChildren(zkPath);
256 if (children != null && children.size() > 0)
257 {
258 new File(fsPath).mkdirs();
259 for (String child : children)
260 {
261 String childPath = zkPath.equals("/")? "/" + child : zkPath + "/" + child;
262 download(childPath, fsPath + "/" + child);
263 }
264 } else
265 {
266 System.out.println("Saving " + zkPath + " to "
267 + new File(fsPath + suffix).getCanonicalPath());
268 FileWriter fileWriter = new FileWriter(fsPath + suffix);
269 Object readData = client.readData(zkPath);
270 if (readData != null)
271 {
272 fileWriter.write((String) readData);
273 }
274 fileWriter.close();
275 }
276 }
277 }