Was ist Frühling für Apache Hadoop?

Übersicht: Feder is one of the widely used frameworks in enterprise applications development. Feder hat verschiedene Komponenten wie Spring ORM, Frühling JDBC usw. zu unterstützen verschiedene Funktionen. Frühling für Apache Hadoop ist die Framework-Anwendung Gebäude mit Hadoop Komponenten wie HDFS zu unterstützen, MapReduce und Hive usw.. Feder stellt APIs mit all diesen Komponenten arbeiten. Frühling unterstützt auch die Integration von Hadoop mit anderen Frühlings Ökosystem-Projekte für das wirkliche Leben Anwendungsentwicklung. In diesem Artikel werden wir diskutieren, die Verwendung von Feder für Apache Hadoop-Frameworks.

Einführung:
Apache Hadoop ist ein Open-Source-Software-Framework, zu speichern und Prozessdaten-Sätze von größeren Volumen, das verwendet wird. Der Frühling ist auch ein Open-Source-Framework, das ist weit verbreitet in Java / J2EE-Anwendungen verwendet. Spring Dependency Injection (VON) oder Umkehrung der Steuer (IO) Mechanismus ist eine beliebte Alternative zu den Enterprise Java Beans werden (oder EJB) model. Frühling hat den Vorteil, dass sie flexibel genug, um leicht mit einem anderen Entwicklungsrahmen gesteckt werden. Mit diesem Vorteil des Frühlings, wir können es mit Apache Hadoop stecken uns den größtmöglichen Nutzen jeder dieser beiden Frameworks erhalten helfen.

Anfangen:
In diesem Abschnitt werden wir reden darüber, wie ein Hadoop MapReduce Job mit Spring zu erstellen. Dies umfasst die folgenden Schritte -

  • Step 1 - Beziehen Sie die erforderlichen Abhängigkeiten mit Maven - Wie wir Maven wissen auf der pom.xml Datei in hohem Maße abhängig ist, wir nehmen Sie die folgenden Einträge in unserem pom.xml Datei. Diese Abhängigkeit Einträge sind für Hadoop Kern und Spring-Framework.

Listing1: Beispielkonfigurationseinträge in pom.xml Datei

[Code]

< !– Spring Data Apache Hadoop — >
< Abhängigkeit >
< groupId > org.springframework.data </ groupId >
< artifactId > Feder-data-hadoop </ artifactId >
< version > 1.0.0.FREISETZUNG </ version >
< /Abhängigkeit >
< !– Apache Hadoop-Kern -- >
< Abhängigkeit >
< groupId > org.apache.hadoop </ groupId >
< artifactId > Hadoop-Kern </ artifactId >
< version > 1.0.3 </version >
</Abhängigkeit>

[/ Code]

  • Step 2 - Erstellen Sie die Zuordnerkomponente - Da wir ein Mapper-Komponente wird verwendet, kennen das eigentliche Problem in kleinere Komponenten zu brechen. Diese kleineren Komponenten dann leichter zu lösen. Wir können unsere eigenen maßgeschneiderten Zuordnerkomponente haben durch die Apache Karte reduzieren Mapper-Klasse erweitern. Wir brauchen die Karte Methode dieser Klasse außer Kraft zu setzen. Der Mapper Klasse erwartet die folgenden vier Parameter -

Für die Eingabe: Folgende Parameter sind für die Eingabe-Taste und Wert

  • THEN - Dieser Parameter beschreibt die Schlüsseltyp, der als eine Eingabe an den Mapper-Komponente vorgesehen ist,.
  • VALUEIN - Dieser Parameter beschreibt den Typ des Wertes, der als eine Eingabe in den Mapper-Komponente vorgesehen ist,.

Für die Ausgabe: Folgende Parameter sind für die Ausgabe Schlüssel und Wert

  • KEYOUT - Dieser Parameter beschreibt die Art der Schlüsselparameter von der Zuordnerkomponente löschte.
  • VALUEOUT - Dieser Parameter beschreibt die Art des Ausgangswertes von der Mapper-Komponente.

Jeder dieser Parameter implementieren muss die schreibbar interface. In dem gegebenen Beispiel, wir haben unser Mapper zu lesen, den Inhalt einer Datei eine Zeile zu einem Zeitpunkt, und bereiten Schlüssel-Wert-Paare jeder Zeile verwendet. Unsere Implementierung der Map-Methode führt die folgenden Aufgaben -

  • First, aufgeteilt, jede einzelne Zeile in Worte
  • Zweite, iterate through every single word and take out neither all the Unicode characters which are neither letters nor characters.
  • Dritte, construct a key-value pair using the write method of the Context class which is compatible with the expected output key-value pair.

Listing2: Sample customized Mapper class

[Code]

public class MyWordMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private Text myword = new Text();

@ Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer lineTokenz = new StringTokenizer(line);
während (lineTokenz.hasMoreTokens()) {
String cleaned_data = removeNonLettersNonNumbers(lineTokenz.nextToken());
myword.set(cleaned_data);
context.write(myword, new IntWritable(1));
}
}

/**
* Replace all Unicode characters that are neither numbers nor letters with an empty string.
* @param original, It is the original string
* @return a string object which contains only letters and numbers
*/
private String removeNonLettersNonNumbers (String original) {
return original.replaceAll(“[^\p{L}\\p{N}]”, “”);
}
}

[/Code]

Step 3 - Create the Reducer Component – A reducer is a component which deletes the unwanted intermediate values and forwards only those key value pairs which are relevant. To have our customized reducer, our class should extend the Reducer class and over ride the reduce method. The reducer class expects the following four parameters.

Für die Eingabe: Folgende Parameter sind für die Eingabe-Taste und Wert

  • THEN - Dieser Parameter beschreibt die Schlüsseltyp, der als eine Eingabe an den Mapper-Komponente vorgesehen ist,.
  • VALUEIN - Dieser Parameter beschreibt den Typ des Wertes, der als eine Eingabe in den Mapper-Komponente vorgesehen ist,.

Für die Ausgabe: Folgende Parameter sind für die Ausgabe Schlüssel und Wert

  • KEYOUT - Dieser Parameter beschreibt die Art der Schlüsselparameter von der Zuordnerkomponente löschte
  • VALUEOUT - Dieser Parameter beschreibt die Art des Ausgangswertes von der Mapper-Komponente.

While implementing we must make sure that the datatype of the ‘keyin’ and ‘keyout’ parameters are same. Also the ‘valuein’ and valueout’ parameters should be of same type. Our implementation of the reduce method performs the following steps –

  • First, check that the input key contains the desired word.
  • Zweite, if the above step is true, get the number of occurrences of the word.
  • Dritte, construct a new key-value pair by calling the write method of the reducer class.

Listing3: Sample customized Reducer class

[Code]

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class MyWordReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
protected static final String MY_TARGET_TEXT = “Hadoop”;

@ Override
protected void reduce(Text keyTxt, Iterable<IntWritable> Werte, Context context) throws IOException, InterruptedException {
wenn (containsTargetWord(keyTxt)) {
int wCount = 0;
für (IntWritable value: Werte) {
wCount = value.get();
}
context.write(key, new IntWritable(wCount));
}
}
private boolean containsTargetWord(Text keyTxt) {
return keyTxt.toString().equals(MY_TARGET_TEXT);
}
}

[/Code]

  • Step 4 - Create the application context – Next step is to create the application context using XML. We can configure the application context of our application using the following steps –
    • Create a properties file which contains the value of the configuration properties. A sample application properties file is shown below –

[Code]
fs.default.name=hdfs://localhost:9000
mapred.job.tracker=localhost:9001
input.path=/path/to/input/file/
output.path=/path/to/output/file
[/Code]

  • Configure a property place holder which is used to fetch the values of configuration properties from the created properties file. This can be done by adding the following in our application context XML file –

[Code]
<Kontext:property-placeholder location=”classpath:application.properties” />

[/Code]

  • Configure Apache Hadoop and its job – We can configure the default file system and its job tracker by adding the following in our application context file

[Code]

<hdp:Konfiguration>
fs.default.name=${fs.default.name}
mapred.job.tracker=${mapred.job.tracker}
</hdp:Konfiguration>

[/Code]

We should add the following in our application context XML file to define the job tracker –

[Code]
<hdp:job id=”wordCountJobId”
input-path=”${input.path}”
output-path=”${output.path}”
jar-by-class=”net.qs.spring.data.apachehadoop.Main”
mapper=”net.qs.spring.data.apachehadoop.MyWordMapper”
reducer=”net.qs.spring.data.apachehadoop.MyWordReducer”/>

[/Code]

  • Configure the job runner which runs the created hadoop job. The Job runner can be configured by adding the following in our application context XML file

[Code]
<hdp:job-runner id=”wordCountJobRunner” job-ref=”wordCountJobId” run-at-startup=”wahr”/>
[/Code]

  • Step 5 – Loading the application context at startup – We can now execute the created Hadoop job by loading the application context when the application starts up. We can do this by creating the instance of the ClasspathXmlApplicationContext object which accepts the name of our application context file as input parameter to the constructor. This can be done as under –

Listing4: Sample showing loading of application context

[Code]
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
public static void main(Schnur[] arguments) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(“applicationContext.xml”);
}
}

[/Code]

  • Step 6 - Run the Mapreduce job – We can start our map reduce job using the following steps –
  • Upload an input file into HDFS – We can do this by executing the following command on the command prompt –

[Code]

hadoop dfs -put sample.txt /input/sample.txt

[/Code]

Following is a sample input file which has been used in this example. The target key word ‘Hadoop’ is highlighted in GREEN. The word ‘Hadoop’ existiert 4 times in the sample.

Input

Input

Bild 1: Sample input file

  • Check if the file has been uploaded successfully by running the following command. It will show the input file.

[Code]

hadoop dfs -ls /input

[/Code]

  • Run the Mapreduce job. This can be done by executing the main method of our java file from the IDE. If all the steps work as expected then the following will be the output.

Output: Hadoop 4
Summary: Let us conclude what we have discussed so far in the following bullets –

  • Both Feder und Hadoop are useful frameworks from the open source community.
  • By combining these we can get the benefit of both the frameworks.
  • Creating a map reduce job using spring is a six step process as explained above.
Stichworte:,
============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share