通过Java程序使用选择器从cloudant DB获取数据时获取异常

时间:2016-01-30 13:37:22

标签: java cloudant

我按照以下结构在CloudantDB中创建了文档。

{
  "_id": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  "_rev": "XXXXXXXXXXXXXXXXXXXXXXXXX",
  "SOCIALKEY": "XXXXXXXXXXXXXXXX",
  "SCREEN_USER": "XXXXXXXXXXXXX",
  "TWEETS_FEED": "XXXXXXXXXXXXXXXXXXXXXXXXX",
  "LOCATION": "XXXXXXXXXXXXXXX",
  "CREATEDATE": "XXXXXXXXXXXXXXX",
  "ANALYSIS_STATUS": "N",
  "SENTIMENT_STATUS": "NA",
  "MIXED": "NA",
  "SCORE": "NA"
}

我正在尝试检索符合条件的所有文件" ANALYSIS_STATUS" =" N"。我正在使用CloudantClient.jar连接CloudantDB&执行数据库操作。 我在下面写了代码来检索文档。

public List<TweetsFeed> getTweetFeed() {
        List<TweetsFeed> tweetList=new ArrayList<TweetsFeed>();
        String selector="\"selector\": {\"ANALYSIS_STATUS\" : \"N\"},\"fields\": [\"_id\",\"TWEETS_FEED\"],\"limit\": 10,\"skip\": 0";
        try{
            db = CloudantClientMgr.getDB();
            tweetList=db.findByIndex(selector,TweetsFeed.class);
            System.out.println("size==>"+tweetList.size());
        }catch(Exception e){
            e.printStackTrace();
        }
        return tweetList;
    }

以下是TweetsFeed.class结构

public class TweetsFeed {
    public Integer id;
    public String socialKey;
    public String screenUser;
    public String tweetsFeed;
    public String location;
    public Date createdate;
    public String analysisStatus;
......
}

执行后我遇到错误。

[err] com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 27
[err]   at com.google.gson.Gson.assertFullConsumption(Gson.java:779)
[err]   at com.google.gson.Gson.fromJson(Gson.java:769)
[err]   at com.google.gson.Gson.fromJson(Gson.java:717)
[err]   at com.google.gson.Gson.fromJson(Gson.java:689)
[err]   at com.cloudant.client.api.Database.getFindByIndexBody(Database.java:1102)
[err]   at com.cloudant.client.api.Database.findByIndex(Database.java:360)
[err]   at com.cloudant.client.api.Database.findByIndex(Database.java:323)
[err]   at com.ibm.dao.CloudantDaoImpl.getTweetFeed(CloudantDaoImpl.java:254)
[err]   at com.ibm.service.SocialKeyManagerImpl.getTweetFeed(SocialKeyManagerImpl.java:78)
[err]   at com.ibm.scheduler.SentimentAnalysis.insertAnalysis(SentimentAnalysis.java:41)
[err]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[err]   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[err]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[err]   at java.lang.reflect.Method.invoke(Method.java:606)
[err]   at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:283)
[err]   at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:272)
[err]   at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
[err]   at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
[err]   at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
[err] Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 27
[err]   at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
[err]   at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386)
[err]   at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:531)
[err]   at com.google.gson.stream.JsonReader.peek(JsonReader.java:414)
[err]   at com.google.gson.Gson.assertFullConsumption(Gson.java:775)
[err]   ... 18 more
Size of List---->0
Data not available

代码有什么问题?

2 个答案:

答案 0 :(得分:2)

selector字符串应该只是选择器的JSON。 您需要使用FindByIndexOptions为索引查询指定其他参数,并将其提供给findByIndex方法,如下所示:

String selector="\"selector\": {\"ANALYSIS_STATUS\" : \"N\"}";
FindByIndexOptions options = new FindByIndexOptions()
            .fields("_id")
            .fields("TWEETS_FEED")
            .limit(10)
            .skip(0);

db.findByIndex(selector, TweetsFeed.class, options);

答案 1 :(得分:0)

错误表示格式错误的JSON输入 - 选择器需要是用{}封装的完整JSON格式 -

尝试下面的一个 -

String selector="{\"selector\": {\"ANALYSIS_STATUS\" : \"N\"},\"fields\": [\"_id\",\"TWEETS_FEED\"],\"limit\": 10,\"skip\": 0}";