Java Code Examples for org.apache.flink.table.utils.EncodingUtils

The following examples show how to use org.apache.flink.table.utils.EncodingUtils. These examples are extracted from open source projects. 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 Project: flink   Source File: LogicalTypeParser.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private LogicalType parseAnyType() {
	nextToken(TokenType.BEGIN_PARAMETER);
	nextToken(TokenType.LITERAL_STRING);
	final String className = tokenAsString();

	nextToken(TokenType.LIST_SEPARATOR);
	nextToken(TokenType.LITERAL_STRING);
	final String serializer = tokenAsString();
	nextToken(TokenType.END_PARAMETER);

	try {
		final Class<?> clazz = Class.forName(className, true, classLoader);
		final byte[] bytes = EncodingUtils.decodeBase64ToBytes(serializer);
		final DataInputDeserializer inputDeserializer = new DataInputDeserializer(bytes);
		final TypeSerializerSnapshot<?> snapshot = TypeSerializerSnapshot.readVersionedSnapshot(
			inputDeserializer,
			classLoader);
		return new AnyType(clazz, snapshot.restoreSerializer());
	} catch (Throwable t) {
		throw parsingError(
			"Unable to restore the ANY type of class '" + className + "' with " +
				"serializer snapshot '" + serializer + "'.", t);
	}
}
 
Example 2
Source Project: flink   Source File: AnyType.java    License: Apache License 2.0 6 votes vote down vote up
private String getOrCreateSerializerString() {
	if (serializerString == null) {
		final DataOutputSerializer outputSerializer = new DataOutputSerializer(128);
		try {
			TypeSerializerSnapshot.writeVersionedSnapshot(outputSerializer, serializer.snapshotConfiguration());
			serializerString = EncodingUtils.encodeBytesToBase64(outputSerializer.getCopyOfBuffer());
			return serializerString;
		} catch (Exception e) {
			throw new TableException(String.format(
				"Unable to generate a string representation of the serializer snapshot of '%s' " +
					"describing the class '%s' for the ANY type.",
				serializer.getClass().getName(),
				clazz.toString()), e);
		}
	}
	return serializerString;
}
 
Example 3
Source Project: flink   Source File: RawType.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Restores a raw type from the components of a serialized string representation.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public static RawType<?> restore(
		ClassLoader classLoader,
		String className,
		String serializerString) {
	try {
		final Class<?> clazz = Class.forName(className, true, classLoader);
		final byte[] bytes = EncodingUtils.decodeBase64ToBytes(serializerString);
		final DataInputDeserializer inputDeserializer = new DataInputDeserializer(bytes);
		final TypeSerializerSnapshot<?> snapshot = TypeSerializerSnapshot.readVersionedSnapshot(
			inputDeserializer,
			classLoader);
		return (RawType<?>) new RawType(clazz, snapshot.restoreSerializer());
	} catch (Throwable t) {
		throw new ValidationException(
			String.format(
				"Unable to restore the RAW type of class '%s' with serializer snapshot '%s'.",
				className,
				serializerString),
			t);
	}
}
 
Example 4
Source Project: flink   Source File: RawType.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the serialized {@link TypeSerializerSnapshot} in Base64 encoding of this raw type.
 */
public String getSerializerString() {
	if (serializerString == null) {
		final DataOutputSerializer outputSerializer = new DataOutputSerializer(128);
		try {
			TypeSerializerSnapshot.writeVersionedSnapshot(outputSerializer, serializer.snapshotConfiguration());
			serializerString = EncodingUtils.encodeBytesToBase64(outputSerializer.getCopyOfBuffer());
			return serializerString;
		} catch (Exception e) {
			throw new TableException(String.format(
				"Unable to generate a string representation of the serializer snapshot of '%s' " +
					"describing the class '%s' for the RAW type.",
				serializer.getClass().getName(),
				clazz.toString()), e);
		}
	}
	return serializerString;
}
 
Example 5
Source Project: flink   Source File: SqlFunctionUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the hash value of a given string.
 *
 * @param algorithm    message digest algorithm.
 * @param str          string to hash.
 * @param charsetName  charset of string.
 * @return           hash value of string.
 */
public static String hash(String algorithm, String str, String charsetName) {
	try {
		byte[] digest = MessageDigest
			.getInstance(algorithm)
			.digest(strToBytesWithCharset(str, charsetName));
		return EncodingUtils.hex(digest);
	} catch (NoSuchAlgorithmException e) {
		throw new IllegalArgumentException("Unsupported algorithm: " + algorithm, e);
	}
}
 
Example 6
Source Project: flink   Source File: UnresolvedUserDefinedType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String asSummaryString() {
	final String path = Stream.of(catalog, database, typeIdentifier)
		.filter(Objects::nonNull)
		.map(EncodingUtils::escapeIdentifier)
		.collect(Collectors.joining("."));
	return withNullability(path);
}
 
Example 7
Source Project: flink   Source File: TimestampExtractor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This method is a default implementation that uses java serialization and it is discouraged.
 * All implementation should provide a more specific set of properties.
 */
@Override
public Map<String, String> toProperties() {
	Map<String, String> properties = new HashMap<>();
	properties.put(Rowtime.ROWTIME_TIMESTAMPS_TYPE, Rowtime.ROWTIME_TIMESTAMPS_TYPE_VALUE_CUSTOM);
	properties.put(Rowtime.ROWTIME_TIMESTAMPS_CLASS, this.getClass().getName());
	properties.put(Rowtime.ROWTIME_TIMESTAMPS_SERIALIZED, EncodingUtils.encodeObjectToString(this));
	return properties;
}
 
Example 8
Source Project: flink   Source File: WatermarkStrategy.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This method is a default implementation that uses java serialization and it is discouraged.
 * All implementation should provide a more specific set of properties.
 */
@Override
public Map<String, String> toProperties() {
	Map<String, String> properties = new HashMap<>();
	properties.put(Rowtime.ROWTIME_WATERMARKS_TYPE, Rowtime.ROWTIME_WATERMARKS_TYPE_VALUE_CUSTOM);
	properties.put(Rowtime.ROWTIME_WATERMARKS_CLASS, this.getClass().getName());
	properties.put(Rowtime.ROWTIME_WATERMARKS_SERIALIZED, EncodingUtils.encodeObjectToString(this));
	return properties;
}
 
Example 9
Source Project: flink   Source File: SqlFunctionUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the hash value of a given string.
 *
 * @param algorithm    message digest algorithm.
 * @param str          string to hash.
 * @param charsetName  charset of string.
 * @return           hash value of string.
 */
public static String hash(String algorithm, String str, String charsetName) {
	try {
		byte[] digest = MessageDigest
			.getInstance(algorithm)
			.digest(strToBytesWithCharset(str, charsetName));
		return EncodingUtils.hex(digest);
	} catch (NoSuchAlgorithmException e) {
		throw new IllegalArgumentException("Unsupported algorithm: " + algorithm, e);
	}
}
 
Example 10
Source Project: flink   Source File: UnresolvedIdentifier.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a string that summarizes this instance for printing to a console or log.
 */
public String asSummaryString() {
	return Stream.of(catalogName, databaseName, objectName)
		.filter(Objects::nonNull)
		.map(EncodingUtils::escapeIdentifier)
		.collect(Collectors.joining("."));
}
 
Example 11
Source Project: flink   Source File: LegacyTypeInformationType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String asSerializableString() {
	return withNullability(
		FORMAT,
		getTypeRoot(),
		EncodingUtils.escapeSingleQuotes(TypeStringUtils.writeTypeInfo(typeInfo)));
}
 
Example 12
Source Project: flink   Source File: TimestampExtractor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This method is a default implementation that uses java serialization and it is discouraged.
 * All implementation should provide a more specific set of properties.
 */
@Override
public Map<String, String> toProperties() {
	Map<String, String> properties = new HashMap<>();
	properties.put(Rowtime.ROWTIME_TIMESTAMPS_TYPE, Rowtime.ROWTIME_TIMESTAMPS_TYPE_VALUE_CUSTOM);
	properties.put(Rowtime.ROWTIME_TIMESTAMPS_CLASS, this.getClass().getName());
	properties.put(Rowtime.ROWTIME_TIMESTAMPS_SERIALIZED, EncodingUtils.encodeObjectToString(this));
	return properties;
}
 
Example 13
Source Project: flink   Source File: WatermarkStrategy.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This method is a default implementation that uses java serialization and it is discouraged.
 * All implementation should provide a more specific set of properties.
 */
@Override
public Map<String, String> toProperties() {
	Map<String, String> properties = new HashMap<>();
	properties.put(Rowtime.ROWTIME_WATERMARKS_TYPE, Rowtime.ROWTIME_WATERMARKS_TYPE_VALUE_CUSTOM);
	properties.put(Rowtime.ROWTIME_WATERMARKS_CLASS, this.getClass().getName());
	properties.put(Rowtime.ROWTIME_WATERMARKS_SERIALIZED, EncodingUtils.encodeObjectToString(this));
	return properties;
}
 
Example 14
Source Project: Flink-CEPplus   Source File: DescriptorProperties.java    License: Apache License 2.0 4 votes vote down vote up
public static String toString(String str) {
	return EncodingUtils.escapeJava(str);
}
 
Example 15
Source Project: Flink-CEPplus   Source File: UserDefinedFunction.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns a unique, serialized representation for this function.
 */
public final String functionIdentifier() {
	final String md5 = EncodingUtils.hex(EncodingUtils.md5(EncodingUtils.encodeObjectToString(this)));
	return getClass().getCanonicalName().replace('.', '$').concat("$").concat(md5);
}
 
Example 16
Source Project: flink   Source File: BinaryStringUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Calculate the hash value of a given string use {@link MessageDigest}.
 */
public static BinaryString hash(BinaryString str, MessageDigest md) {
	return fromString(EncodingUtils.hex(md.digest(str.getBytes())));
}
 
Example 17
Source Project: flink   Source File: SqlFunctionUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns the hex string of a string argument.
 */
public static String hex(String x) {
	return EncodingUtils.hex(x.getBytes(StandardCharsets.UTF_8)).toUpperCase();
}
 
Example 18
Source Project: flink   Source File: DescriptorProperties.java    License: Apache License 2.0 4 votes vote down vote up
public static String toString(String str) {
	return EncodingUtils.escapeJava(str);
}
 
Example 19
Source Project: flink   Source File: UserDefinedFunction.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns a unique, serialized representation for this function.
 */
public final String functionIdentifier() {
	final String md5 = EncodingUtils.hex(EncodingUtils.md5(EncodingUtils.encodeObjectToString(this)));
	return getClass().getCanonicalName().replace('.', '$').concat("$").concat(md5);
}
 
Example 20
Source Project: Alink   Source File: StringFunctions.java    License: Apache License 2.0 4 votes vote down vote up
public static String hash(String str, MessageDigest md) {
    return EncodingUtils.hex(md.digest(str.getBytes(StandardCharsets.UTF_8)));
}
 
Example 21
Source Project: Alink   Source File: MathFunctions.java    License: Apache License 2.0 4 votes vote down vote up
public static String hex(String x) {
    return EncodingUtils.hex(x.getBytes(StandardCharsets.UTF_8)).toUpperCase();
}
 
Example 22
Source Project: flink   Source File: BinaryStringDataUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Calculate the hash value of a given string use {@link MessageDigest}.
 */
public static BinaryStringData hash(BinaryStringData str, MessageDigest md) {
	return fromString(EncodingUtils.hex(md.digest(str.toBytes())));
}
 
Example 23
Source Project: flink   Source File: SqlFunctionUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns the hex string of a string argument.
 */
public static String hex(String x) {
	return EncodingUtils.hex(x.getBytes(StandardCharsets.UTF_8)).toUpperCase();
}
 
Example 24
Source Project: flink   Source File: DescriptorProperties.java    License: Apache License 2.0 4 votes vote down vote up
public static String toString(String str) {
	return EncodingUtils.escapeJava(str);
}
 
Example 25
Source Project: flink   Source File: FactoryUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static String stringifyOption(String key, String value) {
	return String.format(
		"'%s'='%s'",
		EncodingUtils.escapeSingleQuotes(key),
		EncodingUtils.escapeSingleQuotes(value));
}
 
Example 26
Source Project: flink   Source File: UserDefinedFunction.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns a unique, serialized representation for this function.
 */
public final String functionIdentifier() {
	final String md5 = EncodingUtils.hex(EncodingUtils.md5(EncodingUtils.encodeObjectToString(this)));
	return getClass().getName().replace('.', '$').concat("$").concat(md5);
}