2017-01-12 3 views
1

두 개의 데이터 프레임을 생성하고 union을 수행하는 데 문제가 있습니다.스파크 데이터 프레임 만들기

a = [{'letter':'a'}] 
sqlContext.createDataFrame(a).collect() 
b = [{'letter':'b'}] 
sqlContext.createDataFrame(b).collect() 

을하지만 rdd = sc.union([a,b])을 실행하면, 나는

AttributeError: 'list' object has no attribute '_jrdd_deserializer' 그래서 내가 sc.parallelize()을 할 필요가 생각 얻을 :

이 작동합니다. 나는 이것이 문서를 읽은 후에도 무엇을하는지 확신하지 못합니다. 누군가이 기능을 높은 수준에서 설명 할 수 있습니까?

그래서 내가 그랬어 :

a = sc.parallelize(['a']) 
sqlContext.createDataFrame(a).collect() 

하지만 난 여전히 오류가 발생,이 시간 : TypeError: Can not infer schema for type: <type 'str'>

난 그냥 모든 구문과 형식을 이해하기 위해 노력하고있어 및 기능을 간단하게 만드는 데 필요한 2 개의 데이터 프레임과 조합.

답변

3

첫 번째 접근 방식에서는 sc.union([a, b])이 RDD/DataFrames가 아닌 두 개의 목록을 결합하려고하므로 RDD/DataFrames를 만들지 않으므로 AttributeError: 'list' object has no attribute '_jrdd_deserializer'이됩니다. 당신이

[Row(letter=u'a'), Row(letter=u'b')] 

또는 DataFrames에 출력

a = [{'letter':'a'}] 
b = [{'letter':'b'}] 

a_df = sqlContext.createDataFrame(a) 
b_df = sqlContext.createDataFrame(b) 

a_df.unionAll(b_df).collect() 

을 운영하려면 여기

는 조각이다, RDD 작업은 출력

r = sc.union([ 
     sc.parallelize([e['letter'] for e in a]), 
     sc.parallelize([e['letter'] for e in b]), 
    ]) 

r.collect() 

을 것

['a', 'b'] 
+0

고맙습니다. 3 가지를 잘못하고있는 것처럼 보입니다.'createDataFrame'을 변수에 할당하지 않고'collect()'메서드를 사용하고'union' 구문을 다르게 지정했습니다. 아니면'sc.union'이 여전히 실행 가능한 방법인가? – simplycoding

+0

RDD로 작업 할 때'sc.union'은 여전히 ​​좋은 방법입니다. 당신은'[{(sc.parallelize (a), sc.parallelize (b)])에 대해이 sc.union ([sc.parallelize 'letter': 'a'}, { 'letter': 'b'}]'출력. – TDrabas

관련 문제