Changeset 4135


Ignore:
Timestamp:
2010-07-12 11:46:09 (3 years ago)
Author:
bruno
Message:

Import tool: support an option such that only the schema part of the import file is imported. Introduce commons-cli for options parsing (syntax slightly changed). Support reading multiple import files.

Location:
projects/lily/trunk/tools/import
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • projects/lily/trunk/tools/import/README.txt

    r4131 r4135  
    1313In a source setting, you can run this tool with maven: 
    1414 
    15 mvn exec:java -Dexec.args="sample.json localhost:2181" 
     15mvn exec:java -Dexec.args="-z localhost:2181 sample.json" 
  • projects/lily/trunk/tools/import/pom.xml

    r4131 r4135  
    5252      <artifactId>lily-client</artifactId> 
    5353    </dependency> 
     54    <dependency> 
     55      <groupId>commons-cli</groupId> 
     56      <artifactId>commons-cli</artifactId> 
     57      <version>1.2</version> 
     58    </dependency> 
    5459  </dependencies> 
    5560 
  • projects/lily/trunk/tools/import/src/main/java/org/lilycms/tools/import_/JsonImportTool.java

    r4131 r4135  
    11package org.lilycms.tools.import_; 
    22 
     3import org.apache.commons.cli.*; 
    34import org.codehaus.jackson.JsonFactory; 
    45import org.codehaus.jackson.JsonNode; 
     
    1112import org.lilycms.repository.api.*; 
    1213import org.lilycms.repoutil.VersionTag; 
     14import org.lilycms.util.io.Closer; 
    1315 
    1416import static org.lilycms.repoutil.JsonUtil.*; 
     
    2426    private TypeManager typeManager; 
    2527 
     28    private static final String DEFAULT_ZK_CONNECT = "localhost:2181"; 
     29 
    2630    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); 
    2961            System.exit(1); 
    3062        } 
    3163 
    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()); 
    3478 
    3579        Client client = new Client(zookeeperConnectString); 
    3680 
    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); 
    47106    } 
    48107 
     
    57116    } 
    58117 
    59     public void load(InputStream is) throws Exception { 
     118    public void load(InputStream is, boolean schemaOnly) throws Exception { 
    60119        // A combination of the Jackson streaming and tree APIs is used: we move streaming through the 
    61120        // whole of the file, but use the tree API to load individual items (field types, records, ...). 
     
    109168                } 
    110169            } 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(); 
    114178                    } 
    115179                } else { 
    116                     System.out.println("Error: records property should be an array. Skipping."); 
    117                     jp.skipChildren(); 
     180                    jp.skipChildren();                     
    118181                } 
    119182            } 
Note: See TracChangeset for help on using the changeset viewer.