Java Code Examples for org.apache.hadoop.hive.ql.udf.generic.GenericUDF#initialize()

The following examples show how to use org.apache.hadoop.hive.ql.udf.generic.GenericUDF#initialize() . 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: HiveFunctionRegistry.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private HiveFuncHolder matchAndCreateUDFHolder(String udfName,
                                               Class<? extends UDF> udfClazz,
                                               CompleteType[] argTypes,
                                               ObjectInspector[] argOIs) {
  try {
    GenericUDF udfInstance = new GenericUDFBridge(udfName, false/* is operator */, udfClazz.getName());
    ObjectInspector returnOI = udfInstance.initialize(argOIs);

    return new HiveFuncHolder(
      udfName,
      udfClazz,
      argTypes,
      returnOI,
      CompleteType.fromMinorType(ObjectInspectorHelper.getMinorType(returnOI)),
      nonDeterministicUDFs.contains(udfClazz));
  } catch (Exception e) { /*ignore this*/ }

  return null;
}
 
Example 2
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Test(expected = UDFArgumentException.class)
public void testInvalidMode() throws IOException, HiveException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[2];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    // mode
    PrimitiveTypeInfo stringType = new PrimitiveTypeInfo();
    stringType.setTypeName("string");
    argOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
        stringType, new Text("unsupported mode"));
    udf.initialize(argOIs);

    DeferredObject[] args = new DeferredObject[1];
    args[0] = new DeferredObject() {
        public Text get() throws HiveException {
            return new Text("クロモジのJapaneseAnalyzerを使ってみる。テスト。");
        }

        @Override
        public void prepare(int arg) throws HiveException {}
    };
    udf.evaluate(args);

    udf.close();
}
 
Example 3
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Test
public void testThreeArgument() throws UDFArgumentException, IOException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[3];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    // mode
    PrimitiveTypeInfo stringType = new PrimitiveTypeInfo();
    stringType.setTypeName("string");
    argOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
        stringType, null);
    // stopWords
    argOIs[2] = ObjectInspectorFactory.getStandardConstantListObjectInspector(
        PrimitiveObjectInspectorFactory.javaStringObjectInspector, null);
    udf.initialize(argOIs);
    udf.close();
}
 
Example 4
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Test
public void testFourArgument() throws UDFArgumentException, IOException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[4];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    // mode
    PrimitiveTypeInfo stringType = new PrimitiveTypeInfo();
    stringType.setTypeName("string");
    argOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
        stringType, null);
    // stopWords
    argOIs[2] = ObjectInspectorFactory.getStandardConstantListObjectInspector(
        PrimitiveObjectInspectorFactory.javaStringObjectInspector, null);
    // stopTags
    argOIs[3] = ObjectInspectorFactory.getStandardConstantListObjectInspector(
        PrimitiveObjectInspectorFactory.javaStringObjectInspector, null);
    udf.initialize(argOIs);
    udf.close();
}
 
Example 5
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Test
public void testFiveArgumentArray() throws UDFArgumentException, IOException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[5];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    // mode
    PrimitiveTypeInfo stringType = new PrimitiveTypeInfo();
    stringType.setTypeName("string");
    argOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
        stringType, null);
    // stopWords
    argOIs[2] = ObjectInspectorFactory.getStandardConstantListObjectInspector(
        PrimitiveObjectInspectorFactory.javaStringObjectInspector, null);
    // stopTags
    argOIs[3] = ObjectInspectorFactory.getStandardConstantListObjectInspector(
        PrimitiveObjectInspectorFactory.javaStringObjectInspector, null);
    // userDictUrl
    argOIs[4] = ObjectInspectorFactory.getStandardConstantListObjectInspector(
        PrimitiveObjectInspectorFactory.javaStringObjectInspector, null);
    udf.initialize(argOIs);
    udf.close();
}
 
Example 6
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Test
public void testFiveArgumenString() throws UDFArgumentException, IOException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[5];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    // mode
    PrimitiveTypeInfo stringType = new PrimitiveTypeInfo();
    stringType.setTypeName("string");
    argOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
        stringType, null);
    // stopWords
    argOIs[2] = ObjectInspectorFactory.getStandardConstantListObjectInspector(
        PrimitiveObjectInspectorFactory.javaStringObjectInspector, null);
    // stopTags
    argOIs[3] = ObjectInspectorFactory.getStandardConstantListObjectInspector(
        PrimitiveObjectInspectorFactory.javaStringObjectInspector, null);
    // userDictUrl
    argOIs[4] = PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
        stringType, null);
    udf.initialize(argOIs);
    udf.close();
}
 
Example 7
Source File: HiveUDFImplementor.java    From marble with Apache License 2.0 5 votes vote down vote up
public static ObjectInspector initGenericUDF(GenericUDF udfInstance, RelDataTypeHolder[] argsType) {
  ObjectInspector[] inputObjectInspector =
      TypeInferenceUtil.getObjectInspector(
          argsType);
  try {
    ObjectInspector outputObjectInspector = udfInstance.initialize(
        inputObjectInspector);
    return outputObjectInspector;
  } catch (UDFArgumentException e) {
    throw new RuntimeException(e);
  }
}
 
Example 8
Source File: SmartcnUDFTest.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Test
public void testOneArgument() throws UDFArgumentException, IOException {
    GenericUDF udf = new SmartcnUDF();
    ObjectInspector[] argOIs = new ObjectInspector[1];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    udf.initialize(argOIs);
    udf.close();
}
 
Example 9
Source File: SmartcnUDFTest.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoArgument() throws UDFArgumentException, IOException {
    GenericUDF udf = new SmartcnUDF();
    ObjectInspector[] argOIs = new ObjectInspector[2];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    // stopWords
    argOIs[1] = ObjectInspectorFactory.getStandardConstantListObjectInspector(
        PrimitiveObjectInspectorFactory.javaStringObjectInspector, null);
    udf.initialize(argOIs);
    udf.close();
}
 
Example 10
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Test
public void testOneArgument() throws UDFArgumentException, IOException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[1];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    udf.initialize(argOIs);
    udf.close();
}
 
Example 11
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoArgument() throws UDFArgumentException, IOException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[2];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    // mode
    PrimitiveTypeInfo stringType = new PrimitiveTypeInfo();
    stringType.setTypeName("string");
    argOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
        stringType, null);
    udf.initialize(argOIs);
    udf.close();
}
 
Example 12
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
public void testExpectedMode() throws UDFArgumentException, IOException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[2];
    // line
    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    // mode
    PrimitiveTypeInfo stringType = new PrimitiveTypeInfo();
    stringType.setTypeName("string");
    argOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
        stringType, new Text("normal"));
    udf.initialize(argOIs);
    udf.close();
}
 
Example 13
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Test
public void testNormalModeWithOption()
        throws IOException, HiveException, IllegalAccessException, NoSuchFieldException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[2];

    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector; // line
    argOIs[1] = HiveUtils.getConstStringObjectInspector("-mode normal"); // mode
    udf.initialize(argOIs);

    Object mode = PrivilegedAccessor.getValue(udf, "_mode");
    Assert.assertEquals(Mode.NORMAL, mode);

    DeferredObject[] args = new DeferredObject[1];
    args[0] = new DeferredObject() {
        public Text get() throws HiveException {
            return new Text("クロモジのJapaneseAnalyzerを使ってみる。テスト。");
        }

        @Override
        public void prepare(int arg) throws HiveException {}
    };
    Object result = udf.evaluate(args);
    Assert.assertThat(Arrays.asList(new Text("クロモジ"), new Text("japaneseanalyzer"),
        new Text("使う"), new Text("みる"), new Text("テスト")), CoreMatchers.is(result));

    udf.close();
}
 
Example 14
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Test
public void testNormalModeWithPosOptions()
        throws IOException, HiveException, IllegalAccessException, NoSuchFieldException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[2];

    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector; // line
    argOIs[1] = HiveUtils.getConstStringObjectInspector("-mode normal -pos"); // mode
    udf.initialize(argOIs);

    Object mode = PrivilegedAccessor.getValue(udf, "_mode");
    Assert.assertEquals(Mode.NORMAL, mode);

    DeferredObject[] args = new DeferredObject[1];
    args[0] = new DeferredObject() {
        public Text get() throws HiveException {
            return new Text("クロモジのJapaneseAnalyzerを使ってみる。テスト。");
        }

        @Override
        public void prepare(int arg) throws HiveException {}
    };

    Object[] result = (Object[]) udf.evaluate(args);
    Assert.assertEquals(2, result.length);

    Assert.assertEquals(Arrays.asList(new Text("クロモジ"), new Text("japaneseanalyzer"),
        new Text("使う"), new Text("みる"), new Text("テスト")), result[0]);
    Assert.assertEquals(Arrays.asList(new Text("名詞-一般"), new Text("名詞-一般"), new Text("動詞-自立"),
        new Text("動詞-非自立"), new Text("名詞-サ変接続")), result[1]);

    udf.close();
}
 
Example 15
Source File: KuromojiUDFTest.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Test(expected = UDFArgumentException.class)
public void testUnsupportedOptionArgs()
        throws IOException, HiveException, IllegalAccessException, NoSuchFieldException {
    GenericUDF udf = new KuromojiUDF();
    ObjectInspector[] argOIs = new ObjectInspector[2];

    argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector; // line
    argOIs[1] = HiveUtils.getConstStringObjectInspector("-mode normal -unsupported_option"); // mode
    udf.initialize(argOIs);

    udf.close();
}
 
Example 16
Source File: HiveSqlUDFReturnTypeInference.java    From marble with Apache License 2.0 4 votes vote down vote up
@Override public RelDataType inferReturnType(
    final SqlOperatorBinding opBinding) {
  try {
    SqlOperator sqlOperator = opBinding.getOperator();
    List<RelDataTypeHolder> argsType = new ArrayList<>();
    for (int i = 0; i < opBinding.getOperandCount(); i++) {
      RelDataTypeHolder relDataTypeHolder;
      boolean isSqlCallBinding = opBinding instanceof SqlCallBinding;
      if (isSqlCallBinding) {
        List<SqlNode> operands = ((SqlCallBinding) opBinding).operands();
        if (operands.get(i) instanceof SqlLiteral) {
          relDataTypeHolder = new RelDataTypeHolder(
              opBinding.getOperandType(i), true,
              ((SqlLiteral) operands.get(i)).getValue());
        } else {
          relDataTypeHolder = new RelDataTypeHolder(
              opBinding.getOperandType(i));
        }

      } else {
        relDataTypeHolder = new RelDataTypeHolder(
            opBinding.getOperandType(i));
      }
      argsType.add(relDataTypeHolder);
    }
    String opName = sqlOperator.getName();
    GenericUDF udfInstance = HiveUDFImplementor.newGenericUDF(
        opName, sqlOperator.getSyntax());
    ObjectInspector[] inputObjectInspector =
        TypeInferenceUtil.getObjectInspector(
            argsType.toArray(new RelDataTypeHolder[0]));
    ObjectInspector outputObjectInspector = udfInstance.initialize(
        inputObjectInspector);
    RelDataType resultType = TypeInferenceUtil.getRelDataType(
        outputObjectInspector,
        opBinding.getTypeFactory());
    return resultType;

  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}