복붙노트

[SCALA] SparkSQL - 읽기 마루 파일을 직접

SCALA

SparkSQL - 읽기 마루 파일을 직접

나는 테이블을 읽기 위해 다음 코드를 사용하여, SparkSQL에 임팔라에서 마이그레이션하고 있습니다 :

my_data = sqlContext.read.parquet('hdfs://my_hdfs_path/my_db.db/my_table')

이 같은 것을 반환 할 수 있도록하는 방법, 위 SparkSQL 된 invoke I을 수행합니다

'select col_A, col_B from my_table'

해결법

  1. ==============================

    1.마루 파일에서 Dataframe를 생성 한 후, 당신은 그것에서 SQL 쿼리를 실행하는 임시 테이블로 등록해야합니다.

    마루 파일에서 Dataframe를 생성 한 후, 당신은 그것에서 SQL 쿼리를 실행하는 임시 테이블로 등록해야합니다.

    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    
    val df = sqlContext.read.parquet("src/main/resources/peopleTwo.parquet")
    
    df.printSchema
    
    // after registering as a table you will be able to run sql queries
    df.registerTempTable("people")
    
    sqlContext.sql("select * from people").collect.foreach(println)
    
  2. ==============================

    2.JSON, ORC, 마루 및 CSV 파일은 스파크 DataFrame에 테이블을 생성하지 않고 쿼리 할 수 ​​있습니다.

    JSON, ORC, 마루 및 CSV 파일은 스파크 DataFrame에 테이블을 생성하지 않고 쿼리 할 수 ​​있습니다.

    //This Spark 2.x code you can do the same on sqlContext as well
    val spark: SparkSession = SparkSession.builder.master("set_the_master").getOrCreate
    
    spark.sql("select col_A, col_B from parquet.`hdfs://my_hdfs_path/my_db.db/my_table`")
       .show()
    
  3. from https://stackoverflow.com/questions/41254011/sparksql-read-parquet-file-directly by cc-by-sa and MIT license