2016-06-30 4 views
-1

의 구성원이 아닙니다. 파일을로드하고 데이터 프레임으로 변환되었지만 평균을 파싱하는 동안 오류가 발생했습니다. 샘플 요청이 우리를 위해 평균에 대한 = 2.8오류 : 값이 선택은 내가 파일의 모든 JSON 객체의 등급 평균 얻기 위해 노력하고 org.apache.spark.sql.Row

{ 
     "country": "Egypt", 
     "customerId": "Egypt009", 
     "visited": [ 
      { 
       "placeName": "US", 
       "rating": "1.3", 
       "famousRest": "McDonald", 
       "placeId": "Dedcf3" 

      }, 
       { 
       "placeName": "US", 
       "rating": "3.3", 
       "famousRest": "EagleNest", 
       "placeId": "CDfet3" 

      }, 


} 

{ 
     "country": "Canada", 
     "customerId": "Canada012", 
     "visited": [ 
      { 
       "placeName": "UK", 
       "rating": "3.3", 
       "famousRest": "N/A", 
       "placeId": "XSdce2" 

      }, 


    ] 
} 

이 JSON에 대한 그래서

{ 
     "country": "France", 
     "customerId": "France001", 
     "visited": [ 
      { 
       "placeName": "US", 
       "rating": "2.3", 
       "famousRest": "N/A", 
       "placeId": "AVBS34" 

      }, 
       { 
       "placeName": "US", 
       "rating": "3.3", 
       "famousRest": "SeriousPie", 
       "placeId": "VBSs34" 

      }, 
       { 
       "placeName": "Canada", 
       "rating": "4.3", 
       "famousRest": "TimHortons", 
       "placeId": "AVBv4d" 

      }   
    ] 
} 

은 미국 평균 평가 될 것 (2.3 + 3.3)/2 = (3.3 1.3)/2 =

2.3 그래서 모든 이상, 평균 평점은 다음과 같습니다

(2.8 + 2.3)/2 = 2.55

내 스키마 (두 개의 요청이 자신의 방문 목록에 '미국'이)

root 
|-- country: string(nullable=true) 
|-- customerId:string(nullable=true) 
|-- visited: array (nullable = true) 
| |-- element: struct (containsNull = true) 
| | |-- placeId: string (nullable = true) 
| | |-- placeName: string (nullable = true) 
| | |-- famousRest: string (nullable = true) 
| | |-- rating: string (nullable = true) 

val sqlContext = new org.apache.spark.sql.SQLContext(sc) 
val df = sqlContext.jsonFile("temp.txt") 
df.show() 

수행 할 때 :

val app = df.select("strategies"); app.registerTempTable("app"); app.printSchema(); app.show() 
app.foreach({ 
    t => t.select("placeName", "rating").where(t("placeName") == "US") 
}).show() 

I am getting : 
<console>:31: error: value select is not a member of org.apache.spark.sql.Row t => t.select("placeName", "rating").where(t("placeName") == "US")^

누군가가 내가 잘못 여기서 뭐하는 거지 말씀해 주시겠습니까?

답변

2

Dataframe (코드 예제 ... 당신이 df 변수를 만들고 app 변수를 조회 이해되지 않음) app입니다 가정, 당신은 그것을 선택하기 위해 foreach를 호출해서는 안 :

app.select("placeName", "rating").where(t("placeName") == "US") 

foreach은 각 레코드 (유형)에 대해 함수를 호출합니다. 이는 주로 부작용 (예 : 콘솔 출력/외부 서비스로 보내기 등)을 호출하는 데 유용합니다. 주로 데이터 프레임을 선택/변형하는 데이 데이터 프레임을 사용하지 않을 것입니다.

UPDATE : 미국 방문 만의 평균을 계산하는 방법의 원래의 질문에 관해서는

:

// explode to make a record out of each "visited" Array item, 
// taking only "placeName" and "rating" columns 
val exploded: DataFrame = df.explode(df("visited")) { 
    case Row(visits: Seq[Row]) => 
    visits.map(r => (r.getAs[String]("placeName"), r.getAs[String]("rating"))) 
} 

// make some order: rename columns named _1, _2 (since we used a tuple), 
// and cast ratings to Double: 
val ratings: DataFrame = exploded 
    .withColumnRenamed("_1", "placeName") 
    .withColumn("rating", exploded("_2").cast(DoubleType)) 
    .select("placeName", "rating") 

ratings.printSchema() 
ratings.show() 
/* prints: 
root 
|-- placeName: string (nullable = true) 
|-- rating: double (nullable = true) 

+---------+------+ 
|placeName|rating| 
+---------+------+ 
|  US| 1.3| 
|  US| 3.3| 
|  UK| 3.3| 
+---------+------+ 
*/ 

// now filter US only and get average rating: 
val avg = ratings 
    .filter(ratings("placeName") === "US") 
    .select(mean("rating")) 

avg.show() 
/* prints: 
+-----------+ 
|avg(rating)| 
+-----------+ 
|  2.3| 
+-----------+ 
    */ 
+0

발 응용 프로그램 = df.select ("전략"); app.registerTempTable ("app"); app.printSchema(); app.show는() –

+0

앱이 테이블을 제공합니다 (구조체의 배열) –

+0

당신이 날 모든 장소 이름의 평균을 찾기 위해 방향을 수정 가리킬 수 있습니다 목록을보고 각 사용하는 이유 이잖아 방문의 목록으로 구성 = US –