前言
本文主要给出Spark创建空的DataFrame的代码示例,这里讲的空的DataFrame主要指有列名(可以自己随意指定),但是没有行的DataFrame,因为自己在开发过程中有这个需求,之前并不知道怎么创建,就查了一下,发现资料并不多,不知道因为太简单还是用的人少
1、代码
代码较简单,如下
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.types._
import org.apache.spark.sql.Row
import org.apache.spark.rdd.EmptyRDD /**
* Spark创建空DataFrame示例
*/
object EmptyDataFrame { def main(args: Array[String]): Unit = { val spark = SparkSession.builder().appName("EmptyDataFrame").master("local").getOrCreate() /**
* 创建一个空的DataFrame,代表用户
* 有四列,分别代表ID、名字、年龄、生日
*/
val colNames = Array("id", "name", "age", "birth")
//为了简单起见,字段类型都为String
val schema = StructType(colNames.map(fieldName => StructField(fieldName, StringType, true)))
//主要是利用了spark.sparkContext.emptyRDD
val emptyDf = spark.createDataFrame(spark.sparkContext.emptyRDD[Row], schema) emptyDf.show /**
* 也可以给每列指定相对应的类型
*/
val schema1 = StructType(
Seq(
StructField("id", IntegerType, true),
StructField("name", StringType, true),
StructField("age", IntegerType, true),
StructField("birth", StringType, true)))
val emptyDf1 = spark.createDataFrame(spark.sparkContext.emptyRDD[Row], schema1)
emptyDf1.show //还有一种空的DataFrame,没有任何行任何列
spark.emptyDataFrame.show spark.stop()
} }
2、结果
D:\devtools\Java\jdk1..0_191\bin\java.exe "-javaagent:D:\devtools\IntelliJ IDEA Community Edition 2018.2.6\lib\idea_rt.jar=64870:D:\devtools\IntelliJ IDEA Community Edition 2018.2.6\bin" -Dfile.encoding=UTF- -classpath D:\devtools\Java\jdk1..0_191\jre\lib\charsets.jar;D:\devtools\Java\jdk1..0_191\jre\lib\deploy.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\access-bridge-.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\cldrdata.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\dnsns.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\jaccess.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\jfxrt.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\localedata.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\nashorn.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\sunec.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\sunjce_provider.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\sunmscapi.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\sunpkcs11.jar;D:\devtools\Java\jdk1..0_191\jre\lib\ext\zipfs.jar;D:\devtools\Java\jdk1..0_191\jre\lib\javaws.jar;D:\devtools\Java\jdk1..0_191\jre\lib\jce.jar;D:\devtools\Java\jdk1..0_191\jre\lib\jfr.jar;D:\devtools\Java\jdk1..0_191\jre\lib\jfxswt.jar;D:\devtools\Java\jdk1..0_191\jre\lib\jsse.jar;D:\devtools\Java\jdk1..0_191\jre\lib\management-agent.jar;D:\devtools\Java\jdk1..0_191\jre\lib\plugin.jar;D:\devtools\Java\jdk1..0_191\jre\lib\resources.jar;D:\devtools\Java\jdk1..0_191\jre\lib\rt.jar;D:\workspace\Spark_Test\target\classes;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-core_2.\2.4.\spark-core_2.-2.4..jar;C:\Users\一期一会\.m2\repository\org\apache\avro\avro\1.8.\avro-1.8..jar;C:\Users\一期一会\.m2\repository\org\codehaus\jackson\jackson-core-asl\1.9.\jackson-core-asl-1.9..jar;C:\Users\一期一会\.m2\repository\org\codehaus\jackson\jackson-mapper-asl\1.9.\jackson-mapper-asl-1.9..jar;C:\Users\一期一会\.m2\repository\com\thoughtworks\paranamer\paranamer\2.7\paranamer-2.7.jar;C:\Users\一期一会\.m2\repository\org\apache\commons\commons-compress\1.8.\commons-compress-1.8..jar;C:\Users\一期一会\.m2\repository\org\tukaani\xz\1.5\xz-1.5.jar;C:\Users\一期一会\.m2\repository\org\apache\avro\avro-mapred\1.8.\avro-mapred-1.8.-hadoop2.jar;C:\Users\一期一会\.m2\repository\org\apache\avro\avro-ipc\1.8.\avro-ipc-1.8..jar;C:\Users\一期一会\.m2\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;C:\Users\一期一会\.m2\repository\com\twitter\chill_2.\0.9.\chill_2.-0.9..jar;C:\Users\一期一会\.m2\repository\com\esotericsoftware\kryo-shaded\4.0.\kryo-shaded-4.0..jar;C:\Users\一期一会\.m2\repository\com\esotericsoftware\minlog\1.3.\minlog-1.3..jar;C:\Users\一期一会\.m2\repository\org\objenesis\objenesis\2.5.\objenesis-2.5..jar;C:\Users\一期一会\.m2\repository\com\twitter\chill-java\0.9.\chill-java-0.9..jar;C:\Users\一期一会\.m2\repository\org\apache\xbean\xbean-asm6-shaded\4.8\xbean-asm6-shaded-4.8.jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-client\2.6.\hadoop-client-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-common\2.6.\hadoop-common-2.6..jar;C:\Users\一期一会\.m2\repository\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;C:\Users\一期一会\.m2\repository\xmlenc\xmlenc\0.52\xmlenc-0.52.jar;C:\Users\一期一会\.m2\repository\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;C:\Users\一期一会\.m2\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Users\一期一会\.m2\repository\commons-collections\commons-collections\3.2.\commons-collections-3.2..jar;C:\Users\一期一会\.m2\repository\commons-configuration\commons-configuration\1.6\commons-configuration-1.6.jar;C:\Users\一期一会\.m2\repository\commons-digester\commons-digester\1.8\commons-digester-1.8.jar;C:\Users\一期一会\.m2\repository\commons-beanutils\commons-beanutils\1.7.\commons-beanutils-1.7..jar;C:\Users\一期一会\.m2\repository\commons-beanutils\commons-beanutils-core\1.8.\commons-beanutils-core-1.8..jar;C:\Users\一期一会\.m2\repository\com\google\code\gson\gson\2.2.\gson-2.2..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-auth\2.6.\hadoop-auth-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\httpcomponents\httpclient\4.2.\httpclient-4.2..jar;C:\Users\一期一会\.m2\repository\org\apache\httpcomponents\httpcore\4.2.\httpcore-4.2..jar;C:\Users\一期一会\.m2\repository\org\apache\directory\server\apacheds-kerberos-codec\2.0.-M15\apacheds-kerberos-codec-2.0.-M15.jar;C:\Users\一期一会\.m2\repository\org\apache\directory\server\apacheds-i18n\2.0.-M15\apacheds-i18n-2.0.-M15.jar;C:\Users\一期一会\.m2\repository\org\apache\directory\api\api-asn1-api\1.0.-M20\api-asn1-api-1.0.-M20.jar;C:\Users\一期一会\.m2\repository\org\apache\directory\api\api-util\1.0.-M20\api-util-1.0.-M20.jar;C:\Users\一期一会\.m2\repository\org\apache\curator\curator-client\2.6.\curator-client-2.6..jar;C:\Users\一期一会\.m2\repository\org\htrace\htrace-core\3.0.\htrace-core-3.0..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-hdfs\2.6.\hadoop-hdfs-2.6..jar;C:\Users\一期一会\.m2\repository\org\mortbay\jetty\jetty-util\6.1.\jetty-util-6.1..jar;C:\Users\一期一会\.m2\repository\xerces\xercesImpl\2.9.\xercesImpl-2.9..jar;C:\Users\一期一会\.m2\repository\xml-apis\xml-apis\1.3.\xml-apis-1.3..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-app\2.6.\hadoop-mapreduce-client-app-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-common\2.6.\hadoop-mapreduce-client-common-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-yarn-client\2.6.\hadoop-yarn-client-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-yarn-server-common\2.6.\hadoop-yarn-server-common-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-shuffle\2.6.\hadoop-mapreduce-client-shuffle-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-yarn-api\2.6.\hadoop-yarn-api-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-core\2.6.\hadoop-mapreduce-client-core-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-yarn-common\2.6.\hadoop-yarn-common-2.6..jar;C:\Users\一期一会\.m2\repository\javax\xml\bind\jaxb-api\2.2.\jaxb-api-2.2..jar;C:\Users\一期一会\.m2\repository\javax\xml\stream\stax-api\1.0-\stax-api-1.0-.jar;C:\Users\一期一会\.m2\repository\org\codehaus\jackson\jackson-jaxrs\1.9.\jackson-jaxrs-1.9..jar;C:\Users\一期一会\.m2\repository\org\codehaus\jackson\jackson-xc\1.9.\jackson-xc-1.9..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-jobclient\2.6.\hadoop-mapreduce-client-jobclient-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\hadoop\hadoop-annotations\2.6.\hadoop-annotations-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-launcher_2.\2.4.\spark-launcher_2.-2.4..jar;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-kvstore_2.\2.4.\spark-kvstore_2.-2.4..jar;C:\Users\一期一会\.m2\repository\org\fusesource\leveldbjni\leveldbjni-all\1.8\leveldbjni-all-1.8.jar;C:\Users\一期一会\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.6.\jackson-core-2.6..jar;C:\Users\一期一会\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.6.\jackson-annotations-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-network-common_2.\2.4.\spark-network-common_2.-2.4..jar;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-network-shuffle_2.\2.4.\spark-network-shuffle_2.-2.4..jar;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-unsafe_2.\2.4.\spark-unsafe_2.-2.4..jar;C:\Users\一期一会\.m2\repository\javax\activation\activation\1.1.\activation-1.1..jar;C:\Users\一期一会\.m2\repository\org\apache\curator\curator-recipes\2.6.\curator-recipes-2.6..jar;C:\Users\一期一会\.m2\repository\org\apache\curator\curator-framework\2.6.\curator-framework-2.6..jar;C:\Users\一期一会\.m2\repository\com\google\guava\guava\16.0.\guava-16.0..jar;C:\Users\一期一会\.m2\repository\org\apache\zookeeper\zookeeper\3.4.\zookeeper-3.4..jar;C:\Users\一期一会\.m2\repository\javax\servlet\javax.servlet-api\3.1.\javax.servlet-api-3.1..jar;C:\Users\一期一会\.m2\repository\org\apache\commons\commons-lang3\3.5\commons-lang3-3.5.jar;C:\Users\一期一会\.m2\repository\org\apache\commons\commons-math3\3.4.\commons-math3-3.4..jar;C:\Users\一期一会\.m2\repository\com\google\code\findbugs\jsr305\1.3.\jsr305-1.3..jar;C:\Users\一期一会\.m2\repository\org\slf4j\slf4j-api\1.7.\slf4j-api-1.7..jar;C:\Users\一期一会\.m2\repository\org\slf4j\jul-to-slf4j\1.7.\jul-to-slf4j-1.7..jar;C:\Users\一期一会\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.\jcl-over-slf4j-1.7..jar;C:\Users\一期一会\.m2\repository\log4j\log4j\1.2.\log4j-1.2..jar;C:\Users\一期一会\.m2\repository\org\slf4j\slf4j-log4j12\1.7.\slf4j-log4j12-1.7..jar;C:\Users\一期一会\.m2\repository\com\ning\compress-lzf\1.0.\compress-lzf-1.0..jar;C:\Users\一期一会\.m2\repository\org\xerial\snappy\snappy-java\1.1.7.1\snappy-java-1.1.7.1.jar;C:\Users\一期一会\.m2\repository\org\lz4\lz4-java\1.4.\lz4-java-1.4..jar;C:\Users\一期一会\.m2\repository\com\github\luben\zstd-jni\1.3.-\zstd-jni-1.3.-.jar;C:\Users\一期一会\.m2\repository\org\roaringbitmap\RoaringBitmap\0.5.\RoaringBitmap-0.5..jar;C:\Users\一期一会\.m2\repository\commons-net\commons-net\3.1\commons-net-3.1.jar;C:\Users\一期一会\.m2\repository\org\json4s\json4s-jackson_2.\3.5.\json4s-jackson_2.-3.5..jar;C:\Users\一期一会\.m2\repository\org\json4s\json4s-core_2.\3.5.\json4s-core_2.-3.5..jar;C:\Users\一期一会\.m2\repository\org\json4s\json4s-ast_2.\3.5.\json4s-ast_2.-3.5..jar;C:\Users\一期一会\.m2\repository\org\json4s\json4s-scalap_2.\3.5.\json4s-scalap_2.-3.5..jar;C:\Users\一期一会\.m2\repository\org\scala-lang\modules\scala-xml_2.\1.0.\scala-xml_2.-1.0..jar;C:\Users\一期一会\.m2\repository\org\glassfish\jersey\core\jersey-client\2.22.\jersey-client-2.22..jar;C:\Users\一期一会\.m2\repository\javax\ws\rs\javax.ws.rs-api\2.0.\javax.ws.rs-api-2.0..jar;C:\Users\一期一会\.m2\repository\org\glassfish\hk2\hk2-api\2.4.-b34\hk2-api-2.4.-b34.jar;C:\Users\一期一会\.m2\repository\org\glassfish\hk2\hk2-utils\2.4.-b34\hk2-utils-2.4.-b34.jar;C:\Users\一期一会\.m2\repository\org\glassfish\hk2\external\aopalliance-repackaged\2.4.-b34\aopalliance-repackaged-2.4.-b34.jar;C:\Users\一期一会\.m2\repository\org\glassfish\hk2\external\javax.inject\2.4.-b34\javax.inject-2.4.-b34.jar;C:\Users\一期一会\.m2\repository\org\glassfish\hk2\hk2-locator\2.4.-b34\hk2-locator-2.4.-b34.jar;C:\Users\一期一会\.m2\repository\org\javassist\javassist\3.18.-GA\javassist-3.18.-GA.jar;C:\Users\一期一会\.m2\repository\org\glassfish\jersey\core\jersey-common\2.22.\jersey-common-2.22..jar;C:\Users\一期一会\.m2\repository\javax\annotation\javax.annotation-api\1.2\javax.annotation-api-1.2.jar;C:\Users\一期一会\.m2\repository\org\glassfish\jersey\bundles\repackaged\jersey-guava\2.22.\jersey-guava-2.22..jar;C:\Users\一期一会\.m2\repository\org\glassfish\hk2\osgi-resource-locator\1.0.\osgi-resource-locator-1.0..jar;C:\Users\一期一会\.m2\repository\org\glassfish\jersey\core\jersey-server\2.22.\jersey-server-2.22..jar;C:\Users\一期一会\.m2\repository\org\glassfish\jersey\media\jersey-media-jaxb\2.22.\jersey-media-jaxb-2.22..jar;C:\Users\一期一会\.m2\repository\javax\validation\validation-api\1.1..Final\validation-api-1.1..Final.jar;C:\Users\一期一会\.m2\repository\org\glassfish\jersey\containers\jersey-container-servlet\2.22.\jersey-container-servlet-2.22..jar;C:\Users\一期一会\.m2\repository\org\glassfish\jersey\containers\jersey-container-servlet-core\2.22.\jersey-container-servlet-core-2.22..jar;C:\Users\一期一会\.m2\repository\io\netty\netty-all\4.1..Final\netty-all-4.1..Final.jar;C:\Users\一期一会\.m2\repository\io\netty\netty\3.9..Final\netty-3.9..Final.jar;C:\Users\一期一会\.m2\repository\com\clearspring\analytics\stream\2.7.\stream-2.7..jar;C:\Users\一期一会\.m2\repository\io\dropwizard\metrics\metrics-core\3.1.\metrics-core-3.1..jar;C:\Users\一期一会\.m2\repository\io\dropwizard\metrics\metrics-jvm\3.1.\metrics-jvm-3.1..jar;C:\Users\一期一会\.m2\repository\io\dropwizard\metrics\metrics-json\3.1.\metrics-json-3.1..jar;C:\Users\一期一会\.m2\repository\io\dropwizard\metrics\metrics-graphite\3.1.\metrics-graphite-3.1..jar;C:\Users\一期一会\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.6.7.1\jackson-databind-2.6.7.1.jar;C:\Users\一期一会\.m2\repository\com\fasterxml\jackson\module\jackson-module-scala_2.\2.6.7.1\jackson-module-scala_2.-2.6.7.1.jar;C:\Users\一期一会\.m2\repository\org\scala-lang\scala-reflect\2.11.\scala-reflect-2.11..jar;C:\Users\一期一会\.m2\repository\com\fasterxml\jackson\module\jackson-module-paranamer\2.7.\jackson-module-paranamer-2.7..jar;C:\Users\一期一会\.m2\repository\org\apache\ivy\ivy\2.4.\ivy-2.4..jar;C:\Users\一期一会\.m2\repository\oro\oro\2.0.\oro-2.0..jar;C:\Users\一期一会\.m2\repository\net\razorvine\pyrolite\4.13\pyrolite-4.13.jar;C:\Users\一期一会\.m2\repository\net\sf\py4j\py4j\0.10.\py4j-0.10..jar;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-tags_2.\2.4.\spark-tags_2.-2.4..jar;C:\Users\一期一会\.m2\repository\org\apache\commons\commons-crypto\1.0.\commons-crypto-1.0..jar;C:\Users\一期一会\.m2\repository\org\spark-project\spark\unused\1.0.\unused-1.0..jar;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-sql_2.\2.4.\spark-sql_2.-2.4..jar;C:\Users\一期一会\.m2\repository\com\univocity\univocity-parsers\2.7.\univocity-parsers-2.7..jar;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-sketch_2.\2.4.\spark-sketch_2.-2.4..jar;C:\Users\一期一会\.m2\repository\org\apache\spark\spark-catalyst_2.\2.4.\spark-catalyst_2.-2.4..jar;C:\Users\一期一会\.m2\repository\org\scala-lang\modules\scala-parser-combinators_2.\1.1.\scala-parser-combinators_2.-1.1..jar;C:\Users\一期一会\.m2\repository\org\codehaus\janino\janino\3.0.\janino-3.0..jar;C:\Users\一期一会\.m2\repository\org\codehaus\janino\commons-compiler\3.0.\commons-compiler-3.0..jar;C:\Users\一期一会\.m2\repository\org\antlr\antlr4-runtime\4.7\antlr4-runtime-4.7.jar;C:\Users\一期一会\.m2\repository\org\apache\orc\orc-core\1.5.\orc-core-1.5.-nohive.jar;C:\Users\一期一会\.m2\repository\org\apache\orc\orc-shims\1.5.\orc-shims-1.5..jar;C:\Users\一期一会\.m2\repository\com\google\protobuf\protobuf-java\2.5.\protobuf-java-2.5..jar;C:\Users\一期一会\.m2\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;C:\Users\一期一会\.m2\repository\io\airlift\aircompressor\0.10\aircompressor-0.10.jar;C:\Users\一期一会\.m2\repository\org\apache\orc\orc-mapreduce\1.5.\orc-mapreduce-1.5.-nohive.jar;C:\Users\一期一会\.m2\repository\org\apache\parquet\parquet-column\1.10.\parquet-column-1.10..jar;C:\Users\一期一会\.m2\repository\org\apache\parquet\parquet-common\1.10.\parquet-common-1.10..jar;C:\Users\一期一会\.m2\repository\org\apache\parquet\parquet-encoding\1.10.\parquet-encoding-1.10..jar;C:\Users\一期一会\.m2\repository\org\apache\parquet\parquet-hadoop\1.10.\parquet-hadoop-1.10..jar;C:\Users\一期一会\.m2\repository\org\apache\parquet\parquet-format\2.4.\parquet-format-2.4..jar;C:\Users\一期一会\.m2\repository\org\apache\parquet\parquet-jackson\1.10.\parquet-jackson-1.10..jar;C:\Users\一期一会\.m2\repository\org\apache\arrow\arrow-vector\0.10.\arrow-vector-0.10..jar;C:\Users\一期一会\.m2\repository\org\apache\arrow\arrow-format\0.10.\arrow-format-0.10..jar;C:\Users\一期一会\.m2\repository\org\apache\arrow\arrow-memory\0.10.\arrow-memory-0.10..jar;C:\Users\一期一会\.m2\repository\joda-time\joda-time\2.9.\joda-time-2.9..jar;C:\Users\一期一会\.m2\repository\com\carrotsearch\hppc\0.7.\hppc-0.7..jar;C:\Users\一期一会\.m2\repository\com\vlkan\flatbuffers\1.2.-3f79e055\flatbuffers-1.2.-3f79e055.jar;C:\Users\一期一会\.m2\repository\org\scala-lang\scala-library\2.11.\scala-library-2.11..jar com.hikvision.learn.TestEmptyDataFrame
+---+----+---+-----+
| id|name|age|birth|
+---+----+---+-----+
+---+----+---+-----+ +---+----+---+-----+
| id|name|age|birth|
+---+----+---+-----+
+---+----+---+-----+ ++
||
++
++ Process finished with exit code