org.apache.hadoop.hive.ql.udf.generic.GenericUDAFSum Java Examples

The following examples show how to use org.apache.hadoop.hive.ql.udf.generic.GenericUDAFSum. 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: HiveGenericUDAFTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testUDAFSum() throws Exception {
	Object[] constantArgs = new Object[] {
		null
	};

	DataType[] argTypes = new DataType[] {
		DataTypes.DOUBLE()
	};

	HiveGenericUDAF udf = init(GenericUDAFSum.class, constantArgs, argTypes);

	GenericUDAFEvaluator.AggregationBuffer acc = udf.createAccumulator();

	udf.accumulate(acc, 0.5d);
	udf.accumulate(acc, 0.3d);
	udf.accumulate(acc, 5.3d);

	udf.merge(acc, Arrays.asList());

	assertEquals(6.1d, udf.getValue(acc));

	constantArgs = new Object[] {
		null
	};

	argTypes = new DataType[] {
		DataTypes.DECIMAL(5, 3)
	};

	udf = init(GenericUDAFSum.class, constantArgs, argTypes);

	acc = udf.createAccumulator();

	udf.accumulate(acc, BigDecimal.valueOf(10.111));
	udf.accumulate(acc, BigDecimal.valueOf(3.222));
	udf.accumulate(acc, BigDecimal.valueOf(5.333));

	udf.merge(acc, Arrays.asList());

	assertEquals(BigDecimal.valueOf(18.666), udf.getValue(acc));
}
 
Example #2
Source File: HiveCatalogUseBlinkITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testBlinkUdf() throws Exception {
	TableSchema schema = TableSchema.builder()
			.field("name", DataTypes.STRING())
			.field("age", DataTypes.INT())
			.build();

	FormatDescriptor format = new OldCsv()
			.field("name", Types.STRING())
			.field("age", Types.INT());

	CatalogTable source =
			new CatalogTableBuilder(
					new FileSystem().path(this.getClass().getResource("/csv/test.csv").getPath()),
					schema)
					.withFormat(format)
					.inAppendMode()
					.withComment("Comment.")
					.build();

	hiveCatalog.createTable(
			new ObjectPath(HiveCatalog.DEFAULT_DB, sourceTableName),
			source,
			false
	);

	hiveCatalog.createFunction(
			new ObjectPath(HiveCatalog.DEFAULT_DB, "myudf"),
			new CatalogFunctionImpl(TestHiveSimpleUDF.class.getCanonicalName()),
			false);
	hiveCatalog.createFunction(
			new ObjectPath(HiveCatalog.DEFAULT_DB, "mygenericudf"),
			new CatalogFunctionImpl(TestHiveGenericUDF.class.getCanonicalName()),
			false);
	hiveCatalog.createFunction(
			new ObjectPath(HiveCatalog.DEFAULT_DB, "myudtf"),
			new CatalogFunctionImpl(TestHiveUDTF.class.getCanonicalName()),
			false);
	hiveCatalog.createFunction(
			new ObjectPath(HiveCatalog.DEFAULT_DB, "myudaf"),
			new CatalogFunctionImpl(GenericUDAFSum.class.getCanonicalName()),
			false);

	testUdf(true);
	testUdf(false);
}
 
Example #3
Source File: HiveGenericUDAFTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testUDAFSum() throws Exception {
	Object[] constantArgs = new Object[] {
		null
	};

	DataType[] argTypes = new DataType[] {
		DataTypes.DOUBLE()
	};

	HiveGenericUDAF udf = init(GenericUDAFSum.class, constantArgs, argTypes);

	GenericUDAFEvaluator.AggregationBuffer acc = udf.createAccumulator();

	udf.accumulate(acc, 0.5d);
	udf.accumulate(acc, 0.3d);
	udf.accumulate(acc, 5.3d);

	udf.merge(acc, Arrays.asList());

	assertEquals(6.1d, udf.getValue(acc));

	constantArgs = new Object[] {
		null
	};

	argTypes = new DataType[] {
		DataTypes.DECIMAL(5, 3)
	};

	udf = init(GenericUDAFSum.class, constantArgs, argTypes);

	acc = udf.createAccumulator();

	udf.accumulate(acc, BigDecimal.valueOf(10.111));
	udf.accumulate(acc, BigDecimal.valueOf(3.222));
	udf.accumulate(acc, BigDecimal.valueOf(5.333));

	udf.merge(acc, Arrays.asList());

	assertEquals(BigDecimal.valueOf(18.666), udf.getValue(acc));
}