2017-07-21 13 views
1

私はpysparkとhivecontext.sqlを使用しています。私のデータからすべての空値と空値を除外したいと思います。hivecontext.sqlでnull文字列と空の文字列をフィルタリングします。

私は単純なSQLコマンドを使用して最初にNULL値をフィルタリングしましたが、それは機能しません。

マイコード:

hiveContext.sql("select column1 from table where column2 is not null") 

が、それは "column2のがNULLでない" という表現せずに

エラー仕事:

Py4JavaError: An error occurred while calling o577.showString 

を、私はそれが私の選択によるものだったと思うが間違っています。

データ例:

column 1 | column 2 
null  | 1 
null  | 2 
1  | 3 
2  | 4 
null  | 2 
3  | 8 

目的:

column 1 | column 2 
1  | 3 
2  | 4 
3  | 8 

TKS

答えて

1

それは私のために働く:

df.na.drop(subset=["column1"]) 
1

それはハイブテーブルを理解していないので、私たちは、ハイブコンテキストSQLメソッドに直接ハイブのテーブル名を渡すことはできません名。 Hiveテーブルを読み取る方法の1つは、pysaprkシェルを使用することです。

ハイブテーブルの読み込みから取得したデータフレームを登録する必要があります。次に、SQLクエリを実行できます。

1

database_name.tableを指定し、同じクエリを実行する必要があります。それは私が使用上の例は、フィルタリングnull値 アウトについてのあなたの疑問をクリアします願っています

0
Have you entered null values manually? 
If yes then it will treat those as normal strings, 
I tried following two use cases 

dbname.person table in hive 

name age 

aaa  null // this null is entered manually -case 1 
Andy 30 
Justin 19 
okay  NULL // this null came as this field was left blank. case 2 

--------------------------------- 
hiveContext.sql("select * from dbname.person").show(); 
+------+----+ 
| name| age| 
+------+----+ 
| aaa |null| 
| Andy| 30| 
|Justin| 19| 
| okay|null| 
+------+----+ 

----------------------------- 
case 2 
hiveContext.sql("select * from dbname.person where age is not null").show(); 
+------+----+ 
| name|age | 
+------+----+ 
| aaa |null| 
| Andy| 30 | 
|Justin| 19 | 
+------+----+ 
------------------------------------ 
case 1 
hiveContext.sql("select * from dbname.person where age!= 'null'").show(); 
+------+----+ 
| name| age| 
+------+----+ 
| Andy| 30| 
|Justin| 19| 
| okay|null| 
+------+----+ 
------------------------------------ 

を助け場合は私に知らせてください。 sparkで登録されたテーブルをクエリする場合は、sqlContextを使用します。

関連する問題