Example usage for com.google.common.base Preconditions checkArgument

List of usage examples for com.google.common.base Preconditions checkArgument

Introduction

In this page you can find the example usage for com.google.common.base Preconditions checkArgument.

Prototype

public static void checkArgument(boolean expression) 

Source Link

Document

Ensures the truth of an expression involving one or more parameters to the calling method.

Usage

From source file:com.cloudera.navigator.navigator_partner.extraction.HdfsMetadataExtraction.java

public static void main(String[] args) throws IOException {
    // handle arguments
    Preconditions.checkArgument(args.length >= 1);
    String configFilePath = args[0];
    String marker = null;//from   w  w w.j a  v a2  s  . c o m

    NavigatorPlugin navPlugin = NavigatorPlugin.fromConfigFile(configFilePath);
    NavApiCient client = navPlugin.getClient();

    MetadataExtractor extractor = new MetadataExtractor(client, null);

    // Run filtered examples
    getHDFSEntities(client, extractor, marker);
    addCustomTags(navPlugin, extractor, marker);

}

From source file:com.cloudera.oryx.app.traffic.TrafficUtil.java

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.err.println("usage: TrafficUtil [hosts] [requestIntervalMS] [threads] [... other args]");
        return;//from ww  w. j a  va2  s. c om
    }

    String[] hostStrings = COMMA.split(args[0]);
    Preconditions.checkArgument(hostStrings.length >= 1);
    int requestIntervalMS = Integer.parseInt(args[1]);
    Preconditions.checkArgument(requestIntervalMS >= 0);
    int numThreads = Integer.parseInt(args[2]);
    Preconditions.checkArgument(numThreads >= 1);

    String[] otherArgs = new String[args.length - 3];
    System.arraycopy(args, 3, otherArgs, 0, otherArgs.length);

    List<URI> hosts = Arrays.stream(hostStrings).map(URI::create).collect(Collectors.toList());

    int perClientRequestIntervalMS = numThreads * requestIntervalMS;

    Endpoints alsEndpoints = new Endpoints(ALSEndpoint.buildALSEndpoints());
    AtomicLong requestCount = new AtomicLong();
    AtomicLong serverErrorCount = new AtomicLong();
    AtomicLong clientErrorCount = new AtomicLong();
    AtomicLong exceptionCount = new AtomicLong();

    long start = System.currentTimeMillis();
    ExecUtils.doInParallel(numThreads, numThreads, true, i -> {
        RandomGenerator random = RandomManager.getRandom(Integer.toString(i).hashCode() ^ System.nanoTime());
        ExponentialDistribution msBetweenRequests;
        if (perClientRequestIntervalMS > 0) {
            msBetweenRequests = new ExponentialDistribution(random, perClientRequestIntervalMS);
        } else {
            msBetweenRequests = null;
        }

        ClientConfig clientConfig = new ClientConfig();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(numThreads);
        connectionManager.setDefaultMaxPerRoute(numThreads);
        clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
        clientConfig.connectorProvider(new ApacheConnectorProvider());
        Client client = ClientBuilder.newClient(clientConfig);

        try {
            while (true) {
                try {
                    WebTarget target = client.target("http://" + hosts.get(random.nextInt(hosts.size())));
                    Endpoint endpoint = alsEndpoints.chooseEndpoint(random);
                    Invocation invocation = endpoint.makeInvocation(target, otherArgs, random);

                    long startTime = System.currentTimeMillis();
                    Response response = invocation.invoke();
                    try {
                        response.readEntity(String.class);
                    } finally {
                        response.close();
                    }
                    long elapsedMS = System.currentTimeMillis() - startTime;

                    int statusCode = response.getStatusInfo().getStatusCode();
                    if (statusCode >= 400) {
                        if (statusCode >= 500) {
                            serverErrorCount.incrementAndGet();
                        } else {
                            clientErrorCount.incrementAndGet();
                        }
                    }

                    endpoint.recordTiming(elapsedMS);

                    if (requestCount.incrementAndGet() % 10000 == 0) {
                        long elapsed = System.currentTimeMillis() - start;
                        log.info("{}ms:\t{} requests\t({} client errors\t{} server errors\t{} exceptions)",
                                elapsed, requestCount.get(), clientErrorCount.get(), serverErrorCount.get(),
                                exceptionCount.get());
                        for (Endpoint e : alsEndpoints.getEndpoints()) {
                            log.info("{}", e);
                        }
                    }

                    if (msBetweenRequests != null) {
                        int desiredElapsedMS = (int) Math.round(msBetweenRequests.sample());
                        if (elapsedMS < desiredElapsedMS) {
                            Thread.sleep(desiredElapsedMS - elapsedMS);
                        }
                    }
                } catch (Exception e) {
                    exceptionCount.incrementAndGet();
                    log.warn("{}", e.getMessage());
                }
            }
        } finally {
            client.close();
        }
    });
}

From source file:org.apache.streams.youtube.provider.YoutubeChannelProvider.java

/**
 * To use from command line:/*  w w w  . ja v a2s  .  c om*/
 * <p/>
 * Supply (at least) the following required configuration in application.conf:
 * <p/>
 * youtube.oauth.pathToP12KeyFile
 * youtube.oauth.serviceAccountEmailAddress
 * youtube.apiKey
 * youtube.youtubeUsers
 * <p/>
 * Launch using:
 * <p/>
 * mvn exec:java -Dexec.mainClass=org.apache.streams.youtube.provider.YoutubeUserActivityProvider -Dexec.args="application.conf tweets.json"
 * @param args args
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {

    Preconditions.checkArgument(args.length >= 2);

    String configfile = args[0];
    String outfile = args[1];

    Config reference = ConfigFactory.load();
    File file = new File(configfile);
    assert (file.exists());
    Config testResourceConfig = ConfigFactory.parseFileAnySyntax(file,
            ConfigParseOptions.defaults().setAllowMissing(false));

    Config typesafe = testResourceConfig.withFallback(reference).resolve();

    StreamsConfiguration streamsConfiguration = StreamsConfigurator.detectConfiguration(typesafe);
    YoutubeConfiguration config = new ComponentConfigurator<>(YoutubeConfiguration.class)
            .detectConfiguration(typesafe, "youtube");
    YoutubeChannelProvider provider = new YoutubeChannelProvider(config);

    ObjectMapper mapper = StreamsJacksonMapper.getInstance();

    PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outfile)));
    provider.prepare(config);
    provider.startStream();
    do {
        Uninterruptibles.sleepUninterruptibly(streamsConfiguration.getBatchFrequencyMs(),
                TimeUnit.MILLISECONDS);
        for (StreamsDatum datum : provider.readCurrent()) {
            String json;
            try {
                if (datum.getDocument() instanceof String) {
                    json = (String) datum.getDocument();
                } else {
                    json = mapper.writeValueAsString(datum.getDocument());
                }
                outStream.println(json);
            } catch (JsonProcessingException ex) {
                System.err.println(ex.getMessage());
            }
        }
    } while (provider.isRunning());
    provider.cleanUp();
    outStream.flush();
}

From source file:org.apache.streams.facebook.provider.pagefeed.FacebookPageFeedProvider.java

/**
 * Run FacebookPageFeedProvider from command line.
 * @param args configfile outfile/*from w  w  w .ja v  a  2  s .c  om*/
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {

    Preconditions.checkArgument(args.length >= 2);

    String configfile = args[0];
    String outfile = args[1];

    Config reference = ConfigFactory.load();
    File confFile = new File(configfile);
    assert (confFile.exists());
    Config conf = ConfigFactory.parseFileAnySyntax(confFile,
            ConfigParseOptions.defaults().setAllowMissing(false));

    Config typesafe = conf.withFallback(reference).resolve();

    StreamsConfiguration streamsConfiguration = StreamsConfigurator.detectConfiguration(typesafe);
    FacebookConfiguration config = new ComponentConfigurator<>(FacebookConfiguration.class)
            .detectConfiguration(typesafe, "facebook");
    FacebookPageFeedProvider provider = new FacebookPageFeedProvider(config);

    PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outfile)));
    provider.prepare(config);
    provider.startStream();
    do {
        Uninterruptibles.sleepUninterruptibly(streamsConfiguration.getBatchFrequencyMs(),
                TimeUnit.MILLISECONDS);
        for (StreamsDatum datum : provider.readCurrent()) {
            String json;
            try {
                json = MAPPER.writeValueAsString(datum.getDocument());
                outStream.println(json);
            } catch (JsonProcessingException ex) {
                System.err.println(ex.getMessage());
            }
        }
    } while (provider.isRunning());
    provider.cleanUp();
    outStream.flush();
}

From source file:org.apache.streams.gplus.provider.GPlusUserActivityProvider.java

/**
 * Retrieve recent activity from a list of accounts.
 * @param args args// w w w  .j  a v  a  2  s.co  m
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {

    Preconditions.checkArgument(args.length >= 2);

    String configfile = args[0];
    String outfile = args[1];

    Config reference = ConfigFactory.load();
    File file = new File(configfile);
    assert (file.exists());
    Config testResourceConfig = ConfigFactory.parseFileAnySyntax(file,
            ConfigParseOptions.defaults().setAllowMissing(false));

    Config typesafe = testResourceConfig.withFallback(reference).resolve();

    StreamsConfiguration streamsConfiguration = StreamsConfigurator.detectConfiguration(typesafe);
    GPlusConfiguration config = new ComponentConfigurator<>(GPlusConfiguration.class)
            .detectConfiguration(typesafe, "gplus");
    GPlusUserActivityProvider provider = new GPlusUserActivityProvider(config);

    Gson gson = new Gson();

    PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outfile)));
    provider.prepare(config);
    provider.startStream();
    do {
        Uninterruptibles.sleepUninterruptibly(streamsConfiguration.getBatchFrequencyMs(),
                TimeUnit.MILLISECONDS);
        for (StreamsDatum datum : provider.readCurrent()) {
            String json;
            if (datum.getDocument() instanceof String) {
                json = (String) datum.getDocument();
            } else {
                json = gson.toJson(datum.getDocument());
            }
            outStream.println(json);
        }
    } while (provider.isRunning());
    provider.cleanUp();
    outStream.flush();
}

From source file:com.google.gplus.provider.GPlusUserDataProvider.java

/**
 * Retrieve current profile status for a list of accounts.
 * @param args args/*from w w w.j  a v  a 2 s  . c  o m*/
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {

    Preconditions.checkArgument(args.length >= 2);

    String configfile = args[0];
    String outfile = args[1];

    Config reference = ConfigFactory.load();
    File file = new File(configfile);
    assert (file.exists());
    Config testResourceConfig = ConfigFactory.parseFileAnySyntax(file,
            ConfigParseOptions.defaults().setAllowMissing(false));

    Config typesafe = testResourceConfig.withFallback(reference).resolve();

    StreamsConfiguration streamsConfiguration = StreamsConfigurator.detectConfiguration(typesafe);
    GPlusConfiguration config = new ComponentConfigurator<>(GPlusConfiguration.class)
            .detectConfiguration(typesafe, "gplus");
    GPlusUserDataProvider provider = new GPlusUserDataProvider(config);

    Gson gson = new Gson();

    PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outfile)));
    provider.prepare(config);
    provider.startStream();
    do {
        Uninterruptibles.sleepUninterruptibly(streamsConfiguration.getBatchFrequencyMs(),
                TimeUnit.MILLISECONDS);
        Iterator<StreamsDatum> iterator = provider.readCurrent().iterator();
        while (iterator.hasNext()) {
            StreamsDatum datum = iterator.next();
            String json;
            if (datum.getDocument() instanceof String) {
                json = (String) datum.getDocument();
            } else {
                json = gson.toJson(datum.getDocument());
            }
            outStream.println(json);
        }
    } while (provider.isRunning());
    provider.cleanUp();
    outStream.flush();
}

From source file:com.youtube.provider.YoutubeChannelProvider.java

/**
 * To use from command line:/*w w  w. j  a v  a  2  s . c  o m*/
 *
 * <p/>
 * Supply (at least) the following required configuration in application.conf:
 *
 * <p/>
 * youtube.oauth.pathToP12KeyFile
 * youtube.oauth.serviceAccountEmailAddress
 * youtube.apiKey
 * youtube.youtubeUsers
 *
 * <p/>
 * Launch using:
 *
 * <p/>
 * mvn exec:java -Dexec.mainClass=org.apache.streams.youtube.provider.YoutubeUserActivityProvider -Dexec.args="application.conf tweets.json"
 *
 * @param args args
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {

    Preconditions.checkArgument(args.length >= 2);

    String configfile = args[0];
    String outfile = args[1];

    Config reference = ConfigFactory.load();
    File file = new File(configfile);
    assert (file.exists());
    Config testResourceConfig = ConfigFactory.parseFileAnySyntax(file,
            ConfigParseOptions.defaults().setAllowMissing(false));

    Config typesafe = testResourceConfig.withFallback(reference).resolve();

    StreamsConfiguration streamsConfiguration = StreamsConfigurator.detectConfiguration(typesafe);
    YoutubeConfiguration config = new ComponentConfigurator<>(YoutubeConfiguration.class)
            .detectConfiguration(typesafe, "youtube");
    YoutubeChannelProvider provider = new YoutubeChannelProvider(config);

    ObjectMapper mapper = StreamsJacksonMapper.getInstance();

    PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outfile)));
    provider.prepare(config);
    provider.startStream();
    do {
        Uninterruptibles.sleepUninterruptibly(streamsConfiguration.getBatchFrequencyMs(),
                TimeUnit.MILLISECONDS);
        Iterator<StreamsDatum> iterator = provider.readCurrent().iterator();
        while (iterator.hasNext()) {
            StreamsDatum datum = iterator.next();
            String json;
            try {
                if (datum.getDocument() instanceof String) {
                    json = (String) datum.getDocument();
                } else {
                    json = mapper.writeValueAsString(datum.getDocument());
                }
                outStream.println(json);
            } catch (JsonProcessingException ex) {
                System.err.println(ex.getMessage());
            }
        }
    } while (provider.isRunning());
    provider.cleanUp();
    outStream.flush();
}

From source file:com.youtube.provider.YoutubeUserActivityProvider.java

/**
 * To use from command line:// w  w  w . j  ava2s . c o m
 *
 * <p/>
 * Supply (at least) the following required configuration in application.conf:
 *
 * <p/>
 * youtube.oauth.pathToP12KeyFile
 * youtube.oauth.serviceAccountEmailAddress
 * youtube.apiKey
 * youtube.youtubeUsers
 *
 * <p/>
 * Launch using:
 *
 * <p/>
 * mvn exec:java -Dexec.mainClass=org.apache.streams.youtube.provider.YoutubeUserActivityProvider -Dexec.args="application.conf tweets.json"
 *
 * @param args args
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {

    Preconditions.checkArgument(args.length >= 2);

    String configfile = args[0];
    String outfile = args[1];

    Config reference = ConfigFactory.load();
    File file = new File(configfile);
    assert (file.exists());
    Config testResourceConfig = ConfigFactory.parseFileAnySyntax(file,
            ConfigParseOptions.defaults().setAllowMissing(false));

    Config typesafe = testResourceConfig.withFallback(reference).resolve();

    StreamsConfiguration streamsConfiguration = StreamsConfigurator.detectConfiguration(typesafe);
    YoutubeConfiguration config = new ComponentConfigurator<>(YoutubeConfiguration.class)
            .detectConfiguration(typesafe, "youtube");
    YoutubeUserActivityProvider provider = new YoutubeUserActivityProvider(config);

    ObjectMapper mapper = StreamsJacksonMapper.getInstance();

    PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outfile)));
    provider.prepare(config);
    provider.startStream();
    do {
        Uninterruptibles.sleepUninterruptibly(streamsConfiguration.getBatchFrequencyMs(),
                TimeUnit.MILLISECONDS);
        Iterator<StreamsDatum> iterator = provider.readCurrent().iterator();
        while (iterator.hasNext()) {
            StreamsDatum datum = iterator.next();
            String json;
            try {
                if (datum.getDocument() instanceof String) {
                    json = (String) datum.getDocument();
                } else {
                    json = mapper.writeValueAsString(datum.getDocument());
                }
                outStream.println(json);
            } catch (JsonProcessingException ex) {
                System.err.println(ex.getMessage());
            }
        }
    } while (provider.isRunning());
    provider.cleanUp();
    outStream.flush();
}

From source file:org.apache.streams.instagram.provider.recentmedia.InstagramRecentMediaProvider.java

/**
 * To use from command line:/*from   w w  w.  jav a  2  s .c o  m*/
 *
 * <p/>
 * Supply (at least) the following required configuration in application.conf:
 *
 * <p/>
 * instagram.clientKey
 * instagram.usersInfo.authorizedTokens
 * instagram.usersInfo.users
 *
 * <p/>
 * Launch using:
 *
 * <p/>
 * mvn exec:java \
 * -Dexec.mainClass=org.apache.streams.instagram.provider.recentmedia.InstagramRecentMediaProvider \
 * -Dexec.args="application.conf media.json"
 *
 * @param args args
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {

    Preconditions.checkArgument(args.length >= 2);

    String configfile = args[0];
    String outfile = args[1];

    Config reference = ConfigFactory.load();
    File file = new File(configfile);
    assert (file.exists());
    Config conf = ConfigFactory.parseFileAnySyntax(file, ConfigParseOptions.defaults().setAllowMissing(false));

    Config typesafe = conf.withFallback(reference).resolve();

    StreamsConfiguration streamsConfiguration = StreamsConfigurator.detectConfiguration(typesafe);
    InstagramConfiguration config = new ComponentConfigurator<>(InstagramConfiguration.class)
            .detectConfiguration(typesafe, "instagram");
    InstagramRecentMediaProvider provider = new InstagramRecentMediaProvider(config);

    PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outfile)));
    provider.prepare(config);
    provider.startStream();
    do {
        Uninterruptibles.sleepUninterruptibly(streamsConfiguration.getBatchFrequencyMs(),
                TimeUnit.MILLISECONDS);
        Iterator<StreamsDatum> iterator = provider.readCurrent().iterator();
        while (iterator.hasNext()) {
            StreamsDatum datum = iterator.next();
            String json;
            try {
                json = MAPPER.writeValueAsString(datum.getDocument());
                outStream.println(json);
            } catch (JsonProcessingException ex) {
                System.err.println(ex.getMessage());
            }
        }
    } while (provider.isRunning());
    provider.cleanUp();
    outStream.flush();
}

From source file:org.apache.streams.instagram.provider.userinfo.InstagramUserInfoProvider.java

/**
 * To use from command line:/*  w  w  w.j  a va2s.c o m*/
 *
 * <p/>
 * Supply (at least) the following required configuration in application.conf:
 *
 * <p/>
 * instagram.clientKey
 * instagram.usersInfo.authorizedTokens
 * instagram.usersInfo.users
 *
 * <p/>
 * Launch using:
 *
 * <p/>
 * mvn exec:java \
 * -Dexec.mainClass=org.apache.streams.instagram.provider.userinfo.InstagramUserInfoProvider \
 * -Dexec.args="application.conf userinfo.json"
 *
 * @param args args
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {

    Preconditions.checkArgument(args.length >= 2);

    String configfile = args[0];
    String outfile = args[1];

    Config reference = ConfigFactory.load();
    File file = new File(configfile);
    assert (file.exists());
    Config conf = ConfigFactory.parseFileAnySyntax(file, ConfigParseOptions.defaults().setAllowMissing(false));

    Config typesafe = conf.withFallback(reference).resolve();

    StreamsConfiguration streamsConfiguration = StreamsConfigurator.detectConfiguration(typesafe);
    InstagramUserInformationConfiguration config = new ComponentConfigurator<>(
            InstagramUserInformationConfiguration.class).detectConfiguration(typesafe, "instagram");
    InstagramUserInfoProvider provider = new InstagramUserInfoProvider(config);

    PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outfile)));
    provider.prepare(config);
    provider.startStream();
    do {
        Uninterruptibles.sleepUninterruptibly(streamsConfiguration.getBatchFrequencyMs(),
                TimeUnit.MILLISECONDS);
        Iterator<StreamsDatum> iterator = provider.readCurrent().iterator();
        while (iterator.hasNext()) {
            StreamsDatum datum = iterator.next();
            String json;
            try {
                json = MAPPER.writeValueAsString(datum.getDocument());
                outStream.println(json);
            } catch (JsonProcessingException ex) {
                System.err.println(ex.getMessage());
            }
        }
    } while (provider.isRunning());
    provider.cleanUp();
    outStream.flush();
}