mongoDB
を使用して保存されたデータを処理するアプリケーションを実行していますHadoop
。でプログラムを書いていjava
ます。
問題は、配列を含むサブドキュメントがあり、配列の 1 つの属性の値を取得したいということです。より明確にするために例を挙げます。
"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));
}
}
誰でも私を助けることができますか?ありがとう!