Unten ist mein Spark SQL-Skript, das eine Datei lädt und SQL darüber verwendet, möchte ich die Ausgabe von der SQL-Abfrage sammeln und in eine Datei schreiben, nicht sicher, wie man helfen kann.wie man Funken sql Ausgabe in eine Datei sammeln?
//import classes for sql
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
// createSchemaRDD is used to implicitly convert an RDD to a SchemaRDD.
import sqlContext.createSchemaRDD
//hdfs paths
val warehouse="hdfs://quickstart.cloudera/user/hive/warehouse/"
val customers_path=warehouse+"people/people.txt"
customers_path
//create rdd file called file
val file=sc.textFile(customers_path)
val schemaString="name age"
import org.apache.spark.sql._
val schema =
StructType(
schemaString.split(",").map(fieldName => StructField(fieldName, StringType, true)))
val rowRDD=file.map(_.split(",")).map(p => Row(p(0),p(1).trim))
val peopleSchemRDD=sqlContext.applySchema(rowRDD, schema)
// Register the SchemaRDD as a table.
peopleSchemRDD.registerTempTable("people")
// SQL statements can be run by using the sql methods provided by sqlContext.
sqlContext.sql("select count(*) from people").collect().foreach(println)
System.exit(0)
In dem Code haben Sie das Ergebnis von die Abfrage ist nur eine Nummer, oder? Sie fragen, wie man eine Zahl in Scala schreibt? –
ja Ich möchte die Nummer oder Ausgabe in eine Datei geschrieben werden, gibt es eine Möglichkeit, dies zu tun? –
val op = sqlContext.sql ("Anzahl wählen (*) von Personen") val c = op.collect() val rdd = sc.parallelize (c) rdd.saveAsTextFile ("/ home/cloudera/op") System.exit (0) –