`

Java中嵌入Javascript遭遇“"The choice of Java constructor replace matching JavaScript”

阅读更多

今天在使用Solr的DataImportHandler时,需要用一个JavaScript函数来对数据做预处理,写的JS代码如下:

 

<script>
<![CDATA[
var reg = new RegExp("[^\\d,]|^,+|,+$","g");
function cleanFJID(row){
  var val = row.get("FJ");
  var val = val.replace(reg,"");  //把无效字符去掉 
  if(val=="") val = "null";
  row.put('FJ', val);
  return row;
}
]]>
</script>

 这段代码如果在浏览器里执行,应该是没有问题的,但是DIH执行时却报错:

警告: transformer threw error
org.apache.solr.handler.dataimport.DataImportHandlerException: Could not invoke method :cleanFJID
 <script>
null</script> Processing Document # 1
	at org.apache.solr.handler.dataimport.ScriptTransformer.transformRow(ScriptTransformer.java:58)
	at org.apache.solr.handler.dataimport.EntityProcessorWrapper.applyTransformer(EntityProcessorWrapper.java:195)
	at org.apache.solr.handler.dataimport.EntityProcessorWrapper.nextRow(EntityProcessorWrapper.java:241)
	at org.apache.solr.handler.dataimport.DocBuilder.buildDocument(DocBuilder.java:357)
	at org.apache.solr.handler.dataimport.DocBuilder.doFullDump(DocBuilder.java:242)
	at org.apache.solr.handler.dataimport.DocBuilder.execute(DocBuilder.java:180)
	at org.apache.solr.handler.dataimport.DataImporter.doFullImport(DataImporter.java:331)
	at org.apache.solr.handler.dataimport.DataImporter.runCmd(DataImporter.java:389)
	at org.apache.solr.handler.dataimport.DataImporter$1.run(DataImporter.java:370)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.solr.handler.dataimport.ScriptTransformer.transformRow(ScriptTransformer.java:53)
	... 8 more
Caused by: javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: The choice of Java constructor replace matching JavaScript argument types (function,string) is ambiguous; candidate constructors are: 
    class java.lang.String replace(char,char)
    class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence) (<Unknown source>#6) in <Unknown source> at line number 6
	at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:184)
	at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:142)
	... 13 more
2010-6-18 1:43:40 org.apache.solr.handler.dataimport.DocBuilder buildDocument
严重: Exception while processing: XZ_SJFW document : SolrInputDocument[{}]

 

 

看字面的意思是JS的replace函数与java的replace方法混淆,导致javascript引擎不知该调用哪个方法,所以,我就使用了另外一种方法来引用正则表达式,修改后JS如下:

<script>
<![CDATA[
//var reg = new RegExp("[^\\d,]|^,+|,+$","g");
function cleanFJID(row){
  var val = row.get("FJ");
  var val = val.replace(/[^\\d,]|^,+|,+$/g,"");  //把无效字符去掉 
  if(val=="") val = "null";
  row.put('FJ', val);
  return row;
}
]]>
</script>

 

满以为这样修改就不会再有问题,可以一执行,还是报差不多的错:

警告: transformer threw error
org.apache.solr.handler.dataimport.DataImportHandlerException: Could not invoke method :cleanFJID
 <script>
null</script> Processing Document # 1
	at org.apache.solr.handler.dataimport.ScriptTransformer.transformRow(ScriptTransformer.java:58)
	at org.apache.solr.handler.dataimport.EntityProcessorWrapper.applyTransformer(EntityProcessorWrapper.java:195)
	at org.apache.solr.handler.dataimport.EntityProcessorWrapper.nextRow(EntityProcessorWrapper.java:241)
	at org.apache.solr.handler.dataimport.DocBuilder.buildDocument(DocBuilder.java:357)
	at org.apache.solr.handler.dataimport.DocBuilder.doFullDump(DocBuilder.java:242)
	at org.apache.solr.handler.dataimport.DocBuilder.execute(DocBuilder.java:180)
	at org.apache.solr.handler.dataimport.DataImporter.doFullImport(DataImporter.java:331)
	at org.apache.solr.handler.dataimport.DataImporter.runCmd(DataImporter.java:389)
	at org.apache.solr.handler.dataimport.DataImporter$1.run(DataImporter.java:370)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.solr.handler.dataimport.ScriptTransformer.transformRow(ScriptTransformer.java:53)
	... 8 more
Caused by: javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: The choice of Java constructor replace matching JavaScript argument types (function,string) is ambiguous; candidate constructors are: 
    class java.lang.String replace(char,char)
    class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence) (<Unknown source>#5) in <Unknown source> at line number 5
	at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:184)
	at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:142)
	... 13 more
2010-6-18 1:46:50 org.apache.solr.handler.dataimport.DocBuilder buildDocument
严重: Exception while processing: XZ_SJFW document : SolrInputDocument[{}]

 

这下我可傻眼了,搞不懂是什么原因了

上网一顿狠找,找到洋鬼子的一个贴子:

http://www.mirthcorp.com/community/forums/showthread.php?t=235

 

英文水平差,意思没看明白,但看到5楼有人回复,建议把字符串改成

var input = new String(strOut);

再调用replace试试,我照猫画虎,也把代码改成这样:

<script>
<![CDATA[
//var reg = new RegExp("[^\\d,]|^,+|,+$","g");
function cleanFJID(row){
  var val = row.get("FJ");
  var str = new String(val);
  var val = str.replace(/[^\d,]|^,+|,+$/g,"");  //把无效字符去掉 
//  var val = val.replace(reg,"");  //把无效字符去掉 
  if(val=="") val = "null";
  row.put('FJ', val);
  return row;
}
]]>
</script>

 

再试了一下,我的乖乖,问题解决了。不过仅管问题已经解决,但其中的子丑寅卯还是没弄明白

 

分享到:
评论
1 楼 hoszb 2010-09-08  
java有个script引擎,不导入第三方jar的情况下可以处理javascript,导入第三方jar的话,可以处理python,perl等

我实验过,java中运行js的效率极其低下,我的一个实验中同样的js代码运行时间是java的10倍,所以要做预处理的话还是扩展solr的Transformer接口吧,功能更多而且效率更好

相关推荐

    java反射之Constructor

    Java 反射机制中的 Constructor 类提供了关于类的单个构造方法的信息,包括对它的访问权限、Class 获取构造函数的方式等。下面将详细讲解 Constructor 类的概述、获取构造函数的方式、构造类的实例概述等。 ...

    javascript.the.definitive.guide.5th.2006(英文版)

    This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including: ...

    韩顺平培训学员面试资料涵盖Java+javascript

    通常情况下, JavaScript中建立一个对象用"new"加上constructor function来实现. 如new Date(), new Object()等. var book = new Object(); book.name = "JavaScript is Cool"; book.author = "tom"; book.pages = ...

    JavaScript中constructor()方法的使用简介

    在JavaScript编程语言中,constructor()方法是一个非常重要的概念,它属于对象的一个属性,用于指明创建该对象的构造函数。对初学者来说,理解constructor()方法是学习JavaScript对象和原型链的基础。 首先,...

    Java反射之Constructor、Method、Field使用及说明

    在深入探讨Java反射机制中`Constructor`、`Method`、`Field`以及`Class`类的使用之前,我们需要先了解反射的基本概念及其重要性。反射是Java的一项强大功能,它允许程序在运行时检查类和对象的结构,甚至可以创建和...

    JavaScript精炼之构造函数 Constructor及Constructor属性详解

    对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数,除了创建对象,构造函数(constructor) 还做了另一件有用的事情—自动为创建的新对象设置了原型对象(prototype object)

    The Art and Science of Java

    《The Art and Science of Java》是一本专注于Java编程的书籍,它深入浅出地探讨了Java语言的艺术与科学。这份资料包含PPT演示文稿和配套的代码示例,为学习者提供了理论与实践相结合的全面学习体验。下面将详细阐述...

    JavaScript的constructor属性[文].pdf

    JavaScript constructor 属性在类型检查中的应用 在 JavaScript 中,constructor 属性是一种非常有用的工具,可以帮助我们检查变量的类型。Constructor 属性可以帮助我们解决 typeof 函数无法解决的问题,即无法...

    深化浅析JavaScript中的constructor_.docx

    深化浅析JavaScript中的constructor_ constructor 属性是 JavaScript 中的一种特殊属性,它返回对创建此对象的数组函数的引用。下面我们来深入浅析 JavaScript 中的 constructor。 constructor 属性是一个非标准...

    JavaScript权威指南第五版【新】(犀牛书)

    This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including: ...

    Learn Java 17 Programming 2nd Edition

    The title "Learn Java 17 Programming 2nd Edition" indicates that this book is an updated guide designed to teach the basics of Java programming, specifically focusing on Java 17. The description ...

    javascript new后的constructor属性.docx

    ### JavaScript中的`new`操作与`constructor`属性详解 #### 一、`new`操作符的理解 在JavaScript中,`new`操作符被用于创建一个由构造函数定义的新实例对象。当使用`new`关键字调用一个构造函数时,会执行以下步骤...

    Secrets of the JavaScript Ninja, 2nd Edition

    ### JavaScript Ninja Secrets: Key Insights from "Secrets of the JavaScript Ninja, 2nd Edition" In the world of web development, JavaScript has become the lingua franca, powering applications across ...

    深入浅析JavaScript中的constructor

    在JavaScript中,`constructor`属性是一个非常重要的概念,它与对象和类的构造函数紧密相关。构造函数是一种特殊类型的函数,通常用于初始化新创建的对象。当我们谈论`constructor`时,我们指的是一个对象实例的`...

    Harnessing the Power of Java 8 Lambda Expressions

    根据提供的文件信息,此文档是一本关于Java 8中Lambda表达式的书籍的一部分,具体书名为《Functional Programming in Java - Harnessing the Power of Java 8 Lambda Expressions》,作者是Venkat Subramaniam,出版...

    java实验7.1(CollegeCourse.java)

    Write a program called CollegeCourse that computes the cost of taking a college course. Include a constructor that requires a course ID number. Add a subclass Lab to compute a lab fee for a course ...

    JavaScript权威指南(第五版).chm

    This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including: ...

    Javascript 面向对象的JavaScript进阶

    尽管JavaScript没有传统意义上的类继承(如Java),但它支持基于原型的继承。通过`__proto__`链或`Object.setPrototypeOf()`方法,子类可以访问和重写父类的方法。 **示例代码:** ```javascript function Animal...

Global site tag (gtag.js) - Google Analytics