It is often convenient to write spark programs in Scala, after all, the source code of Spark is written in Scala. However, there are so many Java developers, especially when it comes to data docking and on-line services, it's time to master some of the ways spark is used in Java.
First, Map
Map cannot be used more often when it is processed and converted.
Before using map, you first define a conversion function format as follows:
function<string, labeledpoint> transForm = new function<string, labeledpoint> () {//string is the input type of a row Labeledpoint is the converted output type @Override public labeledpoint Call (String row) throws Exception {//Override the Call method string [] Rowarr = Row.split (","); int rowsize = rowarr.length; double[] Doublearr = new Double[rowsize-1]; In addition to the first bit, the rest of the lable is parsed into a double and then placed in the array for (int i = 1; i < rowsize; i++) { String every = Rowarr[i]; Doublearr[i] = double.parsedouble (each); } Using the data you just obtained, turn vector vectors feature = Vectors.dense (Doublearr); Double label = double.parsedouble (Rowarr[0]); Constructs a data format for classification training labelpoint labeledpoint point = new Labeledpoint (label, feature); return point; } };
Special attention is paid to the following:
1, the input of the call method should be the type of the data row before the conversion return value should be the data row type after processing
2. If a custom class is called in the conversion method, note that the class name must be serialized such as
public class Treeensemble implements Serializable {}
3, in the conversion function if the object of some class is called, for example, the method needs to call an external parameter, or a numerical processing model (normalization, normalization, etc.), the object needs to be declared final
And then it's called the conversion function at the right time.
Javardd<labeledpoint> Rdd = Oridata.tojavardd (). Map (TransForm);
This is a way to convert a normal rdd into a javardd to use, and it's not time-consuming to turn into Javardd, don't worry
Second, filter
It is also very common in scenarios such as avoiding null values for data, 0, and can satisfy the WHERE function in SQL
The first is to define a function that returns a Boolean value for the given row of data. The actual effect is to keep the data returned as true
function<string, boolean> boolfilter = new function<string, boolean> () {//string is the input type of a row Boolean is the corresponding output type Used to determine whether the data is retained @Override public Boolean call (String row) throws Exception {//Override the Call method Boolean flag = Row!=null; return flag; } };
In general, the function needs to be modified only by the type of row, which is the input type of the data row, and unlike the conversion function above, the return value of this call method should be fixed to Boolean
And then the calling method
Javardd<labeledpoint> Rdd = Oridata.tojavardd (). filter (Boolfilter);
Third, Maptopair
There are some similarities between this method and the map method, as well as some transformations of the data. However, this function input line output is a tuple, the most common method is used to do cross-validation or statistical error rate recall calculation AUC and so on
Similarly, you need to define a conversion function first
function<string, boolean> transformer = new Pairfunction<labeledpoint, Object, object> () {// Labeledpoint is the two object following the input type do not change @Override public Tuple2 Call (Labeledpoint row) throws Exception {//Override call method Usually only change input parameter output do not change double Predicton = thismodel.predict (Row.features ()); Double label = Row.label (); return new Tuple2 (Predicton, label); } );
Regarding the invocation of the class, the object of the class, the requirement is consistent with the previous, the class needs to be serialized, and the object of the class needs to be declared as a final type
The corresponding calls are as follows:
Javapairrdd<object, object> predictionsandlabels = Oridata.maptopair (transformer);
Then the use of the predictionsandlabels, calculate the accuracy rate, recall, accuracy, AUC, the next blog will be, please look forward to
If you have a supplementary, or question, or have questions, please email me, or directly reply to email: [Email protected]
The notation of various common operators of Java-spark