今天就跟大家聊聊有关Spark2.2.0中RDD转DataFrame的方式是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
Spark SQL如何将现有的RDDs转换为数据集。
方法:通过编程接口,该接口允许您构造一个模式,然后将其应用于现有的RDD。虽然此方法更详细,但它允许您在列及其类型直到运行时才知道时构造数据集。
数据准备studentData.txt
1001,20,zhangsan1002,17,lisi1003,24,wangwu1004,16,zhaogang
代码实例:
package com.unicom.ljs.spark220.study;
import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.rdd.RDD;
import org.apache.spark.sql.*;
import org.apache.spark.sql.types.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author: Created By lujisen
* @company ChinaUnicom Software JiNan
* @date: 2020-01-21 13:42
* @version: v1.0
* @description: com.unicom.ljs.spark220.study
*/
public class RDD2DataFrameProgramatically {
public static void main(String[] args) {
SparkConf sparkConf = new SparkConf().setMaster("local[*]").setAppName("RDD2DataFrameProgramatically");
JavaSparkContext sc = new JavaSparkContext(sparkConf);
SQLContext sqlContext = new SQLContext(sc);
JavaRDD<String> lineRDD =sc.textFile("C://Users//Administrator//Desktop//studentData.txt");
JavaRDD<Row> rowJavaRDD = lineRDD.map(new Function<String, Row>() {
@Override
public Row call(String line) throws Exception {
String[] splitLine = line.split(",");
return RowFactory.create(Integer.valueOf(splitLine[0])
,Integer.valueOf(splitLine[1])
,splitLine[2]);
}
});
List<StructField> structFields=new ArrayList<StructField>();
/*StructField structField1=new StructField("id", DataTypes.IntegerType,true);*/
structFields.add(DataTypes.createStructField("id",DataTypes.IntegerType,true));
structFields.add(DataTypes.createStructField("age",DataTypes.IntegerType,true));
structFields.add(DataTypes.createStructField("name",DataTypes.StringType,true));
StructType structType=DataTypes.createStructType(structFields);
Dataset<Row> dataFrame = sqlContext.createDataFrame(rowJavaRDD, structType);
dataFrame.registerTempTable("studentInfo");
Dataset<Row> resultDataSet = sqlContext.sql("select * from studentInfo where age > 17");
List<Row> collect = resultDataSet.javaRDD().collect();
for(Row row: collect){
System.out.println(row);
}
sc.close();
}
}
pom.xml关键依赖:
<spark.version>2.2.0</spark.version>
<scala.version>2.11.8</scala.version>
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>${spark.version}</version></dependency><dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>${spark.version}</version></dependency>
看完上述内容,你们对Spark2.2.0中RDD转DataFrame的方式是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/223206.html