我将POJO插入到Solr中,现在尝试查询和反序列化为POJO。在尝试实例化POJO(TwitterStatusModel)时,它会抛出IllegalArgumentException。
我的理解是,由于我使用String(插入TrieDateField)将日期插入Solr,我应该能够基于相同的数据类型提取和重新实例化,但是Solr希望将statusCreatedDate设置为一个java.util.Date?还是一个字符串?
非常感谢任何帮助。
注意:我在插入Solr之前正确格式化日期字符串(根据TrieDateField要求)。
注意:当弹出此错误时,两个setter(一个接受java.util.Date,另一个接受String)都命名为setStatusCreatedDate。我重新命名了一个试图缩小问题的范围。
以下是我的架构:
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="MySolrModel" version="1.5">
<fieldType name="double" class="solr.TrieDoubleField" />
<fieldType name="long" class="solr.TrieLongField" positionIncrementGap="0"/>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />
<fieldType name="text_en" class="solr.TextField" >
<analyzer type="index">
<tokenizer class="solr.ClassicTokenizerFactory" />
<filter class="solr.StopFilterFactory" ignoreCase="true" words="lang/stopwords_en.txt"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<fieldType name="date" class="solr.TrieDateField"/>
<fieldType name="int" class="solr.TrieIntField" />
<fieldType name="boolean" class="solr.BoolField" />
<uniqueKey>id</uniqueKey>
<defaultSearchField>statusText</defaultSearchField>
<field name="_version_" type="long" indexed="true" stored="true"/>
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="statusText" type="text_en" indexed="true" stored="true" required="true" multiValued="false" />
<field name="userName" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="placeCountry" type="string" indexed="true" stored="true" required="false" multiValued="false" />
<field name="statusContributorsList" type="long" indexed="false" stored="true" required="false" multiValued="true" />
<field name="statusCreatedDate" type="date" indexed="true" stored="true" required="true" multiValued="false" />
<dynamicField name="*Lat" type="double" indexed="true" stored="true" required="false" multiValued="false" />
<dynamicField name="*Lon" type="double" indexed="true" stored="true" required="false" multiValued="false" />
<dynamicField name="*Int" type="int" indexed="false" stored="true" required="false" multiValued="false" />
<dynamicField name="*Boolean" type="boolean" indexed="false" stored="true" required="false" multiValued="false" />
<dynamicField name="*Long" type="long" indexed="false" stored="true" required="false" multiValued="false" />
<dynamicField name="*String" type="string" indexed="false" stored="true" required="false" multiValued="false" />
</schema>
以下是我认为是POJO的相关部分。正如您所看到的,我有一个接受String的setter和另一个接受Date的setter。我试着对它们进行评论,只是为了看看是否错误的是干扰而没有这样的运气。此外,在出现此错误后,我插入了@JsonSetter和@JsonIgnoreProperties注释。
public class TwitterStatusModel {
@Field("statusCreatedDate")
@JsonProperty
private String statusCreatedDate = "";
public String getStatusCreatedDate() {
return this.statusCreatedDate;
}
@JsonSetter
public void setStatusCreatedDate(String statusCreatedDate) {
this.statusCreatedDate = statusCreatedDate;
}
// @JsonIgnoreProperties
// public void setStatusCreatedDateFromDate(Date created) {
// this.statusCreatedDate = (DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.S'Z'")).print(new DateTime(created));
// }
这是将POJO插入Solr的代码,然后是从Solr中提取它们的代码。
public void insertDocumentList(List<TwitterStatusModel> tweetList) {
try {
server.addBeans(tweetList);
} catch (IOException ioe) {
TwitterCollectionScheduleBean.log.error(ioe);
} catch (SolrServerException sse) {
TwitterCollectionScheduleBean.log.error(sse);
}
}
public void writeRecent() {
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
List<TwitterStatusModel> list = query(query).getBeans(TwitterStatusModel.class);
}
public QueryResponse query(SolrQuery query) {
QueryResponse resp = null;
try {
resp = server.query(query);
} catch (SolrServerException sse) {
TwitterCollectionScheduleBean.log.error(sse);
}
return resp;
}
最后,以下是例外。我试图尽可能地减少它......
01:26:52,995 ERROR [org.jboss.as.ejb3] (ServerService Thread Pool -- 101) javax.ejb.EJBTransactionRolledbackException: Could not instantiate object of class com.mycompany.MyProject.twitter.util.TwitterStatusModel
01:26:52,996 ERROR [org.jboss.as.ejb3.invocation] (ServerService Thread Pool -- 101) JBAS014134: EJB Invocation failed on component JsonSerializer for method public void com.mycompany.MyProject.json.JsonSerializer.writeRecent(): javax.ejb.EJBTransactionRolledbackException: Could not instantiate object of class com.mycompany.MyProject.twitter.util.TwitterStatusModel
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleInCallerTx(CMTTxInterceptor.java:163) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:253) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:342) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:95) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:55) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:326)
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:448)
at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:61)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:326)
at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:185)
at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:182)
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:73)
at com.mycompany.MyProject.json.JsonSerializer$$$view86.writeRecent(Unknown Source) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_75]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_75]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_75]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_75]
at org.jboss.weld.util.reflection.Reflections.invokeAndUnwrap(Reflections.java:414) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:127) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.bean.proxy.InjectionPointPropagatingEnterpriseTargetBeanInstance.invoke(InjectionPointPropagatingEnterpriseTargetBeanInstance.java:65) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:100) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
at com.mycompany.MyProject.json.JsonSerializer$Proxy$_$$_Weld$EnterpriseProxy$.writeRecent(Unknown Source) [classes:]
at com.mycompany.MyProject.scheduler.TwitterCollectionScheduleBean.init(TwitterCollectionScheduleBean.java:86) [classes:]
Caused by: org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class com.mycompany.MyProject.twitter.util.TwitterStatusModel
at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:68) [solr-solrj-4.10.3.jar:4.10.3 1644336 - mark - 2014-12-10 00:35:46]
at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBeans(DocumentObjectBinder.java:47) [solr-solrj-4.10.3.jar:4.10.3 1644336 - mark - 2014-12-10 00:35:46]
at org.apache.solr.client.solrj.response.QueryResponse.getBeans(QueryResponse.java:536) [solr-solrj-4.10.3.jar:4.10.3 1644336 - mark - 2014-12-10 00:35:46]
at com.mycompany.MyProject.json.JsonSerializer.writeRecent(JsonSerializer.java:36) [classes:]
Caused by: org.apache.solr.client.solrj.beans.BindingException: Exception while setting value : Tue Feb 17 10:48:39 EST 2015 on private java.lang.String com.mycompany.MyProject.twitter.util.TwitterStatusModel.statusCreatedDate
at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:370) [solr-solrj-4.10.3.jar:4.10.3 1644336 - mark - 2014-12-10 00:35:46]
at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.inject(DocumentObjectBinder.java:353) [solr-solrj-4.10.3.jar:4.10.3 1644336 - mark - 2014-12-10 00:35:46]
at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:64) [solr-solrj-4.10.3.jar:4.10.3 1644336 - mark - 2014-12-10 00:35:46]
... 140 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.mycompany.MyProject.twitter.util.TwitterStatusModel.statusCreatedDate to java.util.Date
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) [rt.jar:1.7.0_75]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) [rt.jar:1.7.0_75]
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) [rt.jar:1.7.0_75]
at java.lang.reflect.Field.set(Field.java:741) [rt.jar:1.7.0_75]
at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:364) [solr-solrj-4.10.3.jar:4.10.3 1644336 - mark - 2014-12-10 00:35:46]
... 142 more
01:26:53,011 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 101) MSC000001: Failed to start service jboss.deployment.unit."MyProject-1.0-SNAPSHOT.war".component.TwitterCollectionScheduleBean.START: org.jboss.msc.service.StartException in service jboss.deployment.unit."MyProject-1.0-SNAPSHOT.war".component.TwitterCollectionScheduleBean.START: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
... 6 more
Caused by: javax.ejb.EJBTransactionRolledbackException: Could not instantiate object of class com.mycompany.MyProject.twitter.util.TwitterStatusModel
at com.mycompany.MyProject.json.JsonSerializer$Proxy$_$$_Weld$EnterpriseProxy$.writeRecent(Unknown Source)
at com.mycompany.MyProject.scheduler.TwitterCollectionScheduleBean.init(TwitterCollectionScheduleBean.java:86)
... 11 more
Caused by: org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class com.mycompany.MyProject.twitter.util.TwitterStatusModel
at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:68)
at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBeans(DocumentObjectBinder.java:47)
at org.apache.solr.client.solrj.response.QueryResponse.getBeans(QueryResponse.java:536)
at com.mycompany.MyProject.json.JsonSerializer.writeRecent(JsonSerializer.java:36)
... 101 more
Caused by: org.apache.solr.client.solrj.beans.BindingException: Exception while setting value : Tue Feb 17 10:48:39 EST 2015 on private java.lang.String com.mycompany.MyProject.twitter.util.TwitterStatusModel.statusCreatedDate
at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:370)
at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.inject(DocumentObjectBinder.java:353)
at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:64)
... 140 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.mycompany.MyProject.twitter.util.TwitterStatusModel.statusCreatedDate to java.util.Date
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) [rt.jar:1.7.0_75]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) [rt.jar:1.7.0_75]
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) [rt.jar:1.7.0_75]
at java.lang.reflect.Field.set(Field.java:741) [rt.jar:1.7.0_75]
at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:364)
... 142 more
01:26:53,029 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) JBAS014613: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"MyProject-1.0-SNAPSHOT.war\".component.TwitterCollectionScheduleBean.START" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"MyProject-1.0-SNAPSHOT.war\".component.TwitterCollectionScheduleBean.START: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
Caused by: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
Caused by: javax.ejb.EJBTransactionRolledbackException: Could not instantiate object of class com.mycompany.MyProject.twitter.util.TwitterStatusModel
Caused by: org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class com.mycompany.MyProject.twitter.util.TwitterStatusModel
Caused by: org.apache.solr.client.solrj.beans.BindingException: Exception while setting value : Tue Feb 17 10:48:39 EST 2015 on private java.lang.String com.mycompany.MyProject.twitter.util.TwitterStatusModel.statusCreatedDate
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.mycompany.MyProject.twitter.util.TwitterStatusModel.statusCreatedDate to java.util.Date"}}
答案 0 :(得分:0)
似乎即使我使用setter方法保留了类型转换:
public void setStatusCreatedDateFromDate(Date created) {
this.statusCreatedDate = (DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.S'Z'")).print(new DateTime(created));
}
Solr没有查看它可以提供的类型,它会查看POJO字段的类型。我不认为Solr会关心实际字段的类型,只要它得到并且可以使用正确的类型设置,但它确实如此。 Solr希望FIELD具有正确的类型。以下是解决方案:
@Field("statusCreatedDate")
@JsonProperty
private Date statusCreatedDate = new Date();
public void setStatusCreatedDate(Date created) {
this.statusCreatedDate = created;
}
我不知道是否有解决办法,但修复方法是简单地更改POJO。有趣的是,有一些记录被索引,其中statusCreatedDate是String格式的,而其他的是长格式的,强制我转储和重构数据,纠正POJO(使用java.util.Date类)并重新开始。
另外,我发现虽然Solr允许在Solr的String类型中插入POJO long类型,但它不会反序列化回POJO(String - &gt; long)。
对于读取此内容的任何人,请确保POJO中的类型与schema.xml类型完全一致。