User Defined Operators

Post Reply
aboubakrelhacen
Posts: 3
Joined: Thu Sep 04, 2014 11:18 am

User Defined Operators

Post by aboubakrelhacen » Thu Sep 04, 2014 11:21 am

Hi,
can you please give a concrete example where UDOs are used in odysseus. The documentation does not contain a complete example, where UDOs used in a query.
Regards,

User avatar
ckuka
Posts: 35
Joined: Tue Jul 29, 2014 10:55 am
Location: Shanghai, P.R. China/中国上海
Contact:

Re: User Defined Operators

Post by ckuka » Thu Sep 04, 2014 12:04 pm

A good example for UDOs is the Rapid Prototyping Feature [1]. However, if you only search a simple example on how to implement an UDO, take a look at "server/core/core/src/de/uniol/inf/is/odysseus/core/server/udf/ElementSampling.java" in the SVN.

[1] http://odysseus.offis.uni-oldenburg.de: ... ng+Feature
kind regards / 祝好
Christian Kuka

aboubakrelhacen
Posts: 3
Joined: Thu Sep 04, 2014 11:18 am

Re: User Defined Operators

Post by aboubakrelhacen » Thu Sep 04, 2014 12:19 pm

ckuka wrote:A good example for UDOs is the Rapid Prototyping Feature [1]. However, if you only search a simple example on how to implement an UDO, take a look at "server/core/core/src/de/uniol/inf/is/odysseus/core/server/udf/ElementSampling.java" in the SVN.

[1] http://odysseus.offis.uni-oldenburg.de: ... ng+Feature
Hi,
Thak you for your response, i have taken a look at the fast prototyping examples. Since the only method you have to implement is the process method with the following signature: "public Object[] process(Object[] payload, Map<String,Object> metadata)"
Does the payload field refer to each tuple received ? and the second question: how do i do to create my own tuples and output them to the next operators in the query ?

Thanks,

Aboubakr.

User avatar
ckuka
Posts: 35
Joined: Tue Jul 29, 2014 10:55 am
Location: Shanghai, P.R. China/中国上海
Contact:

Re: User Defined Operators

Post by ckuka » Thu Sep 04, 2014 12:38 pm

In the "process" method in the Rapid Prototyping example the payload field refers to the object array holding all values in one tuple. Thus, it depends on the schema of the stream. The return value of the method is the payload of the resulting tuple. However, this is just for rapid stuff and may have some drawbacks as the datatypes of the resulting stream are unknown to other operators. If you really want to implement an UDO the only thing you need to do is to annotate a Java class with:

@UserDefinedFunction(name = "MyName")

and implement the IUserDefinedFunction<Tuple<? extends IMetaAttribute>, Tuple<? extends IMetaAttribute>> interface, e.g.,

Code: Select all

@UserDefinedFunction(name = "MyName")
public class MyUserDefinedFunction implements IUserDefinedFunction<Tuple<? extends IMetaAttribute>, Tuple<? extends IMetaAttribute>> {

 public void init(final String initString) {
 // do init stuff
 }

  public Tuple<? extends IMetaAttribute> process(final Tuple<? extends IMetaAttribute> in, final int port) {
  // process incoming data
  // and return new tuple
}

}
and then place the class in some bundle in the OSGi container. Or you can go a step further and implement an Operator [1].

[1] http://odysseus.offis.uni-oldenburg.de: ... +Framework
kind regards / 祝好
Christian Kuka

aboubakrelhacen
Posts: 3
Joined: Thu Sep 04, 2014 11:18 am

Re: User Defined Operators

Post by aboubakrelhacen » Thu Sep 04, 2014 12:54 pm

ckuka wrote:In the "process" method in the Rapid Prototyping example the payload field refers to the object array holding all values in one tuple. Thus, it depends on the schema of the stream. The return value of the method is the payload of the resulting tuple. However, this is just for rapid stuff and may have some drawbacks as the datatypes of the resulting stream are unknown to other operators. If you really want to implement an UDO the only thing you need to do is to annotate a Java class with:

@UserDefinedFunction(name = "MyName")

and implement the IUserDefinedFunction<Tuple<? extends IMetaAttribute>, Tuple<? extends IMetaAttribute>> interface, e.g.,

Code: Select all

@UserDefinedFunction(name = "MyName")
public class MyUserDefinedFunction implements IUserDefinedFunction<Tuple<? extends IMetaAttribute>, Tuple<? extends IMetaAttribute>> {

 public void init(final String initString) {
 // do init stuff
 }

  public Tuple<? extends IMetaAttribute> process(final Tuple<? extends IMetaAttribute> in, final int port) {
  // process incoming data
  // and return new tuple
}

}
and then place the class in some bundle in the OSGi container. Or you can go a step further and implement an Operator [1].

[1] http://odysseus.offis.uni-oldenburg.de: ... +Framework
is there any example about the UserDefinedFunction method ??

Regards,

Aboubakr

User avatar
ckuka
Posts: 35
Joined: Tue Jul 29, 2014 10:55 am
Location: Shanghai, P.R. China/中国上海
Contact:

Re: User Defined Operators

Post by ckuka » Thu Sep 04, 2014 1:08 pm

This is the actual content of the ELEMENT_SAMPLING UDO:

Code: Select all

package de.uniol.inf.is.odysseus.core.server.udf;

import de.uniol.inf.is.odysseus.core.server.logicaloperator.annotations.UserDefinedFunction;
import de.uniol.inf.is.odysseus.core.server.physicaloperator.AbstractPipe.OutputMode;
import de.uniol.inf.is.odysseus.core.server.physicaloperator.IUserDefinedFunction;

@UserDefinedFunction(name="ELEMENT_SAMPLING")
public class ElementSampling<R> implements IUserDefinedFunction<R, R> {

	int everyNthObject;
	int objectsRead;
	
	/*
	 * 
	 * initString: Send each nth Element
	 */
	@Override
	public void init(String initString) {
		everyNthObject = Integer.parseInt(initString);
		objectsRead = 0;
	}

	@Override
	public R process(R in, int port) {
		objectsRead++;
		if (objectsRead % everyNthObject  == 0){
			return in;
		}
		return null;
	}

	@Override
	public OutputMode getOutputMode() {
		return OutputMode.INPUT;
	}
}
to use this UDO in a PQL query you use the UDO operator as follows:

Code: Select all

output = UDO({class='ELEMENT_SAMPLING', init='5'}, input)
This will actually forward every fifth tuple from the input to the output.
kind regards / 祝好
Christian Kuka

sofiajoe
Posts: 2
Joined: Wed Oct 29, 2014 10:19 am

Re: User Defined Operators

Post by sofiajoe » Wed Oct 29, 2014 11:15 am

hey everyone
nice post and discussion i really like it :)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest