How to realize Matrix multiplication in Spark
This article will explain in detail how to implement matrix multiplication operation in Spark. The content of the article is of high quality. Therefore, Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.
1,Pom.xml introduces Spark-Mllib library
org.apache.spark spark-mllib_2.11 2.3.1 runtime
We need to pay attention here, because we need to use the relevant API, so here the runtime sentence should be removed, using the default dependency mode on the line
org.apache.spark spark-mllib_2.11 runtime
2, Code
public static void main(String[] args) { SparkConf sparkConf = new SparkConf().setAppName("Mllib-test").setMaster("local"); JavaSparkContext jpc = new JavaSparkContext(sparkConf); double[][] data = new double[4][4] ; data[0][0] = 0.0; data[0][1] = 2.0; data[0][2] = 3.0; data[0][3] = 4.0; data[1][0] = 1.0; data[1][1] = 3.0; data[1][2] = 4.0; data[1][3] = 5.0; data[2][0] = 2.0; data[2][1] = 4.0; data[2][2] = 5.0; data[2][3] = 6.0; data[3][0] = 3.0; data[3][1] = 5.0; data[3][2] = 6.0; data[3][3] = 7.0; JavaRDD rdd=jpc.parallelize(Arrays.asList(data)).map(f->{ long key = new Double(f[0]).longValue(); double[] value = new double[f.length-1]; for(int i = 1;i{ long key = new Double(f[0]).longValue(); double[] value = new double[f.length-1]; for(int i = 1;i