Java Code Examples for org.apache.flink.util.InstantiationUtil#instantiate()

The following examples show how to use org.apache.flink.util.InstantiationUtil#instantiate() . 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: KafkaShortRetentionTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void prepare() throws Exception {
	LOG.info("-------------------------------------------------------------------------");
	LOG.info("    Starting KafkaShortRetentionTestBase ");
	LOG.info("-------------------------------------------------------------------------");

	// dynamically load the implementation for the test
	Class<?> clazz = Class.forName("org.apache.flink.streaming.connectors.kafka.KafkaTestEnvironmentImpl");
	kafkaServer = (KafkaTestEnvironment) InstantiationUtil.instantiate(clazz);

	LOG.info("Starting KafkaTestBase.prepare() for Kafka " + kafkaServer.getVersion());

	if (kafkaServer.isSecureRunSupported()) {
		secureProps = kafkaServer.getSecureProperties();
	}

	Properties specificProperties = new Properties();
	specificProperties.setProperty("log.retention.hours", "0");
	specificProperties.setProperty("log.retention.minutes", "0");
	specificProperties.setProperty("log.retention.ms", "250");
	specificProperties.setProperty("log.retention.check.interval.ms", "100");
	kafkaServer.prepare(kafkaServer.createConfig().setKafkaServerProperties(specificProperties));

	standardProps = kafkaServer.getStandardProperties();
}
 
Example 2
Source File: KafkaShortRetentionTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void prepare() throws Exception {
	LOG.info("-------------------------------------------------------------------------");
	LOG.info("    Starting KafkaShortRetentionTestBase ");
	LOG.info("-------------------------------------------------------------------------");

	// dynamically load the implementation for the test
	Class<?> clazz = Class.forName("org.apache.flink.streaming.connectors.kafka.KafkaTestEnvironmentImpl");
	kafkaServer = (KafkaTestEnvironment) InstantiationUtil.instantiate(clazz);

	LOG.info("Starting KafkaTestBase.prepare() for Kafka " + kafkaServer.getVersion());

	if (kafkaServer.isSecureRunSupported()) {
		secureProps = kafkaServer.getSecureProperties();
	}

	Properties specificProperties = new Properties();
	specificProperties.setProperty("log.retention.hours", "0");
	specificProperties.setProperty("log.retention.minutes", "0");
	specificProperties.setProperty("log.retention.ms", "250");
	specificProperties.setProperty("log.retention.check.interval.ms", "100");
	kafkaServer.prepare(kafkaServer.createConfig().setKafkaServerProperties(specificProperties));

	standardProps = kafkaServer.getStandardProperties();
}
 
Example 3
Source File: HighAvailabilityServicesUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
private static HighAvailabilityServices createCustomHAServices(Configuration config, Executor executor) throws FlinkException {
	final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
	final String haServicesClassName = config.getString(HighAvailabilityOptions.HA_MODE);

	final HighAvailabilityServicesFactory highAvailabilityServicesFactory = InstantiationUtil.instantiate(
		haServicesClassName,
		HighAvailabilityServicesFactory.class,
		classLoader);

	try {
		return highAvailabilityServicesFactory.createHAServices(config, executor);
	} catch (Exception e) {
		throw new FlinkException(
			String.format(
				"Could not create the ha services from the instantiated HighAvailabilityServicesFactory %s.",
				haServicesClassName),
			e);
	}
}
 
Example 4
Source File: AbstractIterativeTask.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void reinstantiateDriver() throws Exception {
	if (this.driver instanceof ResettableDriver) {
		final ResettableDriver<?, ?> resDriver = (ResettableDriver<?, ?>) this.driver;
		resDriver.reset();
	} else {
		Class<? extends Driver<S, OT>> driverClass = this.config.getDriver();
		this.driver = InstantiationUtil.instantiate(driverClass, Driver.class);

		try {
			this.driver.setup(this);
		}
		catch (Throwable t) {
			throw new Exception("The pact driver setup for '" + this.getEnvironment().getTaskInfo().getTaskName() +
					"' , caused an error: " + t.getMessage(), t);
		}
	}
}
 
Example 5
Source File: KafkaShortRetentionTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void prepare() throws ClassNotFoundException {
	LOG.info("-------------------------------------------------------------------------");
	LOG.info("    Starting KafkaShortRetentionTestBase ");
	LOG.info("-------------------------------------------------------------------------");

	// dynamically load the implementation for the test
	Class<?> clazz = Class.forName("org.apache.flink.streaming.connectors.kafka.KafkaTestEnvironmentImpl");
	kafkaServer = (KafkaTestEnvironment) InstantiationUtil.instantiate(clazz);

	LOG.info("Starting KafkaTestBase.prepare() for Kafka " + kafkaServer.getVersion());

	if (kafkaServer.isSecureRunSupported()) {
		secureProps = kafkaServer.getSecureProperties();
	}

	Properties specificProperties = new Properties();
	specificProperties.setProperty("log.retention.hours", "0");
	specificProperties.setProperty("log.retention.minutes", "0");
	specificProperties.setProperty("log.retention.ms", "250");
	specificProperties.setProperty("log.retention.check.interval.ms", "100");
	kafkaServer.prepare(kafkaServer.createConfig().setKafkaServerProperties(specificProperties));

	standardProps = kafkaServer.getStandardProperties();
}
 
Example 6
Source File: AbstractIterativeTask.java    From flink with Apache License 2.0 6 votes vote down vote up
private void reinstantiateDriver() throws Exception {
	if (this.driver instanceof ResettableDriver) {
		final ResettableDriver<?, ?> resDriver = (ResettableDriver<?, ?>) this.driver;
		resDriver.reset();
	} else {
		Class<? extends Driver<S, OT>> driverClass = this.config.getDriver();
		this.driver = InstantiationUtil.instantiate(driverClass, Driver.class);

		try {
			this.driver.setup(this);
		}
		catch (Throwable t) {
			throw new Exception("The pact driver setup for '" + this.getEnvironment().getTaskInfo().getTaskName() +
					"' , caused an error: " + t.getMessage(), t);
		}
	}
}
 
Example 7
Source File: ElasticsearchUpsertTableSinkFactoryBase.java    From flink with Apache License 2.0 6 votes vote down vote up
private ActionRequestFailureHandler getFailureHandler(DescriptorProperties descriptorProperties) {
	final String failureHandler = descriptorProperties
		.getOptionalString(CONNECTOR_FAILURE_HANDLER)
		.orElse(DEFAULT_FAILURE_HANDLER);
	switch (failureHandler) {
		case CONNECTOR_FAILURE_HANDLER_VALUE_FAIL:
			return new NoOpFailureHandler();
		case CONNECTOR_FAILURE_HANDLER_VALUE_IGNORE:
			return new IgnoringFailureHandler();
		case CONNECTOR_FAILURE_HANDLER_VALUE_RETRY:
			return new RetryRejectedExecutionFailureHandler();
		case CONNECTOR_FAILURE_HANDLER_VALUE_CUSTOM:
			final Class<? extends ActionRequestFailureHandler> clazz = descriptorProperties
				.getClass(CONNECTOR_FAILURE_HANDLER_CLASS, ActionRequestFailureHandler.class);
			return InstantiationUtil.instantiate(clazz);
		default:
			throw new IllegalArgumentException("Unknown failure handler.");
	}
}
 
Example 8
Source File: HighAvailabilityServicesUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static HighAvailabilityServicesFactory loadCustomHighAvailabilityServicesFactory(String highAvailabilityServicesFactoryClassName) throws FlinkException {
	final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

	return InstantiationUtil.instantiate(
		highAvailabilityServicesFactoryClassName,
		HighAvailabilityServicesFactory.class,
		classLoader);
}
 
Example 9
Source File: TaskConfig.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public DataDistribution getOutputDataDistribution(int outputNum, final ClassLoader cl) throws ClassNotFoundException {
	final String className = this.config.getString(OUTPUT_DATA_DISTRIBUTION_CLASS, null);
	if (className == null) {
		return null;
	}
	
	final Class<? extends DataDistribution> clazz;
	try {
		clazz = Class.forName(className, true, cl).asSubclass(DataDistribution.class);
	} catch (ClassCastException ccex) {
		throw new CorruptConfigurationException("The class noted in the configuration as the data distribution " +
				"is no subclass of DataDistribution.");
	}
	
	final DataDistribution distribution = InstantiationUtil.instantiate(clazz, DataDistribution.class);
	
	final byte[] stateEncoded = this.config.getBytes(OUTPUT_DATA_DISTRIBUTION_PREFIX + outputNum, null);
	if (stateEncoded == null) {
		throw new CorruptConfigurationException(
					"The configuration contained the data distribution type, but no serialized state.");
	}
	
	final ByteArrayInputStream bais = new ByteArrayInputStream(stateEncoded);
	final DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(bais);
	
	try {
		distribution.read(in);
		return distribution;
	} catch (Exception ex) {
		throw new RuntimeException("The deserialization of the encoded data distribution state caused an error"
			+ (ex.getMessage() == null ? "." : ": " + ex.getMessage()), ex);
	}
}
 
Example 10
Source File: ColumnsWriter.java    From Alink with Apache License 2.0 5 votes vote down vote up
static FieldParser <?> getFieldParser(Class typeClazz) {
	Class <? extends FieldParser <?>> parserType = FieldParser.getParserForType(typeClazz);
	if (parserType == null) {
		throw new RuntimeException("No parser available for type '" + typeClazz.getName() + "'.");
	}
	return InstantiationUtil.instantiate(parserType, FieldParser.class);
}
 
Example 11
Source File: Record.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the field at the given position from the record. This method checks internally, if this instance of
 * the record has previously returned a value for this field. If so, it reuses the object, if not, it
 * creates one from the supplied class.
 *  
 * @param <T> The type of the field.
 * 
 * @param fieldNum The logical position of the field.
 * @param type The type of the field as a class. This class is used to instantiate a value object, if none had
 *             previously been instantiated. 
 * @return The field at the given position, or null, if the field was null.
 * @throws IndexOutOfBoundsException Thrown, if the field number is negative or larger or equal to the number of
 *                                   fields in this record.
 */
@SuppressWarnings("unchecked")
public <T extends Value> T getField(final int fieldNum, final Class<T> type) {
	// range check
	if (fieldNum < 0 || fieldNum >= this.numFields) {
		throw new IndexOutOfBoundsException(fieldNum + " for range [0.." + (this.numFields - 1) + "]");
	}
	
	// get offset and check for null
	final int offset = this.offsets[fieldNum];
	if (offset == NULL_INDICATOR_OFFSET) {
		return null;
	}		
	else if (offset == MODIFIED_INDICATOR_OFFSET) {
		// value that has been set is new or modified
		return (T) this.writeFields[fieldNum];
	}
	
	final int limit = offset + this.lengths[fieldNum];
	
	// get an instance, either from the instance cache or create a new one
	final Value oldField = this.readFields[fieldNum]; 
	final T field;
	if (oldField != null && oldField.getClass() == type) {
		field = (T) oldField;
	}
	else {
		field = InstantiationUtil.instantiate(type, Value.class);
		this.readFields[fieldNum] = field;
	}
	
	// deserialize
	deserialize(field, offset, limit, fieldNum);
	return field;
}
 
Example 12
Source File: CopyableValueComparator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public int compareSerialized(DataInputView firstSource, DataInputView secondSource) throws IOException {
	if (tempReference == null) {
		tempReference = InstantiationUtil.instantiate(type, CopyableValue.class);
	}
	
	reference.read(firstSource);
	tempReference.read(secondSource);
	int comp = reference.compareTo(tempReference);
	return ascendingComparison ? comp : -comp;
}
 
Example 13
Source File: UserCodeClassWrapper.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public T getUserCodeObject(Class<? super T> superClass, ClassLoader cl) {
	return InstantiationUtil.instantiate(userCodeClass, superClass);
}
 
Example 14
Source File: RecordComparator.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new comparator that compares Pact Records by the subset of fields as described
 * by the given key positions and types.
 * 
 * @param keyFields The positions of the key fields.
 * @param keyTypes The types (classes) of the key fields.
 * @param sortDirection The direction for sorting. A value of <i>true</i> indicates ascending for an attribute,
 *                  a value of <i>false</i> indicated descending. If the parameter is <i>null</i>, then
 *                  all order comparisons will assume ascending order on all fields.
 */
public RecordComparator(int[] keyFields, Class<? extends Value>[] keyTypes, boolean[] sortDirection) {
	this.keyFields = keyFields;
	
	// instantiate fields to extract keys into
	this.keyHolders = new Value[keyTypes.length];
	this.transientKeyHolders = new Value[keyTypes.length];
	for (int i = 0; i < keyTypes.length; i++) {
		if (keyTypes[i] == null) {
			throw new NullPointerException("Key type " + i + " is null.");
		}
		this.keyHolders[i] = InstantiationUtil.instantiate(keyTypes[i], Value.class);
		this.transientKeyHolders[i] = InstantiationUtil.instantiate(keyTypes[i], Value.class);
	}
	
	// set up auxiliary fields for normalized key support
	this.normalizedKeyLengths = new int[keyFields.length];
	int nKeys = 0;
	int nKeyLen = 0;
	boolean inverted = false;
	for (int i = 0; i < this.keyHolders.length; i++) {
		Value k = this.keyHolders[i];
		if (k instanceof NormalizableKey) {
			if (sortDirection != null) {
				if (sortDirection[i] && inverted) {
					break;
				} else if (i == 0 && !sortDirection[0]) {
					inverted = true;
				}
			}
			nKeys++;
			final int len = ((NormalizableKey) k).getMaxNormalizedKeyLen();
			if (len < 0) {
				throw new RuntimeException("Data type " + k.getClass().getName() + 
					" specifies an invalid length for the normalized key: " + len);
			}
			this.normalizedKeyLengths[i] = len;
			nKeyLen += this.normalizedKeyLengths[i];
			if (nKeyLen < 0) {
				nKeyLen = Integer.MAX_VALUE;
				break;
			}
		} else {
			break;
		}
	}
	this.numLeadingNormalizableKeys = nKeys;
	this.normalizableKeyPrefixLen = nKeyLen;
	
	this.temp1 = new Record();
	this.temp2 = new Record();
	
	if (sortDirection != null) {
		this.ascending = sortDirection;
	} else {
		this.ascending = new boolean[keyFields.length];
		for (int i = 0; i < this.ascending.length; i++) {
			this.ascending[i] = true;
		}
	}
}
 
Example 15
Source File: CopyableValueComparator.java    From flink with Apache License 2.0 4 votes vote down vote up
public CopyableValueComparator(boolean ascending, Class<T> type) {
	this.type = type;
	this.ascendingComparison = ascending;
	this.reference = InstantiationUtil.instantiate(type, CopyableValue.class);
}
 
Example 16
Source File: TypeSerializerSnapshotSerializationUtil.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public static <T> TypeSerializerSnapshot<T> readAndInstantiateSnapshotClass(DataInputView in, ClassLoader cl) throws IOException {
	Class<TypeSerializerSnapshot<T>> clazz =
			InstantiationUtil.resolveClassByName(in, cl, TypeSerializerSnapshot.class);

	return InstantiationUtil.instantiate(clazz);
}
 
Example 17
Source File: UserCodeClassWrapper.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public T getUserCodeObject() {
	return InstantiationUtil.instantiate(userCodeClass, Object.class);
}
 
Example 18
Source File: WritableComparator.java    From flink with Apache License 2.0 4 votes vote down vote up
private void ensureTempReferenceInstantiated() {
	if (tempReference == null) {
		tempReference = InstantiationUtil.instantiate(type, Writable.class);
	}
}
 
Example 19
Source File: WritableComparator.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private void ensureTempReferenceInstantiated() {
	if (tempReference == null) {
		tempReference = InstantiationUtil.instantiate(type, Writable.class);
	}
}
 
Example 20
Source File: WritableComparator.java    From flink with Apache License 2.0 4 votes vote down vote up
private void ensureTempReferenceInstantiated() {
	if (tempReference == null) {
		tempReference = InstantiationUtil.instantiate(type, Writable.class);
	}
}