向外部 H2 数据库添加聚合函数
我正在尝试使用 Java 在 H2 数据库中创建聚合函数。 该函数应返回给定 Double 列的自定义中位数计算结果。 此计算包括仅使用足够接近使用 max_variance int 值的平均值的值。为此,我创建了该类:
package custommedian;
public class CustomMedian implements org.h2.api.AggregateFunction{
final int max_variance = 7;
java.util.LinkedList<Double> values = new java.util.LinkedList<Double>();
@Override
public void init(java.sql.Connection cnctn) throws java.sql.SQLException {
// I ignored this
}
@Override
public int getType(int[] ints) throws java.sql.SQLException {
return java.sql.Types.DOUBLE;
}
@Override
public void add(Object o) throws java.sql.SQLException {
values.add((Double)o);
}
@Override
public Object getResult() throws java.sql.SQLException {
double average = 0;
java.util.Iterator<Double> i;
java.util.LinkedList<Double> properValues = new java.util.LinkedList<Double>();
// Get average value
for( i = values.iterator(); i.hasNext(); ) {
average += i.next();
}
average = average / values.size();
// Filter out invalid values
for( i = values.iterator(); i.hasNext(); ) {
double value = i.next();
if (value - max_variance < average && value + max_variance > average){
properValues.add(value);
}
}
// Sort list
java.util.Collections.sort(properValues);
// Return median
int size = properValues.size();
if (size > 0){
int pos = ((int) size/2);
// Odd size
if ((size%2) == 1 ) return properValues.get(pos);
// Even size
else return ( properValues.get(pos-1) + properValues.get(pos) ) / 2;
}
else
return null;
}
}
现在我应该编译它,以便数据库可以访问它并使用此命令:
CREATE AGGREGATE MEDIAN FOR "custommedian.CustomMedian";
我应该如何做到这一点?
我尝试将 .jar 文件放在与数据库文件相同的文件夹中,但似乎找不到该类:
Class "custommedian.CustomMedian" not found; SQL statement:
任何人都可以告诉我我还需要做什么才能完成这项工作?
预先非常感谢!
编辑:
已解决:
正如 Lukas 所建议的,我最终将 H2 jar 嵌入到我的应用程序中,这样就不存在类路径问题。
I'm trying to create an aggregate function in my H2 database using Java.
The function should return a custom median calculation from the given Double column.
This calculation consist in using only the values that are close enough to the average value using the max_variance int value. For this I created the class:
package custommedian;
public class CustomMedian implements org.h2.api.AggregateFunction{
final int max_variance = 7;
java.util.LinkedList<Double> values = new java.util.LinkedList<Double>();
@Override
public void init(java.sql.Connection cnctn) throws java.sql.SQLException {
// I ignored this
}
@Override
public int getType(int[] ints) throws java.sql.SQLException {
return java.sql.Types.DOUBLE;
}
@Override
public void add(Object o) throws java.sql.SQLException {
values.add((Double)o);
}
@Override
public Object getResult() throws java.sql.SQLException {
double average = 0;
java.util.Iterator<Double> i;
java.util.LinkedList<Double> properValues = new java.util.LinkedList<Double>();
// Get average value
for( i = values.iterator(); i.hasNext(); ) {
average += i.next();
}
average = average / values.size();
// Filter out invalid values
for( i = values.iterator(); i.hasNext(); ) {
double value = i.next();
if (value - max_variance < average && value + max_variance > average){
properValues.add(value);
}
}
// Sort list
java.util.Collections.sort(properValues);
// Return median
int size = properValues.size();
if (size > 0){
int pos = ((int) size/2);
// Odd size
if ((size%2) == 1 ) return properValues.get(pos);
// Even size
else return ( properValues.get(pos-1) + properValues.get(pos) ) / 2;
}
else
return null;
}
}
And now I'm supposed to compile it so it's accessible for the database and use this command:
CREATE AGGREGATE MEDIAN FOR "custommedian.CustomMedian";
How am I supposed to do that?
I tried putting the .jar file in the same folder as the database file is but it doesn't seem to find the class:
Class "custommedian.CustomMedian" not found; SQL statement:
Anyone could please tell me what else I have to do to make this work?
Thanks a lot in advance!
EDIT:
Solved:
As Lukas suggested, I ended up embedding the H2 jar into my application and this way there are no classpath issues.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否已将类添加到 H2 数据库进程的类路径中?即什么时候启动H2数据库?
另一种方法是分叉 H2 并构建它,包括您的自定义聚合函数。或者只是将您的 .class 文件放入 H2 jar 文件中,具体取决于您需要更新自己的代码的频率,这也可能没问题。那么它肯定会在类路径上...
在最坏的情况下,H2 用户组非常活跃:
http://groups.google.com/group/h2-database
Have you added your class to the classpath of the H2 database process? I.e. when starting up the H2 database?
Another way to do this is to fork H2 and build it including your custom aggregate function. Or just put your .class file inside the H2 jar file, depending on how often you have to update your own code, this might be ok, too. Then it will certainly be on the classpath...
In the worst case, the H2 user group is quite active:
http://groups.google.com/group/h2-database
当 H2 服务器作为外部进程运行时,可以使用 java -cp
使用
h2.sh
或h2.bat
启动H2服务器时,可以将该目录添加到环境变量%H2DRIVERS%
或%CLASSPATH%
。When running the H2 server as an external process, the classes can be added to the classpath using
java -cp <dir> ...
.When using the
h2.sh
orh2.bat
to start the H2 server, you can add the directory to the environment variables%H2DRIVERS%
or%CLASSPATH%
.