写入DataStax Solr表

时间:2013-12-04 05:40:34

标签: solr cassandra datastax

我试图从一个Solr表中读取并将文档写入另一个在不同键空间中的文档。这是我使用的代码的破败版本:

public static void main(String[] args ) {
    HttpSolrServer solrServer = new HttpSolrServer(sourceSolrTableUrl);
    solrServer.setParser(new XMLResponseParser());

    HttpSolrServer targetSolrServer = new HttpSolrServer(targetSolrTableUrl);

    SolrQuery query1 = new SolrQuery();
    query1.setQuery( "dev_key:T*" );

    QueryResponse query = solrServer.query(query1);
    SolrDocumentList solrDocList = query.getResults();

    Collection<SolrInputDocument> inputDocs = new ArrayList<SolrInputDocument>();
    for (SolrDocument doc : solrDocList) {
        counter++;
        SolrInputDocument inputDoc = new SolrInputDocument();

        value = (String) doc.get(DEV_KEY);
        addToDoc(inputDoc, DEV_KEY, value);

        value = Long.toString((Long)doc.get(DEVICE_ID));
        addToDoc(inputDoc, DEVICE_ID, value);

        value = (String) doc.get(DEVICE_TYPE);
        addToDoc(inputDoc, DEVICE_TYPE, value);

        value = df.format((Date) doc.get(DEVICE_MFG_DATE));
        addToDoc(inputDoc, DEVICE_MFG_DATE, value);

        value = (String) doc.get(DEV_MODEL);
        addToDoc(inputDoc, DEV_MODEL, value);

        inputDocs.add(inputDoc);
         System.out.println(counter + "\t" + value);

        if (inputDocs.size()>5) {
            break;
        }
    }

    try {
        targetSolrServer.add(inputDocs);
        targetSolrServer.commit();
    } catch (SolrServerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void addToDoc(SolrInputDocument doc, String fieldName, String value) {
    doc.addField(fieldName, value);
}

当我运行此代码时,这是我得到的错误:

Exception in thread "main" java.lang.RuntimeException: Invalid version (expected 2, but 60) or the data in not in 'javabin' format
at org.apache.solr.common.util.JavaBinCodec.unmarshal(JavaBinCodec.java:109)
at org.apache.solr.client.solrj.impl.BinaryResponseParser.processResponse(BinaryResponseParser.java:41)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:384)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:181)
at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:117)
at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:68)
at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:54)
at com.xyz.SolrPopulater.main(SolrPopulater.java:114)

代码似乎在以下行中断:

targetSolrServer.add(inputDocs);

服务器上运行的Solr版本为4.0.0。 我在我的客户端代码中使用了Solr-4.0.0-BETA jar。

有人能给我一些关于可能出错的指示吗?

1 个答案:

答案 0 :(得分:1)

发现我的错误。我试图插入一个日期值,该值不符合schema.xml中的格式。

我想这告诉我应该确保我将来尝试插入Solr的所有数据都应该与schema.xml格式匹配。

摘自我的schema.xml

<!--

 The format for this date field is of the form 1995-12-31T23:59:59Z, and
         is a more restricted form of the canonical representation of dateTime
         http://www.w3.org/TR/xmlschema-2/#dateTime    
         The trailing "Z" designates UTC time and is mandatory.
         Optional fractional seconds are allowed: 1995-12-31T23:59:59.999Z
         All other components are mandatory.

         Expressions can also be used to denote calculations that should be
         performed relative to "NOW" to determine the value, ie...

               NOW/HOUR
                  ... Round to the start of the current hour
               NOW-1DAY
                  ... Exactly 1 day prior to now
               NOW/DAY+6MONTHS+3DAYS
                  ... 6 months and 3 days in the future from the start of
                      the current day

         Consult the DateField javadocs for more information.

         Note: For faster range queries, consider the tdate type

-->
<fieldType name="date" class="solr.TrieDateField" omitNorms="true" precisionStep="0"    positionIncrementGap="0"/>