Guest User

Untitled

a guest
Jan 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. -----------------------
  2. EXTERNAL HIVE TABLE ON PARQUET
  3. -----------------------
  4. create external table table6(
  5. id string,
  6. db1field string,
  7. created string,
  8. empid string
  9. )
  10. stored as parquet
  11. location '/table6';
  12.  
  13. import spark.implicits._
  14. import org.apache.spark.rdd._
  15. import org.apache.spark.sql._
  16. import org.apache.spark.sql.types._
  17.  
  18. val rowsRdd: RDD[Row] = sc.parallelize(
  19. Seq(
  20. Row("first1updatedagain", "1.1", "2018-10-12","12232"),
  21. Row("secondupdated1again", "2.2", "2018-10-12","23244"),
  22. Row("third2updatedagain", "3.3", "2018-10-12","54434"),
  23. Row("withNulls2updatedagain", null, null,null),
  24. Row("withEmptyStrs2updated", "", "",null),
  25. Row("withBadValues4updated", "NotADouble", "NotATimestamp","wrong")
  26. )
  27. )
  28.  
  29. val schema = new StructType()
  30. .add(StructField("id",StringType,true))
  31. .add(StructField("db1field", StringType, true))
  32. .add(StructField("created", StringType, true))
  33. .add(StructField("empid", StringType, true))
  34.  
  35.  
  36. val df = spark.createDataFrame(rowsRdd, schema)
  37.  
  38. println("before writing")
  39. df.show(false)
  40.  
  41. df.write.mode("overwrite").parquet("/table6")
  42.  
  43. val dfRead = spark.read.parquet("/table6")
  44. println("reading the parquet ")
  45. dfRead.show(false)
  46.  
  47. println("Using SQL query")
  48. spark.sql("REFRESH TABLE table6")
  49. spark.sql("select * from table6").show(false)
  50.  
  51. import org.apache.spark.sql.SparkSession
  52. val sparksession=SparkSession.builder().enableHiveSupport().getOrCreate()
  53. val df=sparksession.sql("select * from table6 ")
  54. df.show(false)
  55.  
  56. val df2=sparksession.sqlContext.sql("select * from table6 ")
  57. df2.show(false)
Add Comment
Please, Sign In to add comment