<strong><span style= "FONT-SIZE:18PX;" >/*** * @author Yangxin * @info prepare the vector used for mahout * Convert apple information into input vector */package uniteight;import java.util.arraylist;import ja Va.util.list;import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.fs.filesystem;import Org.apache.hadoop.fs.path;import Org.apache.hadoop.io.sequencefile;import Org.apache.hadoop.io.text;import Org.apache.mahout.math.densevector;import Org.apache.mahout.math.namedvector;import org.apache.mahout.math.vectorwritable;/** * We can use the name or description of the vector as the key, here is the namevector, and the vector itself as the value.* The mahout vector class does not implement the writable interface. To avoid their direct coupling with Hadoop.
* But can use the Vectorwritable class to encapsulate a vector and make it a writable.
* That is, vectors in Mahout can be written to Sequencefile using the Vectorwritable class.
*/public class Applestovectors {public static void main (string[] args) throws exception{list<namedvector> apples = New Arraylist<namedvector> (); Namedvector apple;apple = new Namedvector (new Densevector (New double[]{0.11, 510, 1}), Small round green apple); APPLES.A DD (apple), Apple = new Namedvector (new Densevector (new double[]{0.23, 650, 3}), Large oval Red apple, Apples.add (Apple); Apple = new Namedvector (new Densevector (new double[]{0.09, 630, 1}), Small elongated red apple), Apples.add (Apple), Apple = new Namedvector (new Densevector (New double[]{0.25, 590, 3}), "Large round yellow apple"); Apples.add (apple); Apple = new Namedvector (New Densevector (New double[]{0.18, 520, 2}), "Medium Oval green apple"); Configuration conf = new configuration (); FileSystem fs = Filesystem.get (conf); Path PATH = new Path ("E:\\apples.txt"); Sequencefile.writer Writer = new Sequencefile.writer (FS, conf, path, Text.class, Vectorwritable.class); Vectorwritable VEC = new vectorwritable (); for (namedvector Vector:apples) {vec.set (vector); Writer.append (New Text (Vector.getname ()), VEC);} Writer.close (); Sequencefile.reader Reader = new Sequencefile.reader (FS, New Path ("E:\\apples.txt"), conf); Text key = new text (); Vectorwritable value = new Vectorwritable (), while (Reader.next (key, value)) {System.out.println (key.tostring () + "" + Value.get (). asformatstring ());;} Reader.close ();}} </span></strong>
Prepare the vector used by the mahout applestovectors