java - 将 List<String> 转换为文本数组 MapReduce 时出现 ArrayStoreException

标签 java mapreduce

在mapreduce代码中,将列表转换为(org.apache.hadoop.io.Text)文本类型的数组时,收到ArrayStoreException。

List<String> testList= new ArrayList<String>();
            testList.add("testData1");
            testList.add("testData2");

            Text[] testArray=testList.toArray(new Text[testList.size()]);

但是当我不向列表添加任何值然后将其转换为数组时,它工作正常(使用空值)。有人可以指出我的错误吗?

最佳答案

您无法存储String位于 Text数组(Text[])。

您可以将它们存储在 String 中数组(String[]):

String[] testArray=testList.toArray(new String[testList.size()]);

when i dont add any value to the list and then convert it to array, it works fine

它仅在本例中创建空数组时才有效,因此其中不会存储任何内容。

如果您必须出示Text[]包含来自源 List<String> 的数据,您必须迭代 List 并自己生成 Text 实例:

Text[] testArray = new Text[testList.size()];
for (int i = 0; i < testList.size(); i++) {
    testArray[i] = new Text(testList.get(i)); // assuming the Text class 
                                              // has such a constructor 
}

关于java - 将 List<String> 转换为文本数组 MapReduce 时出现 ArrayStoreException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006298/

相关文章:

hadoop - canopy聚类算法中如何增加reducer的数量

java - 强制hadoop将 map task 的数量设置为1

java - 无法使用 LIKE 从 PostgreSQL 选择 Unicode 数据

java - 使用数据组创建条形图

java - 有时 Android BroadcastReceiver 不起作用

hadoop - 为什么在mapred-site.xml hadoop 2上有一个mapreduce.jobtracker.address属性

hadoop - 有点挑战性但很有趣的话题

python - 读取 block 中的 csv 文件时出现内存不足错误

java - FacesContext.getCurrentInstance().getApplication().getMessageBundle() 返回哪个 messageBundle?

java - 缺少 Java Play 项目所需的 activator-dist 目录