获取子文件mongodb中的数组的值

时间:2013-02-06 10:27:34

标签: java mongodb hadoop

我正在使用mongoDB处理Hadoop中存储的一些数据的应用程序。我正在java编写程序。

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

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

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

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

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));
        }
    }

任何人都可以帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

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

(String)hashtags.get(index)

,但是

hashtags.get(index).toString()

问题可能是basicDbList是BDBObjects的List,你不能将父类型转换为子级。