0

我正在做一个应用程序来处理存储在mongoDBusing中的一些数据Hadoop。我正在编写程序java

问题是我有一个包含数组的子文档,我想获取数组的一个属性的值。我会举一个例子来更清楚地看到它。

 "entities" : {
            "hashtags" : [
                    {
                            "**text**" : "whatever",
                            "indices" : [
                                    59,
                                    69
                            ]
                    },
                    {
                            "**text**" : "whatever",
                            "indices" : [
                                    82,
                                    95
                            ]
                    }
            ],
            "urls" : [ ],
            "user_mentions" : [ ]
    },

文本的值是我要处理的值。

所以我用Java开发了一个程序,它在映射器类中报告了以下错误:

java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to java.lang.String
    at HashTagsMapper.map(HashTagsMapper.java:27)
    at HashTagsMapper.map(HashTagsMapper.java:18)
    at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:144)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:764)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:370)
    at org.apache.hadoop.mapred.Child$4.run(Child.java:255)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:415)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1121)
    at org.apache.hadoop.mapred.Child.main(Child.java:249)

这是映射器类 -->

public class HashTagsMapper extends Mapper<Object, BSONObject, Text, IntWritable>
{   
    public void map(Object key, BSONObject value, Context context) throws IOException, InterruptedException
    {

        ArrayList <String> name = new ArrayList<String>();
        BSONObject entities = (BSONObject) value.get("entities");
        BasicDBList hashtags = (BasicDBList) entities.get("hashtags");
        for(int index = 0; index < hashtags.size(); index++){
            name.add((String) hashtags.get(index));
        }
        try{
           FileWriter fw = new FileWriter("/home/jonrodriguez/Hashtags.txt");
           PrintWriter escribirListaRedundantes = new PrintWriter(fw);

           escribirListaRedundantes.println(name);

           fw.close();

        }

           catch(java.io.IOException ioex){}
        for(int i = 0; i < name.size(); i++){
            context.write(new Text(name.get(i)), new IntWritable(1));
        }
    }

谁能帮我?谢谢!

4

1 回答 1

0

问题是类转换异常。你为什么不试着写不

(String)hashtags.get(index)

, 但

hashtags.get(index).toString()

问题可能是 basicDbList 是 BDBObjects 列表,您不能将父级强制转换为子级。

于 2013-02-06T13:51:14.183 回答