List of usage examples for com.google.common.collect Iterables size
public static int size(Iterable<?> iterable)
From source file:cn.com.bsfit.frms.spark.PageRank.java
public static void main(String[] args) throws Exception { if (args.length < 2) { System.err.println("Usage: JavaPageRank <file> <number_of_iterations>"); System.exit(1);//from w ww . java2s.c om } showWarning(); SparkSession spark = SparkSession.builder().appName("JavaPageRank").getOrCreate(); // Loads in input file. It should be in format of: // URL neighbor URL // URL neighbor URL // URL neighbor URL // ... JavaRDD<String> lines = spark.read().textFile(args[0]).javaRDD(); // Loads all URLs from input file and initialize their neighbors. JavaPairRDD<String, Iterable<String>> links = lines.mapToPair(new PairFunction<String, String, String>() { private static final long serialVersionUID = 1L; @Override public Tuple2<String, String> call(String s) { String[] parts = SPACES.split(s); return new Tuple2<>(parts[0], parts[1]); } }).distinct().groupByKey().cache(); // Loads all URLs with other URL(s) link to from input file and // initialize ranks of them to one. JavaPairRDD<String, Double> ranks = links.mapValues(new Function<Iterable<String>, Double>() { private static final long serialVersionUID = 1L; @Override public Double call(Iterable<String> rs) { return 1.0; } }); // Calculates and updates URL ranks continuously using PageRank // algorithm. for (int current = 0; current < Integer.parseInt(args[1]); current++) { // Calculates URL contributions to the rank of other URLs. JavaPairRDD<String, Double> contribs = links.join(ranks).values() .flatMapToPair(new PairFlatMapFunction<Tuple2<Iterable<String>, Double>, String, Double>() { private static final long serialVersionUID = 1L; @Override public Iterator<Tuple2<String, Double>> call(Tuple2<Iterable<String>, Double> s) { int urlCount = Iterables.size(s._1); List<Tuple2<String, Double>> results = new ArrayList<>(); for (String n : s._1) { results.add(new Tuple2<>(n, s._2() / urlCount)); } return results.iterator(); } }); // Re-calculates URL ranks based on neighbor contributions. ranks = contribs.reduceByKey(new Sum()).mapValues(new Function<Double, Double>() { private static final long serialVersionUID = 1L; @Override public Double call(Double sum) { return 0.15 + sum * 0.85; } }); } // Collects all URL ranks and dump them to console. List<Tuple2<String, Double>> output = ranks.collect(); for (Tuple2<?, ?> tuple : output) { System.out.println(tuple._1() + " has rank: " + tuple._2() + "."); } spark.stop(); }
From source file:org.biocaddie.citationanalysis.metrics.JavaPageRank.java
public static void main(String[] args) throws Exception { if (args.length < 2) { System.err.println("Usage: JavaPageRank <file> <number_of_iterations>"); System.exit(1);//ww w .ja v a 2s . c o m } showWarning(); JavaSparkContext ctx = SparkUtils.getJavaSparkContext("JavaPageRank"); // Loads in input file. It should be in format of: // URL neighbor URL // URL neighbor URL // URL neighbor URL // ... // JavaRDD<String> lines = ctx.textFile(args[0], 1); JavaRDD<String> lines = ctx.textFile(args[0]); // Loads all URLs from input file and initialize their neighbors. JavaPairRDD<String, Iterable<String>> links = lines.mapToPair(new PairFunction<String, String, String>() { @Override public Tuple2<String, String> call(String s) { String[] parts = SPACES.split(s); return new Tuple2<String, String>(parts[0], parts[1]); } }).distinct().groupByKey().cache(); // Loads all URLs with other URL(s) link to from input file and initialize ranks of them to one. JavaPairRDD<String, Double> ranks = links.mapValues(new Function<Iterable<String>, Double>() { @Override public Double call(Iterable<String> rs) { return 1.0; } }); // Calculates and updates URL ranks continuously using PageRank algorithm. for (int current = 0; current < Integer.parseInt(args[1]); current++) { // Calculates URL contributions to the rank of other URLs. JavaPairRDD<String, Double> contribs = links.join(ranks).values() .flatMapToPair(new PairFlatMapFunction<Tuple2<Iterable<String>, Double>, String, Double>() { @Override public Iterable<Tuple2<String, Double>> call(Tuple2<Iterable<String>, Double> s) { int urlCount = Iterables.size(s._1); List<Tuple2<String, Double>> results = new ArrayList<Tuple2<String, Double>>(); for (String n : s._1) { results.add(new Tuple2<String, Double>(n, s._2() / urlCount)); } return results; } }); // Re-calculates URL ranks based on neighbor contributions. ranks = contribs.reduceByKey(new Sum()).mapValues(new Function<Double, Double>() { @Override public Double call(Double sum) { return 0.15 + sum * 0.85; } }); } // Collects all URL ranks and dump them to console. List<Tuple2<String, Double>> output = ranks.collect(); for (Tuple2<?, ?> tuple : output) { System.out.println(tuple._1() + " has rank: " + tuple._2() + "."); } ctx.stop(); }
From source file:SparkExamples.SparkPR.java
public static void main(String[] args) throws Exception { if (args.length < 2) { System.err.println("Usage: JavaPageRank <file> <number_of_iterations>"); System.exit(1);/*from w w w .j a v a2 s . c o m*/ } showWarning(); SparkConf sparkConf = new SparkConf().setAppName("SparkPR"); JavaSparkContext ctx = new JavaSparkContext(sparkConf); // Loads in input file. It should be in format of: // URL neighbor URL // URL neighbor URL // URL neighbor URL // ... long start = System.currentTimeMillis(); JavaRDD<String> lines = ctx.textFile(args[0], 1); int partition = Integer.parseInt(args[3]); // Loads all URLs from input file and initialize their neighbors. JavaPairRDD<String, Iterable<String>> links = lines.mapToPair(new PairFunction<String, String, String>() { @Override public Tuple2<String, String> call(String s) { String[] parts = SPACES.split(s); parts[0] = parts[0].toString() .replaceAll("AAAAAAAAAZAAAAAAAAAZAAAAAAAAAZAAAAAAAAAZAAAAAAAAAZAAAAAAAAAZAAAAAAAAAZ", ""); parts[1] = parts[1].toString() .replaceAll("AAAAAAAAAZAAAAAAAAAZAAAAAAAAAZAAAAAAAAAZAAAAAAAAAZAAAAAAAAAZAAAAAAAAAZ", ""); return new Tuple2<String, String>(parts[0], parts[1]); } }).groupByKey(partition).persist(StorageLevel.MEMORY_AND_DISK()); // Loads all URLs with other URL(s) link to from input file and initialize ranks of them to one. JavaPairRDD<String, Double> ranks = links.mapValues(new Function<Iterable<String>, Double>() { @Override public Double call(Iterable<String> rs) { return 1.0; } }); // Calculates and updates URL ranks continuously using PageRank algorithm. for (int current = 0; current < Integer.parseInt(args[1]); current++) { // Calculates URL contributions to the rank of other URLs. JavaPairRDD<String, Double> contribs = links.join(ranks).values() .flatMapToPair(new PairFlatMapFunction<Tuple2<Iterable<String>, Double>, String, Double>() { @Override public Iterable<Tuple2<String, Double>> call(Tuple2<Iterable<String>, Double> s) { int urlCount = Iterables.size(s._1); List<Tuple2<String, Double>> results = new ArrayList<Tuple2<String, Double>>(); for (String n : s._1) { results.add(new Tuple2<String, Double>(n, s._2() / urlCount)); } return results; } }); // Re-calculates URL ranks based on neighbor contributions. ranks = contribs.reduceByKey(new Sum()).mapValues(new Function<Double, Double>() { @Override public Double call(Double sum) { return 0.15 + sum * 0.85; } }); } ranks.foreach(p -> System.out.println(p)); long end = System.currentTimeMillis(); System.out.println("running time " + (end - start) / 1000 + "s"); String results = "running time " + (new Double((end - start) / 1000)).toString() + "s" + "input file : " + args[0] + ", iteration : " + args[1]; System.out.println(results); String outputurl = ".//results.txt"; BufferedWriter writer = new BufferedWriter(new FileWriter(outputurl, true)); writer.write(results); writer.newLine(); writer.close(); ctx.stop(); }
From source file:com.mmounirou.spotirss.SpotiRss.java
/** * @param args//from w w w. j a va2 s . c o m * @throws IOException * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws SpotifyClientException * @throws ChartRssException * @throws SpotifyException */ public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SpotifyClientException { if (args.length == 0) { System.err.println("usage : java -jar spotiboard.jar <charts-folder>"); return; } Properties connProperties = new Properties(); InputStream inStream = SpotiRss.class.getResourceAsStream("/spotify-server.properties"); try { connProperties.load(inStream); } finally { IOUtils.closeQuietly(inStream); } String host = connProperties.getProperty("host"); int port = Integer.parseInt(connProperties.getProperty("port")); String user = connProperties.getProperty("user"); final SpotifyClient spotifyClient = new SpotifyClient(host, port, user); final Map<String, Playlist> playlistsByTitle = getPlaylistsByTitle(spotifyClient); final File outputDir = new File(args[0]); outputDir.mkdirs(); TrackCache cache = new TrackCache(); try { for (String strProvider : PROVIDERS) { String providerClassName = EntryToTrackConverter.class.getPackage().getName() + "." + StringUtils.capitalize(strProvider); final EntryToTrackConverter converter = (EntryToTrackConverter) SpotiRss.class.getClassLoader() .loadClass(providerClassName).newInstance(); Iterable<String> chartsRss = getCharts(strProvider); final File resultDir = new File(outputDir, strProvider); resultDir.mkdir(); final SpotifyHrefQuery hrefQuery = new SpotifyHrefQuery(cache); Iterable<String> results = FluentIterable.from(chartsRss).transform(new Function<String, String>() { @Override @Nullable public String apply(@Nullable String chartRss) { try { long begin = System.currentTimeMillis(); ChartRss bilboardChartRss = ChartRss.getInstance(chartRss, converter); Map<Track, String> trackHrefs = hrefQuery.getTrackHrefs(bilboardChartRss.getSongs()); String strTitle = bilboardChartRss.getTitle(); File resultFile = new File(resultDir, strTitle); List<String> lines = Lists.newLinkedList(FluentIterable.from(trackHrefs.keySet()) .transform(Functions.toStringFunction())); lines.addAll(trackHrefs.values()); FileUtils.writeLines(resultFile, Charsets.UTF_8.displayName(), lines); Playlist playlist = playlistsByTitle.get(strTitle); if (playlist != null) { playlist.getTracks().clear(); playlist.getTracks().addAll(trackHrefs.values()); spotifyClient.patch(playlist); LOGGER.info(String.format("%s chart exported patched", strTitle)); } LOGGER.info(String.format("%s chart exported in %s in %d s", strTitle, resultFile.getAbsolutePath(), (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - begin))); } catch (Exception e) { LOGGER.error(String.format("fail to export %s charts", chartRss), e); } return ""; } }); // consume iterables Iterables.size(results); } } finally { cache.close(); } }
From source file:tiny.mdhbase.Client.java
/** * //from w w w . jav a 2 s .c om * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Client client = new Client("Sample", 10); try { if (args.length == 0) { showHelp(); } else if (args[0].equals("put")) { int id; if (args.length < 4) { Random idGenerator = new Random(System.nanoTime()); id = idGenerator.nextInt(); } else { id = Integer.parseInt(args[3]); } int x = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); Point p = new Point(id, x, y); client.insert(p); } else if (args[0].equals("get")) { int x = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); Iterable<Point> points = client.get(x, y); for (Point point : points) { System.out.println(point); } } else if (args[0].equals("count")) { int xmin = Integer.parseInt(args[1]); int ymin = Integer.parseInt(args[2]); int xmax = Integer.parseInt(args[3]); int ymax = Integer.parseInt(args[4]); System.out.println(String.format("Query Region: [(%d,%d), (%d,%d)]", xmin, ymin, xmax, ymax)); Iterable<Point> points = client.rangeQuery(new Range(xmin, xmax), new Range(ymin, ymax)); System.out.println(String.format("%d hits", Iterables.size(points))); } else if (args[0].equals("index")) { HTable index = new HTable("Sample_index"); System.out.println("bucket name: size"); ResultScanner entries = index.getScanner(Index.FAMILY_INFO); for (Result entry : entries) { byte[] key = entry.getRow(); int prefixLength = Bytes.toInt(entry.getValue(Index.FAMILY_INFO, Index.COLUMN_PREFIX_LENGTH)); long bucketSize = Bytes.toLong(entry.getValue(Index.FAMILY_INFO, Index.COLUMN_BUCKET_SIZE)); System.out.println(String.format("%s: %d", Utils.toString(key, prefixLength), bucketSize)); } } else if (args[0].equals("drop")) { client.close(); HBaseAdmin admin = new HBaseAdmin(HBaseConfiguration.create()); admin.disableTable("Sample_index"); admin.deleteTable("Sample_index"); admin.disableTable("Sample"); admin.deleteTable("Sample"); admin.close(); } else { showHelp(); } } finally { Closeables.closeQuietly(client); } }
From source file:com.talis.entity.db.babudb.bulk.BabuDbEntityDatabaseBuilder.java
public static void main(String[] args) throws Exception { BabuDbEntityDatabaseBuilder builder = new BabuDbEntityDatabaseBuilder(); File out = new File("/tmp/babudb/out"); File tmp = new File("/tmp/babudb/tmp"); builder.build(new FileInputStream(new File("/tmp/quads/quads.nq")), tmp, out, "db"); DatabaseManager dbm = new DatabaseManager(out, new BabuDBFactoryWrapper()); BabuDbEntityDatabase edb = new BabuDbEntityDatabase(new Marshaller(new SnappyCodec()), "db", dbm); for (Entry<Node, Iterable<Quad>> entity : edb.all()) { if (Iterables.size(entity.getValue()) == 0) { System.out.println(entity.getKey().getURI()); }/*from w w w . j a v a2 s .c o m*/ } System.out.println("Done"); }
From source file:jflowmap.clustering.Cosine.java
private static double[] toArray(Iterable<Double> v) { int size = Iterables.size(v); double[] a = new double[size]; int count = 0; for (Double d : v) { a[count++] = d;/*from www . j a va2s . c o m*/ } return a; }
From source file:org.jclouds.googlecomputeengine.domain.SlashEncodedIds.java
public static SlashEncodedIds fromSlashEncoded(String id) { Iterable<String> parts = Splitter.on('/').split(checkNotNull(id, "id")); checkArgument(Iterables.size(parts) == 2, "id must be in format firstId/secondId"); return new SlashEncodedIds(Iterables.get(parts, 0), Iterables.get(parts, 1)); }
From source file:org.apache.beam.sdk.io.gcp.spanner.MutationUtils.java
/** * Check if the mutation is a delete by a single primary key operation. * * @param m mutation//w w w.ja va 2 s . c o m * @return true if mutation is a point delete */ public static boolean isPointDelete(Mutation m) { return m.getOperation() == Mutation.Op.DELETE && Iterables.isEmpty(m.getKeySet().getRanges()) && Iterables.size(m.getKeySet().getKeys()) == 1; }
From source file:org.jclouds.elb.domain.regionscoped.RegionAndName.java
public static RegionAndName fromSlashEncoded(String name) { Iterable<String> parts = Splitter.on('/').split(checkNotNull(name, "name")); checkArgument(Iterables.size(parts) == 2, "name must be in format regionId/name"); return new RegionAndName(Iterables.get(parts, 0), Iterables.get(parts, 1)); }