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

The following examples show how to use org.apache.flink.table.utils.EncodingUtils. 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: LogicalTypeParser.java    From flink with 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 File: AnyType.java    From flink with 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 File: RawType.java    From flink with 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 #4
Source File: RawType.java    From flink with 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 #5
Source File: SqlFunctionUtils.java    From flink with 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 File: SqlFunctionUtils.java    From flink with 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 #7
Source File: WatermarkStrategy.java    From flink with 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 #8
Source File: TimestampExtractor.java    From flink with 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 #9
Source File: UnresolvedUserDefinedType.java    From flink with 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 #10
Source File: TimestampExtractor.java    From flink with 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 #11
Source File: WatermarkStrategy.java    From flink with 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 #12
Source File: LegacyTypeInformationType.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public String asSerializableString() {
	return withNullability(
		FORMAT,
		getTypeRoot(),
		EncodingUtils.escapeSingleQuotes(TypeStringUtils.writeTypeInfo(typeInfo)));
}
 
Example #13
Source File: UnresolvedIdentifier.java    From flink with 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 #14
Source File: DescriptorProperties.java    From flink with Apache License 2.0 4 votes vote down vote up
public static String toString(String str) {
	return EncodingUtils.escapeJava(str);
}
 
Example #15
Source File: MathFunctions.java    From Alink with 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 #16
Source File: FactoryUtil.java    From flink with 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 #17
Source File: UserDefinedFunction.java    From flink with 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);
}
 
Example #18
Source File: SqlFunctionUtils.java    From flink with 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 #19
Source File: BinaryStringDataUtil.java    From flink with 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 #20
Source File: DescriptorProperties.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public static String toString(String str) {
	return EncodingUtils.escapeJava(str);
}
 
Example #21
Source File: StringFunctions.java    From Alink with 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 #22
Source File: UserDefinedFunction.java    From flink with 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 #23
Source File: DescriptorProperties.java    From flink with Apache License 2.0 4 votes vote down vote up
public static String toString(String str) {
	return EncodingUtils.escapeJava(str);
}
 
Example #24
Source File: SqlFunctionUtils.java    From flink with 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 #25
Source File: BinaryStringUtil.java    From flink with 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 #26
Source File: UserDefinedFunction.java    From Flink-CEPplus with 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);
}