Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PrintUtils.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.common;
16 
17 import static com.cloudera.impala.common.ByteUnits.GIGABYTE;
18 import static com.cloudera.impala.common.ByteUnits.KILOBYTE;
19 import static com.cloudera.impala.common.ByteUnits.MEGABYTE;
20 import static com.cloudera.impala.common.ByteUnits.PETABYTE;
21 import static com.cloudera.impala.common.ByteUnits.TERABYTE;
22 
23 import java.text.DecimalFormat;
24 
28 public class PrintUtils {
33  public static String printBytes(long bytes) {
34  double result = bytes;
35  // Avoid String.format() due to IMPALA-1572 which happens on JDK7 but not JDK6.
36  if (bytes >= PETABYTE) return new DecimalFormat(".00PB").format(result / PETABYTE);
37  if (bytes >= TERABYTE) return new DecimalFormat(".00TB").format(result / TERABYTE);
38  if (bytes >= GIGABYTE) return new DecimalFormat(".00GB").format(result / GIGABYTE);
39  if (bytes >= MEGABYTE) return new DecimalFormat(".00MB").format(result / MEGABYTE);
40  if (bytes >= KILOBYTE) return new DecimalFormat(".00KB").format(result / KILOBYTE);
41  return bytes + "B";
42  }
43 
44  public static String printCardinality(String prefix, long cardinality) {
45  return prefix + "cardinality=" +
46  ((cardinality != -1) ? String.valueOf(cardinality) : "unavailable");
47  }
48 
49  public static String printHosts(String prefix, long numHosts) {
50  return prefix + "hosts=" + ((numHosts != -1) ? numHosts : "unavailable");
51  }
52 
53  public static String printMemCost(String prefix, long perHostMemCost) {
54  return prefix + "per-host-mem=" +
55  ((perHostMemCost != -1) ? printBytes(perHostMemCost) : "unavailable");
56  }
57 
61  public static void printMatrix(boolean[][] matrix, int cellSpacing,
62  StringBuilder matrixStr) {
63  // Print labels.
64  for (int i = 0; i < cellSpacing; ++i) {
65  matrixStr.append(" ");
66  }
67  String formatStr = "%Xd".replace("X", String.valueOf(cellSpacing));
68  for (int i = 0; i < matrix.length; ++i) {
69  matrixStr.append(String.format(formatStr, i));
70  }
71  matrixStr.append("\n");
72 
73  // Print matrix.
74  for (int i = 0; i < matrix.length; ++i) {
75  matrixStr.append(String.format(formatStr, i));
76  for (int j = 0; j < matrix.length; ++j) {
77  int cell = (matrix[i][j]) ? 1 : 0;
78  matrixStr.append(String.format(formatStr, cell));
79  }
80  matrixStr.append("\n");
81  }
82  }
83 }
static void printMatrix(boolean[][] matrix, int cellSpacing, StringBuilder matrixStr)
Definition: PrintUtils.java:61
static String printBytes(long bytes)
Definition: PrintUtils.java:33
static String printMemCost(String prefix, long perHostMemCost)
Definition: PrintUtils.java:53
static String printHosts(String prefix, long numHosts)
Definition: PrintUtils.java:49
static String printCardinality(String prefix, long cardinality)
Definition: PrintUtils.java:44