Java Code Examples for org.apache.flink.types.DoubleValue

The following examples show how to use org.apache.flink.types.DoubleValue. 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: TypeExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionDependingPartialOnInput2() {
	RichMapFunction<DoubleValue, ?> function = new OneAppender<DoubleValue>();

	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, new ValueTypeInfo<DoubleValue>(DoubleValue.class));

	Assert.assertTrue(ti.isTupleType());
	Assert.assertEquals(2, ti.getArity());
	TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti;

	Assert.assertTrue(tti.getTypeAt(0) instanceof ValueTypeInfo<?>);
	ValueTypeInfo<?> vti = (ValueTypeInfo<?>) tti.getTypeAt(0);
	Assert.assertEquals(DoubleValue.class, vti.getTypeClass());
	
	Assert.assertTrue(tti.getTypeAt(1).isBasicType());
	Assert.assertEquals(Integer.class , tti.getTypeAt(1).getTypeClass());
}
 
Example 2
/**
 * Helper method for summarizing a list of values.
 *
 * <p>This method breaks the rule of "testing only one thing" by aggregating and combining
 * a bunch of different ways.
 */
protected NumericColumnSummary<Double> summarize(Double... values) {

	DoubleValue[] doubleValues = new DoubleValue[values.length];
	for (int i = 0; i < values.length; i++) {
		if (values[i] != null) {
			doubleValues[i] = new DoubleValue(values[i]);
		}
	}

	return new AggregateCombineHarness<DoubleValue, NumericColumnSummary<Double>, ValueSummaryAggregator.DoubleValueSummaryAggregator>() {

		@Override
		protected void compareResults(NumericColumnSummary<Double> result1, NumericColumnSummary<Double> result2) {
			Assert.assertEquals(result1.getMin(), result2.getMin(), 0.0);
			Assert.assertEquals(result1.getMax(), result2.getMax(), 0.0);
			Assert.assertEquals(result1.getMean(), result2.getMean(), 1e-12d);
			Assert.assertEquals(result1.getVariance(), result2.getVariance(), 1e-9d);
			Assert.assertEquals(result1.getStandardDeviation(), result2.getStandardDeviation(), 1e-12d);
		}

	}.summarize(doubleValues);
}
 
Example 3
Source Project: flink   Source File: DoubleValueSummaryAggregatorTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for summarizing a list of values.
 *
 * <p>This method breaks the rule of "testing only one thing" by aggregating and combining
 * a bunch of different ways.
 */
protected NumericColumnSummary<Double> summarize(Double... values) {

	DoubleValue[] doubleValues = new DoubleValue[values.length];
	for (int i = 0; i < values.length; i++) {
		if (values[i] != null) {
			doubleValues[i] = new DoubleValue(values[i]);
		}
	}

	return new AggregateCombineHarness<DoubleValue, NumericColumnSummary<Double>, ValueSummaryAggregator.DoubleValueSummaryAggregator>() {

		@Override
		protected void compareResults(NumericColumnSummary<Double> result1, NumericColumnSummary<Double> result2) {
			Assert.assertEquals(result1.getMin(), result2.getMin(), 0.0);
			Assert.assertEquals(result1.getMax(), result2.getMax(), 0.0);
			Assert.assertEquals(result1.getMean(), result2.getMean(), 1e-12d);
			Assert.assertEquals(result1.getVariance(), result2.getVariance(), 1e-9d);
			Assert.assertEquals(result1.getStandardDeviation(), result2.getStandardDeviation(), 1e-12d);
		}

	}.summarize(doubleValues);
}
 
Example 4
Source Project: Flink-CEPplus   Source File: DoubleValueParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, DoubleValue 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 {
		double value = Double.parseDouble(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 5
Source Project: flink   Source File: PageRank.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void coGroup(Iterable<Tuple2<T, DoubleValue>> vertex, Iterable<Edge<T, LongValue>> edges, Collector<Tuple2<T, DoubleValue>> out)
		throws Exception {
	Iterator<Edge<T, LongValue>> edgeIterator = edges.iterator();

	if (edgeIterator.hasNext()) {
		Edge<T, LongValue> edge = edgeIterator.next();

		output.f0 = edge.f1;
		output.f1.setValue(vertex.iterator().next().f1.getValue() / edge.f2.getValue());
		out.collect(output);

		while (edgeIterator.hasNext()) {
			edge = edgeIterator.next();
			output.f0 = edge.f1;
			out.collect(output);
		}
	}
}
 
Example 6
Source Project: Flink-CEPplus   Source File: TypeExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionDependingPartialOnInput2() {
	RichMapFunction<DoubleValue, ?> function = new OneAppender<DoubleValue>();

	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, new ValueTypeInfo<DoubleValue>(DoubleValue.class));

	Assert.assertTrue(ti.isTupleType());
	Assert.assertEquals(2, ti.getArity());
	TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti;

	Assert.assertTrue(tti.getTypeAt(0) instanceof ValueTypeInfo<?>);
	ValueTypeInfo<?> vti = (ValueTypeInfo<?>) tti.getTypeAt(0);
	Assert.assertEquals(DoubleValue.class, vti.getTypeClass());
	
	Assert.assertTrue(tti.getTypeAt(1).isBasicType());
	Assert.assertEquals(Integer.class , tti.getTypeAt(1).getTypeClass());
}
 
Example 7
Source Project: flink   Source File: DoubleValueArray.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean add(DoubleValue value) {
	int newPosition = position + 1;

	if (newPosition > data.length) {
		if (isBounded) {
			return false;
		} else {
			ensureCapacity(newPosition);
		}
	}

	data[position] = value.getValue();
	position = newPosition;

	return true;
}
 
Example 8
Source Project: flink   Source File: SummaryAggregatorFactoryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCreate() throws Exception {
	// supported primitive types
	Assert.assertEquals(StringSummaryAggregator.class, SummaryAggregatorFactory.create(String.class).getClass());
	Assert.assertEquals(ShortSummaryAggregator.class, SummaryAggregatorFactory.create(Short.class).getClass());
	Assert.assertEquals(IntegerSummaryAggregator.class, SummaryAggregatorFactory.create(Integer.class).getClass());
	Assert.assertEquals(LongSummaryAggregator.class, SummaryAggregatorFactory.create(Long.class).getClass());
	Assert.assertEquals(FloatSummaryAggregator.class, SummaryAggregatorFactory.create(Float.class).getClass());
	Assert.assertEquals(DoubleSummaryAggregator.class, SummaryAggregatorFactory.create(Double.class).getClass());
	Assert.assertEquals(BooleanSummaryAggregator.class, SummaryAggregatorFactory.create(Boolean.class).getClass());

	// supported value types
	Assert.assertEquals(ValueSummaryAggregator.StringValueSummaryAggregator.class, SummaryAggregatorFactory.create(StringValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.ShortValueSummaryAggregator.class, SummaryAggregatorFactory.create(ShortValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.IntegerValueSummaryAggregator.class, SummaryAggregatorFactory.create(IntValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.LongValueSummaryAggregator.class, SummaryAggregatorFactory.create(LongValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.FloatValueSummaryAggregator.class, SummaryAggregatorFactory.create(FloatValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.DoubleValueSummaryAggregator.class, SummaryAggregatorFactory.create(DoubleValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.BooleanValueSummaryAggregator.class, SummaryAggregatorFactory.create(BooleanValue.class).getClass());

	// some not well supported types - these fallback to ObjectSummaryAggregator
	Assert.assertEquals(ObjectSummaryAggregator.class, SummaryAggregatorFactory.create(Object.class).getClass());
	Assert.assertEquals(ObjectSummaryAggregator.class, SummaryAggregatorFactory.create(List.class).getClass());
}
 
Example 9
Source Project: flink   Source File: SummaryAggregatorFactoryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCreate() throws Exception {
	// supported primitive types
	Assert.assertEquals(StringSummaryAggregator.class, SummaryAggregatorFactory.create(String.class).getClass());
	Assert.assertEquals(ShortSummaryAggregator.class, SummaryAggregatorFactory.create(Short.class).getClass());
	Assert.assertEquals(IntegerSummaryAggregator.class, SummaryAggregatorFactory.create(Integer.class).getClass());
	Assert.assertEquals(LongSummaryAggregator.class, SummaryAggregatorFactory.create(Long.class).getClass());
	Assert.assertEquals(FloatSummaryAggregator.class, SummaryAggregatorFactory.create(Float.class).getClass());
	Assert.assertEquals(DoubleSummaryAggregator.class, SummaryAggregatorFactory.create(Double.class).getClass());
	Assert.assertEquals(BooleanSummaryAggregator.class, SummaryAggregatorFactory.create(Boolean.class).getClass());

	// supported value types
	Assert.assertEquals(ValueSummaryAggregator.StringValueSummaryAggregator.class, SummaryAggregatorFactory.create(StringValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.ShortValueSummaryAggregator.class, SummaryAggregatorFactory.create(ShortValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.IntegerValueSummaryAggregator.class, SummaryAggregatorFactory.create(IntValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.LongValueSummaryAggregator.class, SummaryAggregatorFactory.create(LongValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.FloatValueSummaryAggregator.class, SummaryAggregatorFactory.create(FloatValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.DoubleValueSummaryAggregator.class, SummaryAggregatorFactory.create(DoubleValue.class).getClass());
	Assert.assertEquals(ValueSummaryAggregator.BooleanValueSummaryAggregator.class, SummaryAggregatorFactory.create(BooleanValue.class).getClass());

	// some not well supported types - these fallback to ObjectSummaryAggregator
	Assert.assertEquals(ObjectSummaryAggregator.class, SummaryAggregatorFactory.create(Object.class).getClass());
	Assert.assertEquals(ObjectSummaryAggregator.class, SummaryAggregatorFactory.create(List.class).getClass());
}
 
Example 10
Source Project: flink   Source File: DoubleValueComparatorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected DoubleValue[] getSortedTestData() {
	Random rnd = new Random(874597969123412338L);
	double rndDouble = rnd.nextDouble();
	if (rndDouble < 0) {
		rndDouble = -rndDouble;
	}
	if (rndDouble == Double.MAX_VALUE) {
		rndDouble -= 3;
	}
	if (rndDouble <= 2) {
		rndDouble += 3;
	}
	return new DoubleValue[]{
		new DoubleValue(-rndDouble),
		new DoubleValue(-1.0D),
		new DoubleValue(0.0D),
		new DoubleValue(2.0D),
		new DoubleValue(rndDouble),
		new DoubleValue(Double.MAX_VALUE)};
}
 
Example 11
Source Project: flink   Source File: DoubleValueParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, DoubleValue 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 {
		double value = Double.parseDouble(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 12
Source Project: flink   Source File: DoubleValueSummaryAggregatorTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for summarizing a list of values.
 *
 * <p>This method breaks the rule of "testing only one thing" by aggregating and combining
 * a bunch of different ways.
 */
protected NumericColumnSummary<Double> summarize(Double... values) {

	DoubleValue[] doubleValues = new DoubleValue[values.length];
	for (int i = 0; i < values.length; i++) {
		if (values[i] != null) {
			doubleValues[i] = new DoubleValue(values[i]);
		}
	}

	return new AggregateCombineHarness<DoubleValue, NumericColumnSummary<Double>, ValueSummaryAggregator.DoubleValueSummaryAggregator>() {

		@Override
		protected void compareResults(NumericColumnSummary<Double> result1, NumericColumnSummary<Double> result2) {
			Assert.assertEquals(result1.getMin(), result2.getMin(), 0.0);
			Assert.assertEquals(result1.getMax(), result2.getMax(), 0.0);
			Assert.assertEquals(result1.getMean(), result2.getMean(), 1e-12d);
			Assert.assertEquals(result1.getVariance(), result2.getVariance(), 1e-9d);
			Assert.assertEquals(result1.getStandardDeviation(), result2.getStandardDeviation(), 1e-12d);
		}

	}.summarize(doubleValues);
}
 
Example 13
Source Project: flink   Source File: EitherSerializerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEitherWithTupleValues() {
	@SuppressWarnings("unchecked")
	Either<Tuple2<LongValue, LongValue>, DoubleValue>[] testData = new Either[] {
		Left(new Tuple2<>(new LongValue(2L), new LongValue(9L))),
		new Left<>(new Tuple2<>(new LongValue(Long.MIN_VALUE), new LongValue(Long.MAX_VALUE))),
		new Right<>(new DoubleValue(32.0)),
		Right(new DoubleValue(Double.MIN_VALUE)),
		Right(new DoubleValue(Double.MAX_VALUE))};

	EitherTypeInfo<Tuple2<LongValue, LongValue>, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(
		new TupleTypeInfo<Tuple2<LongValue, LongValue>>(ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.LONG_VALUE_TYPE_INFO),
		ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
	EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue> eitherSerializer =
		(EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());
	SerializerTestInstance<Either<Tuple2<LongValue, LongValue>, DoubleValue>> testInstance =
		new EitherSerializerTestInstance<>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
	testInstance.testAll();
}
 
Example 14
Source Project: Flink-CEPplus   Source File: DoubleValueArray.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean addAll(ValueArray<DoubleValue> other) {
	DoubleValueArray source = (DoubleValueArray) other;

	int sourceSize = source.position;
	int newPosition = position + sourceSize;

	if (newPosition > data.length) {
		if (isBounded) {
			return false;
		} else {
			ensureCapacity(newPosition);
		}
	}

	System.arraycopy(source.data, 0, data, position, sourceSize);
	position = newPosition;

	return true;
}
 
Example 15
Source Project: Flink-CEPplus   Source File: ValueArrayFactory.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Produce a {@code ValueArray} for the given {@code Value} type with the
 * given bounded size.
 *
 * @param cls {@code Value} class
 * @param bytes limit the array to the given number of bytes
 * @return {@code ValueArray} for given {@code Value} class
 */
@SuppressWarnings("unchecked")
public static <T> ValueArray<T> createValueArray(Class<? extends Value> cls, int bytes) {
	if (ByteValue.class.isAssignableFrom(cls)) {
		return (ValueArray<T>) new ByteValueArray(bytes);
	} else if (CharValue.class.isAssignableFrom(cls)) {
		return (ValueArray<T>) new CharValueArray(bytes);
	} else if (DoubleValue.class.isAssignableFrom(cls)) {
		return (ValueArray<T>) new DoubleValueArray(bytes);
	} else if (FloatValue.class.isAssignableFrom(cls)) {
		return (ValueArray<T>) new FloatValueArray(bytes);
	} else if (IntValue.class.isAssignableFrom(cls)) {
		return (ValueArray<T>) new IntValueArray(bytes);
	} else if (LongValue.class.isAssignableFrom(cls)) {
		return (ValueArray<T>) new LongValueArray(bytes);
	} else if (NullValue.class.isAssignableFrom(cls)) {
		return (ValueArray<T>) new NullValueArray(bytes);
	} else if (ShortValue.class.isAssignableFrom(cls)) {
		return (ValueArray<T>) new ShortValueArray(bytes);
	} else if (StringValue.class.isAssignableFrom(cls)) {
		return (ValueArray<T>) new StringValueArray(bytes);
	} else {
		throw new IllegalArgumentException("Unable to create bounded ValueArray for type " + cls);
	}
}
 
Example 16
Source Project: flink   Source File: PageRank.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void coGroup(Iterable<Tuple2<T, DoubleValue>> vertex, Iterable<Edge<T, LongValue>> edges, Collector<Tuple2<T, DoubleValue>> out)
		throws Exception {
	Iterator<Edge<T, LongValue>> edgeIterator = edges.iterator();

	if (edgeIterator.hasNext()) {
		Edge<T, LongValue> edge = edgeIterator.next();

		output.f0 = edge.f1;
		output.f1.setValue(vertex.iterator().next().f1.getValue() / edge.f2.getValue());
		out.collect(output);

		while (edgeIterator.hasNext()) {
			edge = edgeIterator.next();
			output.f0 = edge.f1;
			out.collect(output);
		}
	}
}
 
Example 17
Source Project: flink   Source File: DoubleValueArray.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void copyTo(ValueArray<DoubleValue> target) {
	DoubleValueArray other = (DoubleValueArray) target;

	other.position = position;
	other.mark = mark;

	other.ensureCapacity(position);
	System.arraycopy(data, 0, other.data, 0, position);
}
 
Example 18
Source Project: Flink-CEPplus   Source File: CsvReaderITCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testValueTypes() throws Exception {
	final String inputData = "ABC,true,1,2,3,4,5.0,6.0\nBCD,false,1,2,3,4,5.0,6.0";
	final String dataPath = createInputData(inputData);
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	DataSet<Tuple8<StringValue, BooleanValue, ByteValue, ShortValue, IntValue, LongValue, FloatValue, DoubleValue>> data =
			env.readCsvFile(dataPath).types(StringValue.class, BooleanValue.class, ByteValue.class, ShortValue.class, IntValue.class, LongValue.class, FloatValue.class, DoubleValue.class);
	List<Tuple8<StringValue, BooleanValue, ByteValue, ShortValue, IntValue, LongValue, FloatValue, DoubleValue>> result = data.collect();

	expected = inputData;
	compareResultAsTuples(result, expected);
}
 
Example 19
Source Project: flink   Source File: CSVReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testWithValueType() throws Exception {
	CsvReader reader = getCsvReader();
	DataSource<Tuple8<StringValue, BooleanValue, ByteValue, ShortValue, IntValue, LongValue, FloatValue, DoubleValue>> items =
			reader.types(StringValue.class, BooleanValue.class, ByteValue.class, ShortValue.class, IntValue.class, LongValue.class, FloatValue.class, DoubleValue.class);
	TypeInformation<?> info = items.getType();

	Assert.assertEquals(true, info.isTupleType());
	Assert.assertEquals(Tuple8.class, info.getTypeClass());
}
 
Example 20
Source Project: flink   Source File: ToNullValueTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTranslation() throws Exception {
	NullValue reuse = NullValue.getInstance();

	assertEquals(NullValue.getInstance(), new ToNullValue<>().translate(new DoubleValue(), reuse));
	assertEquals(NullValue.getInstance(), new ToNullValue<>().translate(new FloatValue(), reuse));
	assertEquals(NullValue.getInstance(), new ToNullValue<>().translate(new IntValue(), reuse));
	assertEquals(NullValue.getInstance(), new ToNullValue<>().translate(new LongValue(), reuse));
	assertEquals(NullValue.getInstance(), new ToNullValue<>().translate(new StringValue(), reuse));
}
 
Example 21
Source Project: flink   Source File: PageRank.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void open(Configuration parameters) throws Exception {
	super.open(parameters);

	Collection<Tuple2<T, DoubleValue>> sumOfScores = getRuntimeContext().getBroadcastVariable(SUM_OF_SCORES);
	Iterator<Tuple2<T, DoubleValue>> sumOfScoresIterator = sumOfScores.iterator();
	// floating point precision error is also included in sumOfSinks
	double sumOfSinks = 1 - (sumOfScoresIterator.hasNext() ? sumOfScoresIterator.next().f1.getValue() : 0);

	Collection<LongValue> vertexCount = getRuntimeContext().getBroadcastVariable(VERTEX_COUNT);
	Iterator<LongValue> vertexCountIterator = vertexCount.iterator();
	this.vertexCount = vertexCountIterator.hasNext() ? vertexCountIterator.next().getValue() : 0;

	this.uniformlyDistributedScore = ((1 - dampingFactor) + dampingFactor * sumOfSinks) / this.vertexCount;
}
 
Example 22
Source Project: flink   Source File: GraphKeyTypeTransformTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testToDoubleValue() throws Exception {
	TranslateFunction<LongValue, DoubleValue> translator = new LongValueToDoubleValue();

	Assert.assertEquals(new DoubleValue(Double.longBitsToDouble(0L)),
		translator.translate(new LongValue(0L), doubleValue));

	Assert.assertEquals(new DoubleValue(Double.longBitsToDouble(Long.MIN_VALUE)),
		translator.translate(new LongValue(Long.MIN_VALUE), doubleValue));

	Assert.assertEquals(new DoubleValue(Double.longBitsToDouble(Long.MAX_VALUE)),
		translator.translate(new LongValue(Long.MAX_VALUE), doubleValue));
}
 
Example 23
Source Project: flink   Source File: ValueArraySerializerUpgradeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Matcher<DoubleValueArray> testDataMatcher() {
	DoubleValueArray array = new DoubleValueArray(128);
	array.add(new DoubleValue(1.2));
	array.add(new DoubleValue(3.4));
	return is(array);
}
 
Example 24
Source Project: Flink-CEPplus   Source File: PageRank.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void flatMap(Vertex<T, Degrees> vertex, Collector<Tuple2<T, DoubleValue>> out)
		throws Exception {
	if (vertex.f1.getInDegree().getValue() == 0) {
		output.f0 = vertex.f0;
		out.collect(output);
	}
}
 
Example 25
Source Project: flink   Source File: CsvReaderITCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testValueTypes() throws Exception {
	final String inputData = "ABC,true,1,2,3,4,5.0,6.0\nBCD,false,1,2,3,4,5.0,6.0";
	final String dataPath = createInputData(inputData);
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	DataSet<Tuple8<StringValue, BooleanValue, ByteValue, ShortValue, IntValue, LongValue, FloatValue, DoubleValue>> data =
			env.readCsvFile(dataPath).types(StringValue.class, BooleanValue.class, ByteValue.class, ShortValue.class, IntValue.class, LongValue.class, FloatValue.class, DoubleValue.class);
	List<Tuple8<StringValue, BooleanValue, ByteValue, ShortValue, IntValue, LongValue, FloatValue, DoubleValue>> result = data.collect();

	expected = inputData;
	compareResultAsTuples(result, expected);
}
 
Example 26
Source Project: flink   Source File: InstantiationUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationToByteArray() throws IOException {
	final DoubleValue toSerialize = new DoubleValue(Math.random());
	final DoubleValueSerializer serializer = new DoubleValueSerializer();

	byte[] serialized = InstantiationUtil.serializeToByteArray(serializer, toSerialize);

	DoubleValue deserialized = InstantiationUtil.deserializeFromByteArray(serializer, serialized);

	assertEquals("Serialized record is not equal after serialization.", toSerialize, deserialized);
}
 
Example 27
Source Project: flink   Source File: HITS.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Tuple3<T, DoubleValue, DoubleValue> join(Tuple2<T, DoubleValue> hubbiness, Tuple2<T, DoubleValue> authority)
		throws Exception {
	output.f0 = (authority == null) ? hubbiness.f0 : authority.f0;
	output.f1.setValue(hubbiness == null ? 0.0 : hubbiness.f1.getValue() / hubbinessRootSumSquared);
	output.f2.setValue(authority == null ? 0.0 : authority.f1.getValue() / authorityRootSumSquared);
	return output;
}
 
Example 28
Source Project: flink   Source File: HITS.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Tuple3<T, DoubleValue, DoubleValue> join(Tuple3<T, DoubleValue, DoubleValue> first, Tuple3<T, DoubleValue, DoubleValue> second)
		throws Exception {
	if (!isInitialSuperstep) {
		changeInScores += Math.abs(second.f1.getValue() - first.f1.getValue());
		changeInScores += Math.abs(second.f2.getValue() - first.f2.getValue());
	}

	return second;
}
 
Example 29
Source Project: flink   Source File: HITS.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void open(Configuration parameters) throws Exception {
	super.open(parameters);

	Collection<DoubleValue> hubbinessSumSquared = getRuntimeContext().getBroadcastVariable(HUBBINESS_SUM_SQUARED);
	Iterator<DoubleValue> hubbinessSumSquaredIterator = hubbinessSumSquared.iterator();
	this.hubbinessRootSumSquared = hubbinessSumSquaredIterator.hasNext() ? Math.sqrt(hubbinessSumSquaredIterator.next().getValue()) : Double.NaN;

	Collection<DoubleValue> authoritySumSquared = getRuntimeContext().getBroadcastVariable(AUTHORITY_SUM_SQUARED);
	Iterator<DoubleValue> authoritySumSquaredIterator = authoritySumSquared.iterator();
	authorityRootSumSquared = authoritySumSquaredIterator.hasNext() ? Math.sqrt(authoritySumSquaredIterator.next().getValue()) : Double.NaN;
}
 
Example 30
Source Project: flink   Source File: EitherSerializerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEitherWithObjectReuse() {
	EitherTypeInfo<LongValue, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(
		ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
	EitherSerializer<LongValue, DoubleValue> eitherSerializer =
		(EitherSerializer<LongValue, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());

	LongValue lv = new LongValue();
	DoubleValue dv = new DoubleValue();

	Either<LongValue, DoubleValue> left = Left(lv);
	Either<LongValue, DoubleValue> right = Right(dv);

	// the first copy creates a new instance of Left
	Either<LongValue, DoubleValue> copy0 = eitherSerializer.copy(left, right);

	// then the cross-references are used for future copies
	Either<LongValue, DoubleValue> copy1 = eitherSerializer.copy(right, copy0);
	Either<LongValue, DoubleValue> copy2 = eitherSerializer.copy(left, copy1);

	// validate reference equality
	assertSame(right, copy1);
	assertSame(copy0, copy2);

	// validate reference equality of contained objects
	assertSame(right.right(), copy1.right());
	assertSame(copy0.left(), copy2.left());
}