Example usage for com.google.common.collect Lists transform

List of usage examples for com.google.common.collect Lists transform

Introduction

In this page you can find the example usage for com.google.common.collect Lists transform.

Prototype

@CheckReturnValue
public static <F, T> List<T> transform(List<F> fromList, Function<? super F, ? extends T> function) 

Source Link

Document

Returns a list that applies function to each element of fromList .

Usage

From source file:net.sourceforge.docfetcher.enums.MsgMigrator.java

public static void main(String[] args) throws Exception {
    File oldTransDir = new File("dev/old-translations-from-1.0.3");
    final String enPropName = "Resource.properties";

    Properties oldEnProp = CharsetDetectorHelper.load(new File(oldTransDir, enPropName));
    List<File> oldPropFiles = Arrays.asList(Util.listFiles(oldTransDir, new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return !name.equals(enPropName);
        }/*from   w w  w .  j  a v  a 2 s.  c o  m*/
    }));

    final Map<Properties, File> propToFileMap = Maps.newHashMap();

    List<Properties> oldProps = Lists.transform(oldPropFiles, new Function<File, Properties>() {
        public Properties apply(File file) {
            try {
                Properties prop = CharsetDetectorHelper.load(file);
                propToFileMap.put(prop, file);
                return prop;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        };
    });

    StringBuilder sb0 = new StringBuilder();
    for (Msg msg : Msg.values()) {
        String key = ConfLoader.convert(msg.name(), true);
        String value = ConfLoader.convert(msg.get(), false);
        String comments = msg.getComment();

        if (!comments.isEmpty())
            sb0.append("# " + comments + Util.LS);
        sb0.append(key + "=" + value);
        sb0.append(Util.LS);
    }
    File enOutFile = new File(Util.TEMP_DIR, enPropName);
    Files.write(sb0.toString(), enOutFile, Charsets.UTF_8);
    Util.println("File written: " + enOutFile.getPath());

    for (Properties oldProp : oldProps) {
        StringBuilder sb = new StringBuilder();
        for (Msg msg : Msg.values()) {
            String key = msg.name();

            String enOldValue = oldEnProp.getProperty(key);
            if (enOldValue == null) // New key?
                continue;
            else if (!enOldValue.equals(msg.get())) // Changed value?
                continue;

            String value = oldProp.getProperty(key);
            if (value == null)
                value = enOldValue;
            else if (value.equals("$TODO$"))
                continue;

            key = ConfLoader.convert(key, true);
            value = ConfLoader.convert(value, false);
            sb.append(key + "=" + value);
            sb.append(Util.LS);
        }

        String filename = propToFileMap.get(oldProp).getName();
        File outFile = new File(Util.TEMP_DIR, filename);
        Files.write(sb.toString(), outFile, Charsets.UTF_8);
        Util.println("File written: " + outFile.getPath());
    }
}

From source file:edu.harvard.med.screensaver.io.libraries.LibraryCreator.java

@SuppressWarnings("static-access")
public static void main(String[] args) {
    CommandLineApplication app = new CommandLineApplication(args);
    try {//from   w w  w  .j a  v a2  s . c o  m
        DateTimeFormatter dateFormat = DateTimeFormat.forPattern(CommandLineApplication.DEFAULT_DATE_PATTERN);

        app.addCommandLineOption(OptionBuilder.hasArg().isRequired().withArgName("library name")
                .withLongOpt("name").withDescription("full, official name for the library").create("n"));
        app.addCommandLineOption(
                OptionBuilder.hasArg().isRequired().withArgName("short name").withLongOpt("short-name")
                        .withDescription("a short name for identifying the library").create("s"));
        app.addCommandLineOption(OptionBuilder.hasArg().isRequired().withArgName("library type")
                .withLongOpt("library-type").withDescription(StringUtils.makeListString(Lists.transform(
                        Lists.newArrayList(LibraryType.values()), new Function<LibraryType, String>() {
                            @Override
                            public String apply(LibraryType arg0) {
                                return arg0.name();
                            }
                        }), ", "))
                .create("lt"));
        app.addCommandLineOption(OptionBuilder.hasArg().isRequired().withArgName("screen type")
                .withLongOpt("screen-type").withDescription(StringUtils.makeListString(Lists
                        .transform(Lists.newArrayList(ScreenType.values()), new Function<ScreenType, String>() {
                            @Override
                            public String apply(ScreenType arg0) {
                                return arg0.name();
                            }
                        }), ", "))
                .create("st"));
        app.addCommandLineOption(OptionBuilder.hasArg(false).withLongOpt("is-pool")
                .withDescription("well contents are pools of reagents (only valid when library-type=RNAI)")
                .create("ip"));
        app.addCommandLineOption(
                OptionBuilder.hasArg().isRequired().withArgName("#").withLongOpt("start-plate").create("sp"));
        app.addCommandLineOption(
                OptionBuilder.hasArg().isRequired().withArgName("#").withLongOpt("end-plate").create("ep"));

        app.addCommandLineOption(
                OptionBuilder.hasArg().withArgName("name").withLongOpt("provider").create("lp"));
        app.addCommandLineOption(
                OptionBuilder.hasArg().withArgName("text").withLongOpt("description").create("d"));
        app.addCommandLineOption(OptionBuilder.hasArg().withArgName(CommandLineApplication.DEFAULT_DATE_PATTERN)
                .withLongOpt("date-received").create("dr"));
        app.addCommandLineOption(OptionBuilder.hasArg().withArgName(CommandLineApplication.DEFAULT_DATE_PATTERN)
                .withLongOpt("date-screenable").create("ds"));
        app.addCommandLineOption(OptionBuilder.hasArg().withArgName("plate size")
                .withDescription(StringUtils.makeListString(Lists
                        .transform(Lists.newArrayList(PlateSize.values()), new Function<PlateSize, String>() {
                            @Override
                            public String apply(PlateSize arg0) {
                                return arg0.name();
                            }
                        }), ", "))
                .withLongOpt("plate-size").create("ps"));

        app.processOptions(true, true);

        String libraryName = app.getCommandLineOptionValue("n");
        String shortName = app.getCommandLineOptionValue("s");
        LibraryType libraryType = app.getCommandLineOptionEnumValue("lt", LibraryType.class);
        boolean isPool = app.isCommandLineFlagSet("ip");
        ScreenType screenType = app.getCommandLineOptionEnumValue("st", ScreenType.class);
        int startPlate = app.getCommandLineOptionValue("sp", Integer.class);
        int endPlate = app.getCommandLineOptionValue("ep", Integer.class);
        String vendor = app.isCommandLineFlagSet("lp") ? app.getCommandLineOptionValue("lp") : null;
        String description = app.isCommandLineFlagSet("d") ? app.getCommandLineOptionValue("d") : null;
        LocalDate dateReceived = app.isCommandLineFlagSet("dr")
                ? app.getCommandLineOptionValue("dr", dateFormat).toLocalDate()
                : null;
        LocalDate dateScreenable = app.isCommandLineFlagSet("ds")
                ? app.getCommandLineOptionValue("ds", dateFormat).toLocalDate()
                : null;
        PlateSize plateSize = app.isCommandLineFlagSet("ps")
                ? app.getCommandLineOptionEnumValue("ps", PlateSize.class)
                : ScreensaverConstants.DEFAULT_PLATE_SIZE;

        Library library = new Library(app.findAdministratorUser(), libraryName, shortName, screenType,
                libraryType, startPlate, endPlate, plateSize);
        library.setPool(isPool);
        library.setDescription(description);
        library.setProvider(vendor);
        library.setDateReceived(dateReceived);
        library.setDateScreenable(dateScreenable);

        edu.harvard.med.screensaver.service.libraries.LibraryCreator libraryCreator = (edu.harvard.med.screensaver.service.libraries.LibraryCreator) app
                .getSpringBean("libraryCreator");
        libraryCreator.createLibrary(library);
        log.info("library succesfully added to database");
    } catch (Exception e) {
        e.printStackTrace();
        log.error(e.toString());
        System.err.println("error: " + e.getMessage());
        System.exit(1);
    }
}

From source file:org.thiesen.jiffs.tools.OPMLImporter.java

@SuppressWarnings("unchecked")
public static void main(final String... args) throws Exception {
    final SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", false);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);

    final Document document = builder.build(new File(args[0]));

    XPath path = XPath.newInstance("//outline[@xmlUrl]");

    final List<Element> nodes = path.selectNodes(document);

    System.out.println("Found " + nodes.size() + " Feeds to import!");

    final SubscriptionDAO subscriptionDAO = new SubscriptionDAO();

    subscriptionDAO.insert(Lists.transform(nodes, TO_DBO_FUNCTION));

    System.out.println("Done");
}

From source file:org.carrot2.examples.clustering.UsingComponentSuites.java

public static void main(String[] args) throws Exception {
    @SuppressWarnings("unchecked")
    final Controller controller = ControllerFactory.createCachingPooling(IDocumentSource.class);

    // Initialization-time attributes that will apply to all components.
    final Map<String, Object> initAttributes = Maps.newHashMap();

    // Prepare resource lookup facade. We will use the suites directory 
    // and class path resources.
    final ResourceLookup resourceLookup = new ResourceLookup(new DirLocator(new File("suites")),
            new ContextClassLoaderLocator());

    // We know we'll be using Bing so set up its access key.
    // use your own ID here!
    Bing3WebDocumentSourceDescriptor.attributeBuilder(initAttributes).appid(BingKeyAccess.getKey());

    // We'll read the component suite definition from an XML stream.
    // IResource is an abstraction layer over resources in Carrot2.
    IResource suiteXml = resourceLookup.getFirst("suite-examples.xml");

    // Deserialize the component suite definition.
    final ProcessingComponentSuite suite = ProcessingComponentSuite.deserialize(suiteXml, resourceLookup);

    // Initialize the controller with the suite. All components from the suite
    // will be available for processing within this controller.
    controller.init(initAttributes, suite.getComponentConfigurations());

    // From the suite definition, you can get the document sources and clustering
    // algorithm descriptors.
    final List<DocumentSourceDescriptor> sources = suite.getSources();
    final List<String> sourceIds = Lists.transform(sources,
            ProcessingComponentDescriptor.ProcessingComponentDescriptorToId.INSTANCE);
    System.out.println("Found " + sourceIds.size() + " document sources: " + sourceIds);

    final List<ProcessingComponentDescriptor> algorithms = suite.getAlgorithms();
    final List<String> algorithmIds = Lists.transform(algorithms,
            ProcessingComponentDescriptor.ProcessingComponentDescriptorToId.INSTANCE);
    System.out.println("Found " + algorithmIds.size() + " clutering algorithms: " + algorithmIds + "\n\n");

    // Run not more than two algorithms on not more than two sources
    for (int s = 0; s < Math.min(sourceIds.size(), 2); s++) {
        for (int a = 0; a < Math.min(algorithmIds.size(), 2); a++) {
            // You can retrieve some metadata about the components, such as
            // human-readable label, from their descriptors.
            System.out.println("Querying " + sources.get(s).getLabel() + ", clustering with "
                    + algorithms.get(a).getLabel());

            // As usual, we pass attributes for processing
            final Map<String, Object> attributes = Maps.newHashMap();
            CommonAttributesDescriptor.attributeBuilder(attributes).query("data mining");

            // Pass component ids to the controller to perform processing
            final ProcessingResult result = controller.process(attributes, sourceIds.get(s),
                    algorithmIds.get(a));
            ConsoleFormatter.displayClusters(result.getClusters());
            System.out.println();
        }/* w w  w  . j  ava2  s .  c  om*/
    }
}

From source file:tv.icntv.grade.film.recommend.TopNJob.java

public static void main(String[] args) throws Exception {
    final Configuration configuration = HBaseConfiguration.create();
    configuration.addResource("grade.xml");
    String tables = configuration.get("hbase.cdn.tables");
    if (Strings.isNullOrEmpty(tables)) {
        return;/*w ww  . ja  v  a 2s.  com*/
    }
    List<String> list = Lists.newArrayList(Splitter.on(",").split(tables));
    List<String> results = Lists.transform(list, new Function<String, String>() {
        @Override
        public String apply(@Nullable java.lang.String input) {
            return String.format(configuration.get("hdfs.directory.base.db"), new Date(), input);
        }
    });

    String[] arrays = new String[] { Joiner.on(",").join(results),
            String.format(configuration.get("hdfs.directory.num.middle"), new Date()),
            String.format(configuration.get("hdfs.directory.num.result"), new Date()) };
    AbstractJob job = new TopNJob();
    //        job.setStart(true);
    int i = ToolRunner.run(configuration, job, arrays);
    System.exit(i);
}

From source file:tv.icntv.grade.film.recommend.CFRecommendJob.java

public static void main(String[] args) throws Exception {
    final Configuration configuration = HBaseConfiguration.create();
    configuration.addResource("grade.xml");
    String baseCfData = String.format(configuration.get("hdfs.directory.base.score"), new Date());
    String output = String.format(configuration.get("icntv.cf.recommend.directory.target"), new Date());
    String temp = String.format(configuration.get("icntv.cf.recommend.directory.temp"), new Date());
    StringBuilder sb = new StringBuilder();
    sb.append("--input ").append(baseCfData);
    sb.append(" --output ").append(output);
    sb.append(" --numRecommendations ").append(configuration.get("icntv.cf.recommend.num"));
    sb.append(" --similarityClassname ").append(configuration.get("icntv.cf.recommend.similarityClassname"));
    sb.append(" --tempDir ").append(temp);

    String tables = configuration.get("hbase.cdn.tables");

    if (Strings.isNullOrEmpty(tables)) {
        return;// w w w. j a v  a  2s . co m
    }
    List<String> list = Lists.newArrayList(Splitter.on(",").split(tables));
    List<String> results = Lists.transform(list, new Function<String, String>() {
        @Override
        public String apply(@Nullable java.lang.String input) {
            return String.format(configuration.get("hdfs.directory.base.db"), new Date(), input);
        }
    });

    int i = ToolRunner.run(configuration, new CFRecommendJob(),
            new String[] { Joiner.on(",").join(results), baseCfData, sb.toString(), output, temp });
    System.exit(i);
}

From source file:tv.icntv.grade.film.recommend.CorrelateJob.java

public static void main(String[] args) throws Exception {
    final Configuration configuration = HBaseConfiguration.create();
    configuration.addResource("grade.xml");
    String tables = configuration.get("hbase.cdn.tables");
    if (Strings.isNullOrEmpty(tables)) {
        return;//w w w . java 2s. c  o  m
    }
    List<String> list = Lists.newArrayList(Splitter.on(",").split(tables));
    List<String> results = Lists.transform(list, new Function<String, String>() {
        @Override
        public String apply(@Nullable java.lang.String input) {
            return String.format(configuration.get("hdfs.directory.base.db"), new Date(), input);
        }
    });
    String middleDirectory = String.format(configuration.get("icntv.correlate.input"), new Date());
    StringBuilder sb = new StringBuilder();
    sb.append("minSupport=").append(configuration.get("correlate.minSupport", "3")).append("--")
            .append("maxHeapSize=100").append("--").append("splitterPattern='[\t ]'").append("--")
            .append("input=").append(middleDirectory).append("--").append("output=")
            .append(String.format(configuration.get("icntv.correlate.fp.growth.output"), new Date()));
    ToolRunner.run(configuration, new CorrelateJob(),
            new String[] { Joiner.on(",").join(results), middleDirectory, sb.toString(),
                    String.format(configuration.get("icntv.correlate.output"), new Date()) });
}

From source file:tv.icntv.grade.film.grade.GradeJob.java

public static void main(String[] args) throws Exception {
    final Configuration configuration = HBaseConfiguration.create();
    configuration.addResource("grade.xml");
    String tables = configuration.get("hbase.cdn.tables");
    if (Strings.isNullOrEmpty(tables)) {
        return;//from www  .  j av a  2  s  .c o m
    }
    List<String> list = Lists.newArrayList(Splitter.on(",").split(tables));
    List<String> results = Lists.transform(list, new Function<String, String>() {
        @Override
        public String apply(@Nullable java.lang.String input) {
            return String.format(configuration.get("hdfs.directory.base.db"), new Date(), input);
        }
    });

    String[] arrays = new String[] { Joiner.on(",").join(results), configuration.get("film.see.num.table"),
            String.format(configuration.get("hdfs.directory.base.score"), new Date()),
            String.format(configuration.get("icntv.correlate.input"), new Date()) };
    int i = ToolRunner.run(configuration, new GradeJob(), arrays);
    System.exit(i);
}

From source file:it.anyplace.sync.client.Main.java

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("C", "set-config", true, "set config file for s-client");
    options.addOption("c", "config", false, "dump config");
    options.addOption("sp", "set-peers", true, "set peer, or comma-separated list of peers");
    options.addOption("q", "query", true, "query directory server for device id");
    options.addOption("d", "discovery", true, "discovery local network for device id");
    options.addOption("p", "pull", true, "pull file from network");
    options.addOption("P", "push", true, "push file to network");
    options.addOption("o", "output", true, "set output file/directory");
    options.addOption("i", "input", true, "set input file/directory");
    options.addOption("lp", "list-peers", false, "list peer addresses");
    options.addOption("a", "address", true, "use this peer addresses");
    options.addOption("L", "list-remote", false, "list folder (root) content from network");
    options.addOption("I", "list-info", false, "dump folder info from network");
    options.addOption("li", "list-info", false, "list folder info from local db");
    //        options.addOption("l", "list-local", false, "list folder content from local (saved) index");
    options.addOption("s", "search", true, "search local index for <term>");
    options.addOption("D", "delete", true, "push delete to network");
    options.addOption("M", "mkdir", true, "push directory create to network");
    options.addOption("h", "help", false, "print help");
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("s-client", options);
        return;//from   www .  j av  a 2 s .co m
    }

    File configFile = cmd.hasOption("C") ? new File(cmd.getOptionValue("C"))
            : new File(System.getProperty("user.home"), ".s-client.properties");
    logger.info("using config file = {}", configFile);
    ConfigurationService configuration = ConfigurationService.newLoader().loadFrom(configFile);
    FileUtils.cleanDirectory(configuration.getTemp());
    KeystoreHandler.newLoader().loadAndStore(configuration);
    if (cmd.hasOption("c")) {
        logger.info("configuration =\n{}", configuration.newWriter().dumpToString());
    } else {
        logger.trace("configuration =\n{}", configuration.newWriter().dumpToString());
    }
    logger.debug("{}", configuration.getStorageInfo().dumpAvailableSpace());

    if (cmd.hasOption("sp")) {
        List<String> peers = Lists.newArrayList(Lists.transform(
                Arrays.<String>asList(cmd.getOptionValue("sp").split(",")), new Function<String, String>() {
                    @Override
                    public String apply(String input) {
                        return input.trim();
                    }
                }));
        logger.info("set peers = {}", peers);
        configuration.edit().setPeers(Collections.<DeviceInfo>emptyList());
        for (String peer : peers) {
            KeystoreHandler.validateDeviceId(peer);
            configuration.edit().addPeers(new DeviceInfo(peer, null));
        }
        configuration.edit().persistNow();
    }

    if (cmd.hasOption("q")) {
        String deviceId = cmd.getOptionValue("q");
        logger.info("query device id = {}", deviceId);
        List<DeviceAddress> deviceAddresses = new GlobalDiscoveryHandler(configuration).query(deviceId);
        logger.info("server response = {}", deviceAddresses);
    }
    if (cmd.hasOption("d")) {
        String deviceId = cmd.getOptionValue("d");
        logger.info("discovery device id = {}", deviceId);
        List<DeviceAddress> deviceAddresses = new LocalDiscorveryHandler(configuration).queryAndClose(deviceId);
        logger.info("local response = {}", deviceAddresses);
    }

    if (cmd.hasOption("p")) {
        String path = cmd.getOptionValue("p");
        logger.info("file path = {}", path);
        String folder = path.split(":")[0];
        path = path.split(":")[1];
        try (SyncthingClient client = new SyncthingClient(configuration);
                BlockExchangeConnectionHandler connectionHandler = client.connectToBestPeer()) {
            InputStream inputStream = client.pullFile(connectionHandler, folder, path).waitForComplete()
                    .getInputStream();
            String fileName = client.getIndexHandler().getFileInfoByPath(folder, path).getFileName();
            File file;
            if (cmd.hasOption("o")) {
                File param = new File(cmd.getOptionValue("o"));
                file = param.isDirectory() ? new File(param, fileName) : param;
            } else {
                file = new File(fileName);
            }
            FileUtils.copyInputStreamToFile(inputStream, file);
            logger.info("saved file to = {}", file.getAbsolutePath());
        }
    }
    if (cmd.hasOption("P")) {
        String path = cmd.getOptionValue("P");
        File file = new File(cmd.getOptionValue("i"));
        checkArgument(!path.startsWith("/")); //TODO check path syntax
        logger.info("file path = {}", path);
        String folder = path.split(":")[0];
        path = path.split(":")[1];
        try (SyncthingClient client = new SyncthingClient(configuration);
                BlockPusher.FileUploadObserver fileUploadObserver = client.pushFile(new FileInputStream(file),
                        folder, path)) {
            while (!fileUploadObserver.isCompleted()) {
                fileUploadObserver.waitForProgressUpdate();
                logger.debug("upload progress {}", fileUploadObserver.getProgressMessage());
            }
            logger.info("uploaded file to network");
        }
    }
    if (cmd.hasOption("D")) {
        String path = cmd.getOptionValue("D");
        String folder = path.split(":")[0];
        path = path.split(":")[1];
        logger.info("delete path = {}", path);
        try (SyncthingClient client = new SyncthingClient(configuration);
                IndexEditObserver observer = client.pushDelete(folder, path)) {
            observer.waitForComplete();
            logger.info("deleted path");
        }
    }
    if (cmd.hasOption("M")) {
        String path = cmd.getOptionValue("M");
        String folder = path.split(":")[0];
        path = path.split(":")[1];
        logger.info("dir path = {}", path);
        try (SyncthingClient client = new SyncthingClient(configuration);
                IndexEditObserver observer = client.pushDir(folder, path)) {
            observer.waitForComplete();
            logger.info("uploaded dir to network");
        }
    }
    if (cmd.hasOption("L")) {
        try (SyncthingClient client = new SyncthingClient(configuration)) {
            client.waitForRemoteIndexAquired();
            for (String folder : client.getIndexHandler().getFolderList()) {
                try (IndexBrowser indexBrowser = client.getIndexHandler().newIndexBrowserBuilder()
                        .setFolder(folder).build()) {
                    logger.info("list folder = {}", indexBrowser.getFolder());
                    for (FileInfo fileInfo : indexBrowser.listFiles()) {
                        logger.info("\t\t{} {} {}", fileInfo.getType().name().substring(0, 1),
                                fileInfo.getPath(), fileInfo.describeSize());
                    }
                }
            }
        }
    }
    if (cmd.hasOption("I")) {
        try (SyncthingClient client = new SyncthingClient(configuration)) {
            if (cmd.hasOption("a")) {
                String deviceId = cmd.getOptionValue("a").substring(0, 63),
                        address = cmd.getOptionValue("a").substring(64);
                try (BlockExchangeConnectionHandler connection = client.getConnection(
                        DeviceAddress.newBuilder().setDeviceId(deviceId).setAddress(address).build())) {
                    client.getIndexHandler().waitForRemoteIndexAquired(connection);
                }
            } else {
                client.waitForRemoteIndexAquired();
            }
            String folderInfo = "";
            for (String folder : client.getIndexHandler().getFolderList()) {
                folderInfo += "\n\t\tfolder info : " + client.getIndexHandler().getFolderInfo(folder);
                folderInfo += "\n\t\tfolder stats : "
                        + client.getIndexHandler().newFolderBrowser().getFolderStats(folder).dumpInfo() + "\n";
            }
            logger.info("folders:\n{}\n", folderInfo);
        }
    }
    if (cmd.hasOption("li")) {
        try (SyncthingClient client = new SyncthingClient(configuration)) {
            String folderInfo = "";
            for (String folder : client.getIndexHandler().getFolderList()) {
                folderInfo += "\n\t\tfolder info : " + client.getIndexHandler().getFolderInfo(folder);
                folderInfo += "\n\t\tfolder stats : "
                        + client.getIndexHandler().newFolderBrowser().getFolderStats(folder).dumpInfo() + "\n";
            }
            logger.info("folders:\n{}\n", folderInfo);
        }
    }
    if (cmd.hasOption("lp")) {
        try (SyncthingClient client = new SyncthingClient(configuration);
                DeviceAddressSupplier deviceAddressSupplier = client.getDiscoveryHandler()
                        .newDeviceAddressSupplier()) {
            String deviceAddressesStr = "";
            for (DeviceAddress deviceAddress : Lists.newArrayList(deviceAddressSupplier)) {
                deviceAddressesStr += "\n\t\t" + deviceAddress.getDeviceId() + " : "
                        + deviceAddress.getAddress();
            }
            logger.info("device addresses:\n{}\n", deviceAddressesStr);
        }
    }
    if (cmd.hasOption("s")) {
        String term = cmd.getOptionValue("s");
        try (SyncthingClient client = new SyncthingClient(configuration);
                IndexFinder indexFinder = client.getIndexHandler().newIndexFinderBuilder().build()) {
            client.waitForRemoteIndexAquired();
            logger.info("search term = '{}'", term);
            IndexFinder.SearchCompletedEvent event = indexFinder.doSearch(term);
            if (event.hasGoodResults()) {
                logger.info("search results for term = '{}' :", term);
                for (FileInfo fileInfo : event.getResultList()) {
                    logger.info("\t\t{} {} {}", fileInfo.getType().name().substring(0, 1), fileInfo.getPath(),
                            fileInfo.describeSize());
                }
            } else if (event.hasTooManyResults()) {
                logger.info("too many results found for term = '{}'", term);
            } else {
                logger.info("no result found for term = '{}'", term);
            }
        }
    }
    //        if (cmd.hasOption("l")) {
    //            String indexDump = new IndexHandler(configuration).dumpIndex();
    //            logger.info("index dump = \n\n{}\n", indexDump);
    //        }
    IOUtils.closeQuietly(configuration);
}

From source file:io.druid.server.sql.SQLRunner.java

public static void main(String[] args) throws Exception {

    Options options = new Options();
    options.addOption("h", "help", false, "help");
    options.addOption("v", false, "verbose");
    options.addOption("e", "host", true, "endpoint [hostname:port]");

    CommandLine cmd = new GnuParser().parse(options, args);

    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("SQLRunner", options);
        System.exit(2);/*  w  w  w  .  j a  v a  2  s .c o  m*/
    }

    String hostname = cmd.getOptionValue("e", "localhost:8080");
    String sql = cmd.getArgs().length > 0 ? cmd.getArgs()[0] : STATEMENT;

    ObjectMapper objectMapper = new DefaultObjectMapper();
    ObjectWriter jsonWriter = objectMapper.writerWithDefaultPrettyPrinter();

    CharStream stream = new ANTLRInputStream(sql);
    DruidSQLLexer lexer = new DruidSQLLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    DruidSQLParser parser = new DruidSQLParser(tokenStream);
    lexer.removeErrorListeners();
    parser.removeErrorListeners();

    lexer.addErrorListener(ConsoleErrorListener.INSTANCE);
    parser.addErrorListener(ConsoleErrorListener.INSTANCE);

    try {
        DruidSQLParser.QueryContext queryContext = parser.query();
        if (parser.getNumberOfSyntaxErrors() > 0)
            throw new IllegalStateException();
        //      parser.setBuildParseTree(true);
        //      System.err.println(q.toStringTree(parser));
    } catch (Exception e) {
        String msg = e.getMessage();
        if (msg != null)
            System.err.println(e);
        System.exit(1);
    }

    final Query query;
    final TypeReference typeRef;
    boolean groupBy = false;
    if (parser.groupByDimensions.isEmpty()) {
        query = Druids.newTimeseriesQueryBuilder().dataSource(parser.getDataSource())
                .aggregators(new ArrayList<AggregatorFactory>(parser.aggregators.values()))
                .postAggregators(parser.postAggregators).intervals(parser.intervals)
                .granularity(parser.granularity).filters(parser.filter).build();

        typeRef = new TypeReference<List<Result<TimeseriesResultValue>>>() {
        };
    } else {
        query = GroupByQuery.builder().setDataSource(parser.getDataSource())
                .setAggregatorSpecs(new ArrayList<AggregatorFactory>(parser.aggregators.values()))
                .setPostAggregatorSpecs(parser.postAggregators).setInterval(parser.intervals)
                .setGranularity(parser.granularity).setDimFilter(parser.filter)
                .setDimensions(new ArrayList<DimensionSpec>(parser.groupByDimensions.values())).build();

        typeRef = new TypeReference<List<Row>>() {
        };
        groupBy = true;
    }

    String queryStr = jsonWriter.writeValueAsString(query);
    if (cmd.hasOption("v"))
        System.err.println(queryStr);

    URL url = new URL(String.format("http://%s/druid/v2/?pretty", hostname));
    final URLConnection urlConnection = url.openConnection();
    urlConnection.addRequestProperty("content-type", MediaType.APPLICATION_JSON);
    urlConnection.getOutputStream().write(StringUtils.toUtf8(queryStr));
    BufferedReader stdInput = new BufferedReader(
            new InputStreamReader(urlConnection.getInputStream(), Charsets.UTF_8));

    Object res = objectMapper.readValue(stdInput, typeRef);

    Joiner tabJoiner = Joiner.on("\t");

    if (groupBy) {
        List<Row> rows = (List<Row>) res;
        Iterable<String> dimensions = Iterables.transform(parser.groupByDimensions.values(),
                new Function<DimensionSpec, String>() {
                    @Override
                    public String apply(@Nullable DimensionSpec input) {
                        return input.getOutputName();
                    }
                });

        System.out.println(
                tabJoiner.join(Iterables.concat(Lists.newArrayList("timestamp"), dimensions, parser.fields)));
        for (final Row r : rows) {
            System.out.println(tabJoiner.join(Iterables.concat(
                    Lists.newArrayList(parser.granularity.toDateTime(r.getTimestampFromEpoch())),
                    Iterables.transform(parser.groupByDimensions.values(),
                            new Function<DimensionSpec, String>() {
                                @Override
                                public String apply(@Nullable DimensionSpec input) {
                                    return Joiner.on(",").join(r.getDimension(input.getOutputName()));
                                }
                            }),
                    Iterables.transform(parser.fields, new Function<String, Object>() {
                        @Override
                        public Object apply(@Nullable String input) {
                            return r.getFloatMetric(input);
                        }
                    }))));
        }
    } else {
        List<Result<TimeseriesResultValue>> rows = (List<Result<TimeseriesResultValue>>) res;
        System.out.println(tabJoiner.join(Iterables.concat(Lists.newArrayList("timestamp"), parser.fields)));
        for (final Result<TimeseriesResultValue> r : rows) {
            System.out.println(tabJoiner.join(Iterables.concat(Lists.newArrayList(r.getTimestamp()),
                    Lists.transform(parser.fields, new Function<String, Object>() {
                        @Override
                        public Object apply(@Nullable String input) {
                            return r.getValue().getMetric(input);
                        }
                    }))));
        }
    }

    CloseQuietly.close(stdInput);
}