2012-03-05 14 views
7

Estoy aprendiendo Mahout y leyendo "Mahout en acción".Obtener una IOException al ejecutar un código de muestra en "Mahout en acción" en mahout-0.6

Cuando intenté ejecutar el código de ejemplo en Capítulo7 SimpleKMeansClustering.java, una excepción apareció:

Excepción en el hilo "principal" java.io.IOException: malo clase de valor: 0,0: nulo no es org clase .apache.mahout.clustering.WeightedPropertyVectorWritable en org.apache.hadoop.io.SequenceFile $ Reader.next (SequenceFile.java:1874) en SimpleKMeansClustering.main (SimpleKMeansClustering.java:95)

I successed este código en mahout -0.5, pero en mahout-0.6 vi esta excepción. Incluso cambié el nombre del directorio de clusters-0 a clusters-0-final, todavía estoy frente a esta excepción.

KMeansDriver.run(conf, vectors, new Path(canopyCentroids, "clusters-0-final"), clusterOutput, new TanimotoDistanceMeasure(), 0.01, 20, true, false);//First, I changed this path. 

    SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path("output/clusters/clusteredPoints/part-m-00000"), conf);//I double checked this folder and filename. 

    IntWritable key = new IntWritable(); 
    WeightedVectorWritable value = new WeightedVectorWritable(); 
    int i=0; 
    while(reader.next(key, value)) { 
     System.out.println(value.toString() + " belongs to cluster " + key.toString()); 
     i++; 
    } 
    System.out.println(i); 
    reader.close(); 

¿Alguien tiene alguna idea sobre esta excepción? He estado tratando de resolverlo durante mucho tiempo y no tengo ni idea. Y hay pocas fuentes en internet.

Gracias de antemano

+1

Por lo general significa que su entrada está vacía o malformado. También tenga en cuenta que el libro va con Mahout 0.5, sin embargo, en general, no esperaría problemas al usar los ejemplos con 0.6. Sin embargo, no puedo decirlo con seguridad. –

+0

Gracias Sean Owen. Voy a ir con Mahout 0.5 entonces. :) – Nebulach

Respuesta

4

Para que este ejemplo funcione en Mahout 0,6, se suman

import org.apache.mahout.clustering.WeightedPropertyVectorWritable; 

a las importaciones y vuelva a colocar la línea:

WeightedVectorWritable value = new WeightedVectorWritable(); 

por

WeightedPropertyVectorWritable value = new WeightedPropertyVectorWritable(); 

T Esto sucede porque el código de Mahout 0.6 escribe los valores de salida del clúster en el nuevo tipo WeightedPropertyVectorWritable.

2

El ejemplo en el libro funciona bien para mahout 05 con los siguientes cambios pequeños:

(1) establece los caminos correctamente:

KMeansDriver.run(conf, new Path("testdata/points"), new Path("testdata/clusters"), new Path("testdata/output"), new EuclideanDistanceMeasure(), 0.001, 10, true, false); 

y

SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path("testdata/output/clusteredPoints/part-m-0"), conf); 

(2) también si no tiene HADOOP instalado, entonces necesita cambiar el último parámetro de la llamada KMeansDriver.run() de 'falso' a 'verdadero'.

KMeansDriver.run(conf, new Path("testdata/points"), new Path("testdata/clusters"), new Path("testdata/output"), new EuclideanDistanceMeasure(), 0.001, 10, true, true); 

Luego, el ejemplo funciona.

0

Reemplazar

import org.apache.mahout.clustering.WeightedVectorWritable; 

con

import org.apache.mahout.clustering.classify.WeightedVectorWritable; 
3

Para quien pueda interesar, aquí es una muestra MIA trabajar para mahout 0,9:

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.FileSystem; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.IntWritable; 
import org.apache.hadoop.io.LongWritable; 
import org.apache.hadoop.io.SequenceFile; 
import org.apache.hadoop.io.Text; 
import org.apache.mahout.clustering.Cluster; 
import org.apache.mahout.clustering.classify.WeightedPropertyVectorWritable; 
import org.apache.mahout.clustering.kmeans.KMeansDriver; 
import org.apache.mahout.clustering.kmeans.Kluster; 
import org.apache.mahout.common.distance.EuclideanDistanceMeasure; 
import org.apache.mahout.math.RandomAccessSparseVector; 
import org.apache.mahout.math.Vector; 
import org.apache.mahout.math.VectorWritable; 

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

public class SimpleKMeansClustering { 

    public static final double[][] points = { 
      {1, 1}, {2, 1}, {1, 2}, 
      {2, 2}, {3, 3}, {8, 8}, 
      {9, 8}, {8, 9}, {9, 9}}; 

    public static void writePointsToFile(List<Vector> points, 
             String fileName, 
             FileSystem fs, 
             Configuration conf) throws IOException { 
     Path path = new Path(fileName); 
     SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, 
       path, LongWritable.class, VectorWritable.class); 
     long recNum = 0; 
     VectorWritable vec = new VectorWritable(); 
     for (Vector point : points) { 
      vec.set(point); 
      writer.append(new LongWritable(recNum++), vec); 
     } 
     writer.close(); 
    } 

    public static List<Vector> getPoints(double[][] raw) { 
     List<Vector> points = new ArrayList<Vector>(); 
     for (int i = 0; i < raw.length; i++) { 
      double[] fr = raw[i]; 
      Vector vec = new RandomAccessSparseVector(fr.length); 
      vec.assign(fr); 
      points.add(vec); 
     } 
     return points; 
    } 

    public static void main(String args[]) throws Exception { 

     int k = 2; 

     List<Vector> vectors = getPoints(points); 

     File testData = new File("clustering/testdata"); 
     if (!testData.exists()) { 
      testData.mkdir(); 
     } 
     testData = new File("clustering/testdata/points"); 
     if (!testData.exists()) { 
      testData.mkdir(); 
     } 

     Configuration conf = new Configuration(); 
     FileSystem fs = FileSystem.get(conf); 
     writePointsToFile(vectors, "clustering/testdata/points/file1", fs, conf); 

     Path path = new Path("clustering/testdata/clusters/part-00000"); 
     SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, path, Text.class, Kluster.class); 

     for (int i = 0; i < k; i++) { 
      Vector vec = vectors.get(i); 
      Kluster cluster = new Kluster(vec, i, new EuclideanDistanceMeasure()); 
      writer.append(new Text(cluster.getIdentifier()), cluster); 
     } 
     writer.close(); 

     KMeansDriver.run(conf, 
       new Path("clustering/testdata/points"), 
       new Path("clustering/testdata/clusters"), 
       new Path("clustering/output"), 
       0.001, 
       10, 
       true, 
       0, 
       true); 

     SequenceFile.Reader reader = new SequenceFile.Reader(fs, 
       new Path("clustering/output/" + Cluster.CLUSTERED_POINTS_DIR + "/part-m-0"), conf); 

     IntWritable key = new IntWritable(); 
     WeightedPropertyVectorWritable value = new WeightedPropertyVectorWritable(); 
     while (reader.next(key, value)) { 
      System.out.println(value.toString() + " belongs to cluster " + key.toString()); 
     } 
     reader.close(); 
    } 

} 
+0

¡Guau!¡Muchas gracias, funcionó! He estado depurando el código de muestra durante 0.7 horas. – nilsi

Cuestiones relacionadas