Changeset 4135
- Timestamp:
- 2010-07-12 11:46:09 (3 years ago)
- Location:
- projects/lily/trunk/tools/import
- Files:
-
- 3 edited
-
README.txt (modified) (1 diff)
-
pom.xml (modified) (1 diff)
-
src/main/java/org/lilycms/tools/import_/JsonImportTool.java (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
projects/lily/trunk/tools/import/README.txt
r4131 r4135 13 13 In a source setting, you can run this tool with maven: 14 14 15 mvn exec:java -Dexec.args=" sample.json localhost:2181"15 mvn exec:java -Dexec.args="-z localhost:2181 sample.json" -
projects/lily/trunk/tools/import/pom.xml
r4131 r4135 52 52 <artifactId>lily-client</artifactId> 53 53 </dependency> 54 <dependency> 55 <groupId>commons-cli</groupId> 56 <artifactId>commons-cli</artifactId> 57 <version>1.2</version> 58 </dependency> 54 59 </dependencies> 55 60 -
projects/lily/trunk/tools/import/src/main/java/org/lilycms/tools/import_/JsonImportTool.java
r4131 r4135 1 1 package org.lilycms.tools.import_; 2 2 3 import org.apache.commons.cli.*; 3 4 import org.codehaus.jackson.JsonFactory; 4 5 import org.codehaus.jackson.JsonNode; … … 11 12 import org.lilycms.repository.api.*; 12 13 import org.lilycms.repoutil.VersionTag; 14 import org.lilycms.util.io.Closer; 13 15 14 16 import static org.lilycms.repoutil.JsonUtil.*; … … 24 26 private TypeManager typeManager; 25 27 28 private static final String DEFAULT_ZK_CONNECT = "localhost:2181"; 29 26 30 public static void main(String[] args) throws Exception { 27 if (args.length != 2) { 28 System.out.println("Specify two arguments: file to import, zookeeper connect string"); 31 Options cliOptions = new Options(); 32 33 Option zkOption = OptionBuilder 34 .withArgName("quorum") 35 .hasArg() 36 .withDescription("Zookeeper quorum: hostname1:port,hostname2:port,...") 37 .withLongOpt("zookeeper") 38 .create("z"); 39 cliOptions.addOption(zkOption); 40 41 Option schemaOnlyOption = OptionBuilder 42 .withDescription("Only import the field types and record types, not the records.") 43 .withLongOpt("schema-only") 44 .create("s"); 45 cliOptions.addOption(schemaOnlyOption); 46 47 Option helpOption = new Option("h", "help", false, "Shows help"); 48 cliOptions.addOption(helpOption); 49 50 CommandLineParser parser = new PosixParser(); 51 CommandLine cmd = null; 52 boolean showHelp = false; 53 try { 54 cmd = parser.parse(cliOptions, args); 55 } catch (ParseException e) { 56 showHelp = true; 57 } 58 59 if (showHelp || cmd.hasOption(helpOption.getOpt())) { 60 printHelp(cliOptions); 29 61 System.exit(1); 30 62 } 31 63 32 String fileName = args[0]; 33 String zookeeperConnectString = args[1]; 64 if (cmd.getArgList().size() < 1) { 65 System.out.println("No import file specified!"); 66 System.exit(1); 67 } 68 69 String zookeeperConnectString; 70 if (!cmd.hasOption(zkOption.getOpt())) { 71 System.out.println("Zookeeper quorum not specified, using default: " + DEFAULT_ZK_CONNECT); 72 zookeeperConnectString = DEFAULT_ZK_CONNECT; 73 } else { 74 zookeeperConnectString = cmd.getOptionValue(zkOption.getOpt()); 75 } 76 77 boolean schemaOnly = cmd.hasOption(schemaOnlyOption.getOpt()); 34 78 35 79 Client client = new Client(zookeeperConnectString); 36 80 37 InputStream is = new FileInputStream(fileName); 38 load(client.getRepository(), is); 39 } 40 41 public static void load(Repository repository, InputStream is) throws Exception { 42 load(repository, new DefaultImportListener(), is); 43 } 44 45 public static void load(Repository repository, ImportListener importListener, InputStream is) throws Exception { 46 new JsonImportTool(repository, importListener).load(is); 81 for (String arg : (List<String>)cmd.getArgList()) { 82 System.out.println("----------------------------------------------------------------------"); 83 System.out.println("Importing " + arg); 84 InputStream is = new FileInputStream(arg); 85 try { 86 load(client.getRepository(), is, schemaOnly); 87 } finally { 88 Closer.close(is); 89 } 90 } 91 } 92 93 private static void printHelp(Options cliOptions) { 94 HelpFormatter help = new HelpFormatter(); 95 help.setArgName("[import file]"); 96 help.printHelp("lily-import", cliOptions, true); 97 } 98 99 public static void load(Repository repository, InputStream is, boolean schemaOnly) throws Exception { 100 load(repository, new DefaultImportListener(), is, schemaOnly); 101 } 102 103 public static void load(Repository repository, ImportListener importListener, InputStream is, boolean schemaOnly) 104 throws Exception { 105 new JsonImportTool(repository, importListener).load(is, schemaOnly); 47 106 } 48 107 … … 57 116 } 58 117 59 public void load(InputStream is ) throws Exception {118 public void load(InputStream is, boolean schemaOnly) throws Exception { 60 119 // A combination of the Jackson streaming and tree APIs is used: we move streaming through the 61 120 // whole of the file, but use the tree API to load individual items (field types, records, ...). … … 109 168 } 110 169 } else if (fieldName.equals("records")) { 111 if (current == JsonToken.START_ARRAY) { 112 while (jp.nextToken() != JsonToken.END_ARRAY) { 113 importRecord(jp.readValueAsTree()); 170 if (!schemaOnly) { 171 if (current == JsonToken.START_ARRAY) { 172 while (jp.nextToken() != JsonToken.END_ARRAY) { 173 importRecord(jp.readValueAsTree()); 174 } 175 } else { 176 System.out.println("Error: records property should be an array. Skipping."); 177 jp.skipChildren(); 114 178 } 115 179 } else { 116 System.out.println("Error: records property should be an array. Skipping."); 117 jp.skipChildren(); 180 jp.skipChildren(); 118 181 } 119 182 }
Note: See TracChangeset
for help on using the changeset viewer.