use of java.text.DecimalFormat in project hive by apache.
the class GenericUDFFormatNumber method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 2) {
throw new UDFArgumentLengthException("The function FORMAT_NUMBER(X, D or F) needs two arguments.");
}
switch(arguments[0].getCategory()) {
case PRIMITIVE:
break;
default:
throw new UDFArgumentTypeException(0, "Argument 1" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DOUBLE_TYPE_NAME + "\"" + " or \"" + serdeConstants.FLOAT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DECIMAL_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" was found.");
}
switch(arguments[1].getCategory()) {
case PRIMITIVE:
break;
default:
throw new UDFArgumentTypeException(1, "Argument 2" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.STRING_TYPE_NAME + "\", but \"" + arguments[1].getTypeName() + "\" was found.");
}
PrimitiveObjectInspector xObjectInspector = (PrimitiveObjectInspector) arguments[0];
PrimitiveObjectInspector dObjectInspector = (PrimitiveObjectInspector) arguments[1];
switch(xObjectInspector.getPrimitiveCategory()) {
case VOID:
case BYTE:
case SHORT:
case INT:
case LONG:
case DOUBLE:
case FLOAT:
case DECIMAL:
break;
default:
throw new UDFArgumentTypeException(0, "Argument 1" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DOUBLE_TYPE_NAME + "\"" + " or \"" + serdeConstants.FLOAT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DECIMAL_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" was found.");
}
dType = dObjectInspector.getPrimitiveCategory();
switch(dType) {
case VOID:
case BYTE:
case SHORT:
case INT:
case LONG:
break;
case STRING:
if (!(arguments[1] instanceof ConstantObjectInspector)) {
throw new UDFArgumentTypeException(1, "Format string passed must be a constant STRING." + arguments[1].toString());
}
ConstantObjectInspector constantOI = (ConstantObjectInspector) arguments[1];
String fValue = constantOI.getWritableConstantValue().toString();
DecimalFormat dFormat = new DecimalFormat(fValue);
numberFormat.applyPattern(dFormat.toPattern());
break;
default:
throw new UDFArgumentTypeException(1, "Argument 2" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.STRING_TYPE_NAME + "\", but \"" + arguments[1].getTypeName() + "\" was found.");
}
argumentOIs = arguments;
return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
}
use of java.text.DecimalFormat in project tomcat by apache.
the class SSIFsize method formatSize.
//We try to mimic Apache here, as we do everywhere
//All the 'magic' numbers are from the util_script.c Apache source file.
protected String formatSize(long size, String format) {
String retString = "";
if (format.equalsIgnoreCase("bytes")) {
DecimalFormat decimalFormat = new DecimalFormat("#,##0");
retString = decimalFormat.format(size);
} else {
if (size == 0) {
retString = "0k";
} else if (size < ONE_KILOBYTE) {
retString = "1k";
} else if (size < ONE_MEGABYTE) {
retString = Long.toString((size + 512) / ONE_KILOBYTE);
retString += "k";
} else if (size < 99 * ONE_MEGABYTE) {
DecimalFormat decimalFormat = new DecimalFormat("0.0M");
retString = decimalFormat.format(size / (double) ONE_MEGABYTE);
} else {
retString = Long.toString((size + (529 * ONE_KILOBYTE)) / ONE_MEGABYTE);
retString += "M";
}
retString = padLeft(retString, 5);
}
return retString;
}
use of java.text.DecimalFormat in project tomcat by apache.
the class SocketNioSend method main.
public static void main(String[] args) throws Exception {
Selector selector = Selector.open();
Member mbr = new MemberImpl("localhost", 9999, 0);
ChannelData data = new ChannelData();
data.setOptions(Channel.SEND_OPTIONS_BYTE_MESSAGE);
data.setAddress(mbr);
byte[] buf = new byte[8192 * 4];
data.setMessage(new XByteBuffer(buf, false));
buf = XByteBuffer.createDataPackage(data);
int len = buf.length;
BigDecimal total = new BigDecimal((double) 0);
BigDecimal bytes = new BigDecimal((double) len);
NioSender sender = new NioSender();
sender.setDestination(mbr);
sender.setDirectBuffer(true);
sender.setSelector(selector);
sender.setTxBufSize(1024 * 1024);
sender.connect();
sender.setMessage(buf);
System.out.println("Writing to 9999");
long start = 0;
double mb = 0;
boolean first = true;
int count = 0;
DecimalFormat df = new DecimalFormat("##.00");
while (count < 100000) {
if (first) {
first = false;
start = System.currentTimeMillis();
}
sender.setMessage(buf);
int selectedKeys = 0;
try {
selectedKeys = selector.select(0);
} catch (Exception e) {
e.printStackTrace();
continue;
}
if (selectedKeys == 0) {
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = it.next();
it.remove();
try {
int readyOps = sk.readyOps();
sk.interestOps(sk.interestOps() & ~readyOps);
if (sender.process(sk, false)) {
total = total.add(bytes);
sender.reset();
sender.setMessage(buf);
mb += ((double) len) / 1024 / 1024;
if (((++count) % 10000) == 0) {
long time = System.currentTimeMillis();
double seconds = ((double) (time - start)) / 1000;
System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds, total " + mb + " MB, total " + total + " bytes.");
}
}
} catch (Throwable t) {
t.printStackTrace();
return;
}
}
selector.selectedKeys().clear();
}
System.out.println("Complete, sleeping 15 seconds");
Thread.sleep(15000);
}
use of java.text.DecimalFormat in project tomcat by apache.
the class SocketNioValidateSend method main.
public static void main(String[] args) throws Exception {
Selector selector = Selector.open();
Member mbr = new MemberImpl("localhost", 9999, 0);
byte seq = 0;
byte[] buf = new byte[50000];
Arrays.fill(buf, seq);
int len = buf.length;
BigDecimal total = new BigDecimal((double) 0);
BigDecimal bytes = new BigDecimal((double) len);
NioSender sender = new NioSender();
sender.setDestination(mbr);
sender.setDirectBuffer(true);
sender.setSelector(selector);
sender.connect();
sender.setMessage(buf);
System.out.println("Writing to 9999");
long start = 0;
double mb = 0;
boolean first = true;
int count = 0;
DecimalFormat df = new DecimalFormat("##.00");
while (count < 100000) {
if (first) {
first = false;
start = System.currentTimeMillis();
}
sender.setMessage(buf);
int selectedKeys = 0;
try {
selectedKeys = selector.select(0);
} catch (Exception e) {
e.printStackTrace();
continue;
}
if (selectedKeys == 0) {
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = it.next();
it.remove();
try {
int readyOps = sk.readyOps();
sk.interestOps(sk.interestOps() & ~readyOps);
if (sender.process(sk, false)) {
total = total.add(bytes);
sender.reset();
seq++;
Arrays.fill(buf, seq);
sender.setMessage(buf);
mb += ((double) len) / 1024 / 1024;
if (((++count) % 10000) == 0) {
long time = System.currentTimeMillis();
double seconds = ((double) (time - start)) / 1000;
System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds, total " + mb + " MB, total " + total + " bytes.");
}
}
} catch (Throwable t) {
t.printStackTrace();
return;
}
}
}
System.out.println("Complete, sleeping 15 seconds");
Thread.sleep(15000);
}
use of java.text.DecimalFormat in project tomcat by apache.
the class SocketSend method main.
public static void main(String[] args) throws Exception {
Member mbr = new MemberImpl("localhost", 9999, 0);
ChannelData data = new ChannelData();
data.setOptions(Channel.SEND_OPTIONS_BYTE_MESSAGE);
data.setAddress(mbr);
byte[] buf = new byte[8192 * 4];
data.setMessage(new XByteBuffer(buf, false));
buf = XByteBuffer.createDataPackage(data);
int len = buf.length;
System.out.println("Message size:" + len + " bytes");
BigDecimal total = new BigDecimal((double) 0);
BigDecimal bytes = new BigDecimal((double) len);
try (Socket socket = new Socket("localhost", 9999)) {
System.out.println("Writing to 9999");
OutputStream out = socket.getOutputStream();
long start = 0;
double mb = 0;
boolean first = true;
int count = 0;
DecimalFormat df = new DecimalFormat("##.00");
while (count < 1000000) {
if (first) {
first = false;
start = System.currentTimeMillis();
}
out.write(buf, 0, buf.length);
mb += ((double) buf.length) / 1024 / 1024;
total = total.add(bytes);
if (((++count) % 10000) == 0) {
long time = System.currentTimeMillis();
double seconds = ((double) (time - start)) / 1000;
System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds messages " + count + ", total " + mb + " MB, total " + total + " bytes.");
}
}
out.flush();
System.out.println("Complete, sleeping 5 seconds");
Thread.sleep(5000);
}
}
Aggregations