Come costruire un indice invertito Con MapReduce

May 6

Come costruire un indice invertito Con MapReduce


MapReduce è un modello di programmazione parallela sviluppato in Google per grandi insiemi di dati. Essa elabora i dati in blocchi, piuttosto che in ordine sequenziale. In tal modo, si basa su una mappa di funzioni di ingresso appaiati (chiavi) e valori che mette poi attraverso la funzione di ridurre - in tal modo, il suo nome - per rendere i dati più facile da capire. Invece di fornire la funzione di carta con una chiave e un valore, un inverted coppie Indice parole e documenti per la ricerca di testo. È possibile utilizzare gli indici invertiti in MapReduce per creare un indice per una ricerca per parole chiave, per esempio.

istruzione

1 Digitare il seguente codice per la funzione mappa:

class InvertedIndexerMapper public static estende MapReduceBase

implements Mapper<LongWritable, Text, Text, Text>

{

private final static Text word = new Text () ;
private final static Text location = new Text () ;

mappa public void (chiave LongWritable, Testo val,
OutputCollector & lt; Testo, Testo> uscita, Reporter giornalista)
throws IOException
{
FileSplit FileSplit = (FileSplit) reporter.getInputSplit ();
String filename = fileSplit.getPath () .getName ();
location.set (fileName);

Linea String - val.toString ();
StringTokenizer itr = new StringTokenizer (line.toLowerCase ());
mentre (itr.hasMoreTokens ()) {
word.set (itr.nextToken ());
output.collect (parola, posizione);
}
}

}

2 Digitare il seguente codice per la funzione di ridurre:

class InvertedIndexerReducer public static estende MapReduceBase

implements Reducer<Text, Text, Text, Text>

{

public void reduce(Text key, Iterator<Text> values,
OutputCollector<Text, Text> output,
Reporter reporter) throws IOException
{
boolean first = true;
StringBuilder toReturn = new StringBuilder() ;
while (values.hasNext()) {
if (!first)
toReturn.append(", ") ;
first = false;
toReturn.append(values.next().toString()) ;
}
output.collect(key, new Text(toReturn.toString())) ;
}

}

3 Digitare il seguente codice per completare l'indice invertito:

Main (string [] args) public static void throws IOException
{

if (args.length < 2) {
System.out
println("Usage: InvertedIndex <input path> <output path>") ;
system.exit(1) ;
}
JobConf conf = new JobConf(InvertedIndex.class) ;
conf.setJobName("InvertedIndex") ;

conf.setOutputKeyClass (Text.class);
conf.setOutputValueClass (Text.class);

conf.setMapperClass (InvertedIndexerMapper.class);
conf.setReducerClass (InvertedIndexerReducer.class);

FileInputFormat.setInputPaths (conf, nuovo percorso (args [0]));
FileOutputFormat.setOutputPath (conf, nuovo percorso (args [1]));
provare {
JobClient.runJob (conf);
} Catch (Exception e) {
e.pringStackTrace ();
}

}