use of java.text.NumberFormat in project flink by apache.
the class JaccardIndex method main.
public static void main(String[] args) throws Exception {
// Set up the execution environment
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
env.getConfig().enableObjectReuse();
ParameterTool parameters = ParameterTool.fromArgs(args);
env.getConfig().setGlobalJobParameters(parameters);
int little_parallelism = parameters.getInt("little_parallelism", PARALLELISM_DEFAULT);
DataSet ji;
switch(parameters.get("input", "")) {
case "csv":
{
String lineDelimiter = StringEscapeUtils.unescapeJava(parameters.get("input_line_delimiter", CsvOutputFormat.DEFAULT_LINE_DELIMITER));
String fieldDelimiter = StringEscapeUtils.unescapeJava(parameters.get("input_field_delimiter", CsvOutputFormat.DEFAULT_FIELD_DELIMITER));
GraphCsvReader reader = Graph.fromCsvReader(parameters.getRequired("input_filename"), env).ignoreCommentsEdges("#").lineDelimiterEdges(lineDelimiter).fieldDelimiterEdges(fieldDelimiter);
switch(parameters.get("type", "")) {
case "integer":
{
Graph<LongValue, NullValue, NullValue> graph = reader.keyType(LongValue.class);
if (parameters.getBoolean("simplify", false)) {
graph = graph.run(new org.apache.flink.graph.asm.simple.undirected.Simplify<LongValue, NullValue, NullValue>(false).setParallelism(little_parallelism));
}
ji = graph.run(new org.apache.flink.graph.library.similarity.JaccardIndex<LongValue, NullValue, NullValue>().setLittleParallelism(little_parallelism));
}
break;
case "string":
{
Graph<StringValue, NullValue, NullValue> graph = reader.keyType(StringValue.class);
if (parameters.getBoolean("simplify", false)) {
graph = graph.run(new org.apache.flink.graph.asm.simple.undirected.Simplify<StringValue, NullValue, NullValue>(false).setParallelism(little_parallelism));
}
ji = graph.run(new org.apache.flink.graph.library.similarity.JaccardIndex<StringValue, NullValue, NullValue>().setLittleParallelism(little_parallelism));
}
break;
default:
throw new ProgramParametrizationException(getUsage("invalid CSV type"));
}
}
break;
case "rmat":
{
int scale = parameters.getInt("scale", DEFAULT_SCALE);
int edgeFactor = parameters.getInt("edge_factor", DEFAULT_EDGE_FACTOR);
RandomGenerableFactory<JDKRandomGenerator> rnd = new JDKRandomGeneratorFactory();
long vertexCount = 1L << scale;
long edgeCount = vertexCount * edgeFactor;
Graph<LongValue, NullValue, NullValue> graph = new RMatGraph<>(env, rnd, vertexCount, edgeCount).setParallelism(little_parallelism).generate();
boolean clipAndFlip = parameters.getBoolean("clip_and_flip", DEFAULT_CLIP_AND_FLIP);
if (scale > 32) {
ji = graph.run(new Simplify<LongValue, NullValue, NullValue>(clipAndFlip).setParallelism(little_parallelism)).run(new org.apache.flink.graph.library.similarity.JaccardIndex<LongValue, NullValue, NullValue>().setLittleParallelism(little_parallelism));
} else {
ji = graph.run(new TranslateGraphIds<LongValue, IntValue, NullValue, NullValue>(new LongValueToUnsignedIntValue()).setParallelism(little_parallelism)).run(new Simplify<IntValue, NullValue, NullValue>(clipAndFlip).setParallelism(little_parallelism)).run(new org.apache.flink.graph.library.similarity.JaccardIndex<IntValue, NullValue, NullValue>().setLittleParallelism(little_parallelism));
}
}
break;
default:
throw new ProgramParametrizationException(getUsage("invalid input type"));
}
switch(parameters.get("output", "")) {
case "print":
System.out.println();
for (Object e : ji.collect()) {
Result result = (Result) e;
System.out.println(result.toPrintableString());
}
break;
case "hash":
System.out.println();
System.out.println(DataSetUtils.checksumHashCode(ji));
break;
case "csv":
String filename = parameters.getRequired("output_filename");
String lineDelimiter = StringEscapeUtils.unescapeJava(parameters.get("output_line_delimiter", CsvOutputFormat.DEFAULT_LINE_DELIMITER));
String fieldDelimiter = StringEscapeUtils.unescapeJava(parameters.get("output_field_delimiter", CsvOutputFormat.DEFAULT_FIELD_DELIMITER));
ji.writeAsCsv(filename, lineDelimiter, fieldDelimiter);
env.execute("Jaccard Index");
break;
default:
throw new ProgramParametrizationException(getUsage("invalid output type"));
}
JobExecutionResult result = env.getLastJobExecutionResult();
NumberFormat nf = NumberFormat.getInstance();
System.out.println();
System.out.println("Execution runtime: " + nf.format(result.getNetRuntime()) + " ms");
}
use of java.text.NumberFormat in project hadoop by apache.
the class StoragePolicySummary method toString.
public String toString() {
StringBuilder compliantBlocksSB = new StringBuilder();
compliantBlocksSB.append("\nBlocks satisfying the specified storage policy:");
compliantBlocksSB.append("\nStorage Policy # of blocks % of blocks\n");
StringBuilder nonCompliantBlocksSB = new StringBuilder();
Formatter compliantFormatter = new Formatter(compliantBlocksSB);
Formatter nonCompliantFormatter = new Formatter(nonCompliantBlocksSB);
NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMinimumFractionDigits(4);
percentFormat.setMaximumFractionDigits(4);
for (Map.Entry<StorageTypeAllocation, Long> storageComboCount : sortByComparator(storageComboCounts)) {
double percent = (double) storageComboCount.getValue() / (double) totalBlocks;
StorageTypeAllocation sta = storageComboCount.getKey();
if (sta.policyMatches()) {
compliantFormatter.format("%-25s %10d %20s%n", sta.getStoragePolicyDescriptor(), storageComboCount.getValue(), percentFormat.format(percent));
} else {
if (nonCompliantBlocksSB.length() == 0) {
nonCompliantBlocksSB.append("\nBlocks NOT satisfying the specified storage policy:");
nonCompliantBlocksSB.append("\nStorage Policy ");
nonCompliantBlocksSB.append("Specified Storage Policy # of blocks % of blocks\n");
}
nonCompliantFormatter.format("%-35s %-20s %10d %20s%n", sta.getStoragePolicyDescriptor(), sta.getSpecifiedStoragePolicy().getName(), storageComboCount.getValue(), percentFormat.format(percent));
}
}
if (nonCompliantBlocksSB.length() == 0) {
nonCompliantBlocksSB.append("\nAll blocks satisfy specified storage policy.\n");
}
compliantFormatter.close();
nonCompliantFormatter.close();
return compliantBlocksSB.toString() + nonCompliantBlocksSB;
}
use of java.text.NumberFormat in project NewPipe by TeamNewPipe.
the class Localization method localizeViewCount.
public static String localizeViewCount(long viewCount, Context context) {
Locale locale = getPreferredLocale(context);
Resources res = context.getResources();
String viewsString = res.getString(R.string.view_count_text);
NumberFormat nf = NumberFormat.getInstance(locale);
String formattedViewCount = nf.format(viewCount);
return String.format(viewsString, formattedViewCount);
}
use of java.text.NumberFormat in project ice by Netflix.
the class BillingFileProcessor method sendOndemandCostAlert.
private void sendOndemandCostAlert() {
if (ondemandThreshold == null || StringUtils.isEmpty(fromEmail) || StringUtils.isEmpty(alertEmails) || endMilli < lastAlertMillis() + AwsUtils.hourMillis * 24)
return;
Map<Long, Map<Ec2InstanceReservationPrice.Key, Double>> ondemandCosts = getOndemandCosts(lastAlertMillis() + AwsUtils.hourMillis);
Long maxHour = null;
double maxTotal = ondemandThreshold;
for (Long hour : ondemandCosts.keySet()) {
double total = 0;
for (Double value : ondemandCosts.get(hour).values()) total += value;
if (total > maxTotal) {
maxHour = hour;
maxTotal = total;
}
}
if (maxHour != null) {
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
String subject = String.format("Alert: Ondemand cost per hour reached $%s at %s", numberFormat.format(maxTotal), AwsUtils.dateFormatter.print(maxHour));
StringBuilder body = new StringBuilder();
body.append(String.format("Total ondemand cost $%s at %s:<br><br>", numberFormat.format(maxTotal), AwsUtils.dateFormatter.print(maxHour)));
TreeMap<Double, String> costs = Maps.newTreeMap();
for (Map.Entry<Ec2InstanceReservationPrice.Key, Double> entry : ondemandCosts.get(maxHour).entrySet()) {
costs.put(entry.getValue(), entry.getKey().region + " " + entry.getKey().usageType + ": ");
}
for (Double cost : costs.descendingKeySet()) {
if (cost > 0)
body.append(costs.get(cost)).append("$" + numberFormat.format(cost)).append("<br>");
}
body.append("<br>Please go to <a href=\"" + urlPrefix + "dashboard/reservation#usage_cost=cost&groupBy=UsageType&product=ec2_instance&operation=OndemandInstances\">Ice</a> for details.");
SendEmailRequest request = new SendEmailRequest();
request.withSource(fromEmail);
List<String> emails = Lists.newArrayList(alertEmails.split(","));
request.withDestination(new Destination(emails));
request.withMessage(new Message(new Content(subject), new Body().withHtml(new Content(body.toString()))));
AmazonSimpleEmailServiceClient emailService = AwsUtils.getAmazonSimpleEmailServiceClient();
try {
emailService.sendEmail(request);
updateLastAlertMillis(endMilli);
logger.info("updateLastAlertMillis " + endMilli);
} catch (Exception e) {
logger.error("Error in sending alert emails", e);
}
}
}
use of java.text.NumberFormat in project deeplearning4j by deeplearning4j.
the class Counter method toStringSortedByKeys.
public String toStringSortedByKeys() {
StringBuilder sb = new StringBuilder("[");
NumberFormat f = NumberFormat.getInstance();
f.setMaximumFractionDigits(5);
int numKeysPrinted = 0;
for (E element : new TreeSet<>(keySet())) {
sb.append(element.toString());
sb.append(" : ");
sb.append(f.format(getCount(element)));
if (numKeysPrinted < size() - 1)
sb.append(", ");
numKeysPrinted++;
}
if (numKeysPrinted < size())
sb.append("...");
sb.append("]");
return sb.toString();
}
Aggregations