@SuppressWarnings("unchecked") @Test public void udf3Test() { spark.udf().registerJava("stringLengthTest", StringLengthTest.class.getName(), DataTypes.IntegerType); Row result = spark.sql("SELECT stringLengthTest('test', 'test2')").head(); Assert.assertEquals(9, result.getInt(0)); // returnType is not provided spark.udf().registerJava("stringLengthTest2", StringLengthTest.class.getName(), null); result = spark.sql("SELECT stringLengthTest('test', 'test2')").head(); Assert.assertEquals(9, result.getInt(0)); }
@SuppressWarnings("unchecked") @Test public void udf3Test() { spark.udf().registerJava("stringLengthTest", StringLengthTest.class.getName(), DataTypes.IntegerType); Row result = spark.sql("SELECT stringLengthTest('test', 'test2')").head(); Assert.assertEquals(9, result.getInt(0)); // returnType is not provided spark.udf().registerJava("stringLengthTest2", StringLengthTest.class.getName(), null); result = spark.sql("SELECT stringLengthTest('test', 'test2')").head(); Assert.assertEquals(9, result.getInt(0)); }
@SuppressWarnings("unchecked") @Test public void udf3Test() { spark.udf().registerJava("stringLengthTest", StringLengthTest.class.getName(), DataTypes.IntegerType); Row result = spark.sql("SELECT stringLengthTest('test', 'test2')").head(); Assert.assertEquals(9, result.getInt(0)); // returnType is not provided spark.udf().registerJava("stringLengthTest2", StringLengthTest.class.getName(), null); result = spark.sql("SELECT stringLengthTest('test', 'test2')").head(); Assert.assertEquals(9, result.getInt(0)); }
static void initializeUDFs(Config config) { if (!config.hasPath(UDFS_SECTION_CONFIG)) return; ConfigList udfList = config.getList(UDFS_SECTION_CONFIG); for (ConfigValue udfValue : udfList) { ConfigValueType udfValueType = udfValue.valueType(); if (!udfValueType.equals(ConfigValueType.OBJECT)) { throw new RuntimeException("UDF list must contain UDF objects"); } Config udfConfig = ((ConfigObject)udfValue).toConfig(); for (String path : Lists.newArrayList(UDFS_NAME, UDFS_CLASS)) { if (!udfConfig.hasPath(path)) { throw new RuntimeException("UDF entries must provide '" + path + "'"); } } String name = udfConfig.getString(UDFS_NAME); String className = udfConfig.getString(UDFS_CLASS); // null third argument means that registerJava will infer the return type Contexts.getSparkSession().udf().registerJava(name, className, null); LOG.info("Registered Spark SQL UDF: " + name); } }