Java Code Examples for org.apache.flink.configuration.ConfigConstants#DEFAULT_CHARSET

The following examples show how to use org.apache.flink.configuration.ConfigConstants#DEFAULT_CHARSET . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigIntParser.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, BigInteger reusable) {
	final int endPos = nextStringEndPos(bytes, startPos, limit, delimiter);
	if (endPos < 0) {
		return -1;
	}

	if (endPos > startPos &&
			(Character.isWhitespace(bytes[startPos]) || Character.isWhitespace(bytes[(endPos - 1)]))) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_ILLEGAL_CHARACTER);
		return -1;
	}

	String str = new String(bytes, startPos, endPos - startPos, ConfigConstants.DEFAULT_CHARSET);
	try {
		this.result = new BigInteger(str);
		return (endPos == limit) ? limit : endPos + delimiter.length;
	} catch (NumberFormatException e) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_FORMAT_ERROR);
		return -1;
	}
}
 
Example 2
Source File: SqlTimestampParser.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, Timestamp reusable) {
	final int endPos = nextStringEndPos(bytes, startPos, limit, delimiter);
	if (endPos < 0) {
		return -1;
	}

	if (endPos > startPos &&
			(Character.isWhitespace(bytes[startPos]) || Character.isWhitespace(bytes[(endPos - 1)]))) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_ILLEGAL_CHARACTER);
		return -1;
	}

	String str = new String(bytes, startPos, endPos - startPos, ConfigConstants.DEFAULT_CHARSET);
	try {
		this.result = Timestamp.valueOf(str);
		return (endPos == limit) ? limit : endPos + delimiter.length;
	} catch (IllegalArgumentException e) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_FORMAT_ERROR);
		return -1;
	}
}
 
Example 3
Source File: DataSourceTaskTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Record readRecord(Record target, byte[] record, int offset, int numBytes) {
	try {
		Thread.sleep(100);
	}
	catch (InterruptedException e) {
		return null;
	}
	
	String line = new String(record, offset, numBytes, ConfigConstants.DEFAULT_CHARSET);
	
	try {
		this.key.setValue(Integer.parseInt(line.substring(0,line.indexOf("_"))));
		this.value.setValue(Integer.parseInt(line.substring(line.indexOf("_")+1,line.length())));
	}
	catch(RuntimeException re) {
		return null;
	}
	
	target.setField(0, this.key);
	target.setField(1, this.value);
	return target;
}
 
Example 4
Source File: SqlTimestampParser.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, Timestamp reusable) {
	final int endPos = nextStringEndPos(bytes, startPos, limit, delimiter);
	if (endPos < 0) {
		return -1;
	}

	if (endPos > startPos &&
			(Character.isWhitespace(bytes[startPos]) || Character.isWhitespace(bytes[(endPos - 1)]))) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_ILLEGAL_CHARACTER);
		return -1;
	}

	String str = new String(bytes, startPos, endPos - startPos, ConfigConstants.DEFAULT_CHARSET);
	try {
		this.result = Timestamp.valueOf(str);
		return (endPos == limit) ? limit : endPos + delimiter.length;
	} catch (IllegalArgumentException e) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_FORMAT_ERROR);
		return -1;
	}
}
 
Example 5
Source File: FloatValueParser.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, FloatValue reusable) {
	final int endPos = nextStringEndPos(bytes, startPos, limit, delimiter);
	if (endPos < 0) {
		return -1;
	}

	if (endPos > startPos &&
			(Character.isWhitespace(bytes[startPos]) || Character.isWhitespace(bytes[endPos - 1]))) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_ILLEGAL_CHARACTER);
		return -1;
	}

	String str = new String(bytes, startPos, endPos - startPos, ConfigConstants.DEFAULT_CHARSET);
	try {
		float value = Float.parseFloat(str);
		reusable.setValue(value);
		this.result = reusable;
		return (endPos == limit) ? limit : endPos + delimiter.length;
	}
	catch (NumberFormatException e) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_FORMAT_ERROR);
		return -1;
	}
}
 
Example 6
Source File: DataSourceTaskTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Record readRecord(Record target, byte[] record, int offset, int numBytes) {
	
	String line = new String(record, offset, numBytes, ConfigConstants.DEFAULT_CHARSET);
	
	try {
		this.key.setValue(Integer.parseInt(line.substring(0,line.indexOf("_"))));
		this.value.setValue(Integer.parseInt(line.substring(line.indexOf("_")+1,line.length())));
	}
	catch(RuntimeException re) {
		return null;
	}
	
	target.setField(0, this.key);
	target.setField(1, this.value);
	return target;
}
 
Example 7
Source File: SqlTimeParser.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, Time reusable) {
	final int endPos = nextStringEndPos(bytes, startPos, limit, delimiter);
	if (endPos < 0) {
		return -1;
	}

	String str = new String(bytes, startPos, endPos - startPos, ConfigConstants.DEFAULT_CHARSET);
	try {
		this.result = Time.valueOf(str);
		return (endPos == limit) ? limit : endPos + delimiter.length;
	} catch (IllegalArgumentException e) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_FORMAT_ERROR);
		return -1;
	}
}
 
Example 8
Source File: StatsDReporterTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
	while (running) {
		try {
			byte[] buffer = new byte[1024];

			DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

			socket.receive(packet);

			String line = new String(packet.getData(), 0, packet.getLength(), ConfigConstants.DEFAULT_CHARSET);

			lines.put(line, obj);

			synchronized (lines) {
				lines.notifyAll();
			}
		} catch (IOException ex) {
			// ignore the exceptions
		}
	}
}
 
Example 9
Source File: SqlTimeParser.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, Time reusable) {
	final int endPos = nextStringEndPos(bytes, startPos, limit, delimiter);
	if (endPos < 0) {
		return -1;
	}

	String str = new String(bytes, startPos, endPos - startPos, ConfigConstants.DEFAULT_CHARSET);
	try {
		this.result = Time.valueOf(str);
		return (endPos == limit) ? limit : endPos + delimiter.length;
	} catch (IllegalArgumentException e) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_FORMAT_ERROR);
		return -1;
	}
}
 
Example 10
Source File: SqlTimeParser.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, Time reusable) {
	final int endPos = nextStringEndPos(bytes, startPos, limit, delimiter);
	if (endPos < 0) {
		return -1;
	}

	String str = new String(bytes, startPos, endPos - startPos, ConfigConstants.DEFAULT_CHARSET);
	try {
		this.result = Time.valueOf(str);
		return (endPos == limit) ? limit : endPos + delimiter.length;
	} catch (IllegalArgumentException e) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_FORMAT_ERROR);
		return -1;
	}
}
 
Example 11
Source File: SqlDateParser.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, Date reusable) {
	final int endPos = nextStringEndPos(bytes, startPos, limit, delimiter);
	if (endPos < 0) {
		return -1;
	}

	if (endPos > startPos &&
			(Character.isWhitespace(bytes[startPos]) || Character.isWhitespace(bytes[(endPos - 1)]))) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_ILLEGAL_CHARACTER);
		return -1;
	}

	String str = new String(bytes, startPos, endPos - startPos, ConfigConstants.DEFAULT_CHARSET);
	try {
		this.result = Date.valueOf(str);
		return (endPos == limit) ? limit : endPos + delimiter.length;
	} catch (IllegalArgumentException e) {
		setErrorState(ParseErrorState.NUMERIC_VALUE_FORMAT_ERROR);
		return -1;
	}
}
 
Example 12
Source File: AbstractServerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TestMessage deserializeMessage(ByteBuf buf) {
	int length = buf.readInt();
	String message = "";
	if (length > 0) {
		byte[] name = new byte[length];
		buf.readBytes(name);
		message = new String(name, ConfigConstants.DEFAULT_CHARSET);
	}
	return new TestMessage(message);
}
 
Example 13
Source File: ZooKeeperStringSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserialize(byte[] bytes) {
	if (bytes == null) {
		return null;
	}
	else {
		return new String(bytes, ConfigConstants.DEFAULT_CHARSET);
	}
}
 
Example 14
Source File: StringArrayWritable.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void readFields(DataInput in) throws IOException {
	this.array = new String[in.readInt()];

	for (int i = 0; i < this.array.length; i++) {
		byte[] b = new byte[in.readInt()];
		in.readFully(b);
		this.array[i] = new String(b, ConfigConstants.DEFAULT_CHARSET);
	}
}
 
Example 15
Source File: RMQSourceTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public String deserialize(byte[] message) throws IOException {
	try {
		// wait a bit to not cause too much cpu load
		Thread.sleep(1);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	return new String(message, ConfigConstants.DEFAULT_CHARSET);
}
 
Example 16
Source File: DelimitedInputFormatTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public String readRecord(String reuse, byte[] bytes, int offset, int numBytes) {
	return new String(bytes, offset, numBytes, ConfigConstants.DEFAULT_CHARSET);
}
 
Example 17
Source File: StringDeserializerMap.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public String map(byte[] value) throws Exception {
	//discard type byte and size
	return new String(value, 5, value.length - 5, ConfigConstants.DEFAULT_CHARSET);
}
 
Example 18
Source File: DoubleParser.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Static utility to parse a field of type double from a byte sequence that represents text 
 * characters
 * (such as when read from a file stream).
 *
 * @param bytes     The bytes containing the text data that should be parsed.
 * @param startPos  The offset to start the parsing.
 * @param length    The length of the byte sequence (counting from the offset).
 * @param delimiter The delimiter that terminates the field.
 * @return The parsed value.
 * @throws IllegalArgumentException Thrown when the value cannot be parsed because the text
 * represents not a correct number.
 */
public static final double parseField(byte[] bytes, int startPos, int length, char delimiter) {
	final int limitedLen = nextStringLength(bytes, startPos, length, delimiter);

	if (limitedLen > 0 &&
			(Character.isWhitespace(bytes[startPos]) || Character.isWhitespace(bytes[startPos + limitedLen - 1]))) {
		throw new NumberFormatException("There is leading or trailing whitespace in the numeric field.");
	}

	final String str = new String(bytes, startPos, limitedLen, ConfigConstants.DEFAULT_CHARSET);
	return Double.parseDouble(str);
}
 
Example 19
Source File: DoubleParser.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
/**
 * Static utility to parse a field of type double from a byte sequence that represents text 
 * characters
 * (such as when read from a file stream).
 *
 * @param bytes     The bytes containing the text data that should be parsed.
 * @param startPos  The offset to start the parsing.
 * @param length    The length of the byte sequence (counting from the offset).
 * @param delimiter The delimiter that terminates the field.
 * @return The parsed value.
 * @throws IllegalArgumentException Thrown when the value cannot be parsed because the text
 * represents not a correct number.
 */
public static final double parseField(byte[] bytes, int startPos, int length, char delimiter) {
	final int limitedLen = nextStringLength(bytes, startPos, length, delimiter);

	if (limitedLen > 0 &&
			(Character.isWhitespace(bytes[startPos]) || Character.isWhitespace(bytes[startPos + limitedLen - 1]))) {
		throw new NumberFormatException("There is leading or trailing whitespace in the numeric field.");
	}

	final String str = new String(bytes, startPos, limitedLen, ConfigConstants.DEFAULT_CHARSET);
	return Double.parseDouble(str);
}
 
Example 20
Source File: SqlTimestampParser.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
/**
 * Static utility to parse a field of type Timestamp from a byte sequence that represents text
 * characters
 * (such as when read from a file stream).
 *
 * @param bytes     The bytes containing the text data that should be parsed.
 * @param startPos  The offset to start the parsing.
 * @param length    The length of the byte sequence (counting from the offset).
 * @param delimiter The delimiter that terminates the field.
 * @return The parsed value.
 * @throws IllegalArgumentException Thrown when the value cannot be parsed because the text
 * represents not a correct number.
 */
public static final Timestamp parseField(byte[] bytes, int startPos, int length, char delimiter) {
	final int limitedLen = nextStringLength(bytes, startPos, length, delimiter);

	if (limitedLen > 0 &&
			(Character.isWhitespace(bytes[startPos]) || Character.isWhitespace(bytes[startPos + limitedLen - 1]))) {
		throw new NumberFormatException("There is leading or trailing whitespace in the numeric field.");
	}

	final String str = new String(bytes, startPos, limitedLen, ConfigConstants.DEFAULT_CHARSET);
	return Timestamp.valueOf(str);
}