Home Hardware Networking Programmazione Software Domanda Sistemi
Conoscenza Informatica >> Programmazione >> Computer Programming Languages >> .

Come costruire un indice invertito Con MapReduce

MapReduce è un modello di programmazione parallela sviluppata in Google per grandi insiemi di dati . Esso elabora i dati in blocchi piuttosto che in ordine sequenziale . In tal modo, essa si basa sulla mappa di funzioni di ingresso accoppiati (chiavi) e valori che poi mette attraverso la funzione reduce - in tal modo , il suo nome - per rendere più facili i dati per capire . Invece di fornire la funzione di mappa con una chiave e un valore , un indice invertito coppie di 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. Istruzioni
1

Digitare il seguente codice per la funzione mappa :

pubblico InvertedIndexerMapper classe statica estende MapReduceBaseimplements Mapper {private ultima parola del testo statico = new testo ( ) ; private posizione testo statico finale = nuovo testo ( ) ;

pubblici mappa void ( tasto LongWritable , testo val , OutputCollector , Reporter giornalista ) throws IOException { FileSplit FileSplit = ( FileSplit ) reporter.getInputSplit (); String fileName = fileSplit.getPath ( ) getName () ; location.set ( fileName ) ;

linea String - val.toString ( ) . ITR StringTokenizer = new StringTokenizer ( riga. toLowerCase ()); while ( itr.hasMoreTokens ( ) ) { word.set ( itr.nextToken ()); output.collect (word , posizione );} } }
2

Digitare il seguente codice per la funzione di ridurre :

pubblico InvertedIndexerReducer classe statica estende MapReduceBaseimplements Reducer { public void ridurre ( tasto Text , valori Iterator , OutputCollector , Reporter giornalista ) throws IOException { boolean primo = true ; StringBuilder toReturn = new StringBuilder (); while ( values.hasNext ( ) ) {if ( primo ) toReturn.append ( "," ) ; ! primo = false; toReturn . append ( . values.next ( ) toString ()); } output.collect ( chiave , nuovo Testo ( toReturn.toString ( ))) ; } }
3

digitare il seguente codice per completare l' indice invertito :

public static void main (String [] args ) throws IOException {if ( args.length < 2) { System.outprintln ( " uso: InvertedIndex < , percorso di uscita > "); 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] ) ) ; try { JobClient.runJob ( conf) ; } catch ( Exception e) { e.pringStackTrace (); } }

 

Programmazione © www.354353.com