为了账号安全,请及时绑定邮箱和手机立即绑定

SparkSQL常用操作

标签:
Spark

 1、从json文件创建dataFrame

val df: DataFrame = sqlContext.read.json("hdfs://master:9000/user/spark/data/people.json")

val people = df.registerTempTable("person")

val teenegers: DataFrame = sqlContext.sql("select name,age from person")

teenegers.map(x => "name:" + x(0)+ " " + "age:" + x(1)).collect().foreach(println)

2、从parquet文件创建dataFrame

val df: DataFrame = sqlContext.read.parquet("hdfs://master:9000/user/spark/data/namesAndAges.parquet")

val people = df.registerTempTable("person")

val teenegers: DataFrame = sqlContext.sql("select name,age from person")

teenegers.map(x => "name:" + x(0)+ " " + "age:" + x(1)).collect().foreach(println)

3、从普通RDD创建dataFrame_1

val people = sc.textFile("hdfs://master:9000/user/spark/data/people.txt").map(_.split(",")).map(p => Person(p(0), p(1).trim.toInt)).toDF

people.registerTempTable("people")

val teenagers = sqlContext.sql("select name,age from people")

teenagers.map(x => "name:" + x(0)+ " " + "age:" + x(1)).collect().foreach(println)

4、从普通RDD创建dataFrame_2

val people = sc.textFile("hdfs://master:9000/user/spark/data/people.txt")

val schemaString = "name age"

import org.apache.spark.sql.Row

import org.apache.spark.sql.types.{StructType,StructField,StringType}

val schema = StructType(schemaString.split(" ").map(fieldName => StructField(fieldName,StringType,true)))

val rowRDD = people.map(_.split(",")).map(x => Row(x(0),x(1).trim))

val df: DataFrame = sqlContext.createDataFrame(rowRDD,schema)

df.registerTempTable("people")val teenagers = sqlContext.sql("select name,age from people")

teenagers.map(x => "name:" + x(0)+ " " + "age:" + x(1)).collect().foreach(println)

5、测试dataframe的read和save方法(注意load方法默认是加载parquet文件)

val df = sqlContext.read.load("hdfs://master:9000/user/spark/data/namesAndAges.parquet")

df.select("name").write.save("hdfs://master:9000/user/spark/data/name.parquet")

6、测试dataframe的read和save方法(可通过手动设置数据源和保存测mode)

val df =sqlContext.read.format("json").load("hdfs://master:9000/user/spark/ data/people.json")

df.select("age").write.format("parquet").mode(SaveMode.Append).save("hdfs://master:9000/user/spark/data/ages.parquet")

7、直接使用sql查询数据源

val df = sqlContext.sql("SELECT * FROM parquet.`hdfs://master:9000/user/spark/data/ages.parquet`")

df.map(x => "name:" + x(0)).foreach(println)

8、parquest文件的读写

val people = sc.textFile("hdfs://master:9000/user/spark/data/people.txt").toDF

people.write.mode(SaveMode.Overwrite).parquet("hdfs://master:9000/user/spark/data/people.parquet")

val parquetFile = sqlContext.read.parquet("hdfs://master:9000/user/spark/data/people.parquet")

parquetFile.registerTempTable("parquetFile")

val teenagers = sqlContext.sql("SELECT name FROM parquetFile")

teenagers.map(t => "Name: " + t(0)).collect().foreach(println)

9、Schema Merging

val df1 = sc.makeRDD(1 to 5).map(i => (i, i * 2)).toDF("single", "double")

df1.write.mode(SaveMode.Overwrite).parquet("hdfs://master:9000/user/spark/data/test_table/key=1")

df2 = sc.makeRDD(6 to 10).map(i => (i, i * 3)).toDF("single", "triple")

df2.write.mode(SaveMode.Overwrite).parquet("hdfs://master:9000/user/spark/data/test_table/key=2")

df3 = sqlContext.read.option("mergeSchema", "true").parquet("hdfs://master:9000/user/spark/data/test_table")

df3.printSchema()

df3.show()

10、hive metastore

val sqlContext = new HiveContext(sc)sqlContext.setConf("spark.sql.shuffle.partitions","5")

sqlContext.sql("use my_hive")

sqlContext.sql("create table if not exists sogouInfo (time STRING,id STRING,webAddr STRING,downFlow INT,upFlow INT,url STRING) row format delimited fields terminated by '\t'")

sqlContext.sql("LOAD DATA LOCAL INPATH '/root/testData/SogouQ1.txt' overwrite INTO TABLE sogouInfo")

sqlContext.sql("select " +"count(distinct id) as c " +"from sogouInfo " +"group by time order by c desc limit 10").collect().foreach(println)

11、df from jdbc eg:mysql

val sqlContext = new SQLContext(sc)

val jdbcDF = sqlContext.read.format("jdbc").options(Map("driver" -> "com.mysql.jdbc.Driver","url" -> "jdbc:mysql://192.168.0.65:3306/test?user=root&password=root","dbtable" -> "trade_total_info_copy")).load()

jdbcDF.registerTempTable("trade_total_info_copy")

sqlContext.sql("select * from trade_total_info_copy").foreach(println)



作者:BIGUFO
链接:https://www.jianshu.com/p/f0d7cc1cd7e0


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消