2017-04-30 17 views
0

のような出力を返します。ハイブにテーブルを作成し、外部のcsvファイルからデータを読み込みました。私がPythonからデータを印刷しようとすると、 ['\ x00 "\ x00m \ x00e \ x00s \ x00s \ x00a \ x00g \ x00e \ x00" \ x00 "]" Hive GUIを照会すると、結果は適切です。 PythonプログラムPythonプログラムからのハイブクエリは、 "x00e x00" x00 "

私のpythonコード:

import pyhs2 

with pyhs2.connect(host='192.168.56.101', 
       port=10000, 
       authMechanism='PLAIN', 
       user='hiveuser', 
       password='password', 
       database='anuvrat') as conn: 
with conn.cursor() as cur: 
    cur.execute('SELECT message FROM ABC_NEWS LIMIT 5') 

    print cur.fetchone() 

出力は次のとおりです。

/usr/bin/python2.7 /home/anuvrattiku/SPRING_2017/CMPE239/Facebook_Fake_news_detection/code_fake_news/code.py 
['\x00"\x00m\x00e\x00s\x00s\x00a\x00g\x00e\x00"\x00'] 

Process finished with exit code 0 

私はハイブで同じテーブルを照会するとき、私は次の出力を得る:

CREATE TABLE ABC_NEWS(
ID STRING, 
PAGE_ID INT, 
NAME STRING, 
MESSAGE STRING, 
DESCRIPTION STRING, 
CAPTION STRING, 
POST_TYPE STRING, 
STATUS_TYPE STRING, 
LIKES_COUNT SMALLINT, 
COMMENTS SMALLINT, 
SHARES_COUNT SMALLINT, 
LOVE_COUNT SMALLINT, 
WOW_COUNT SMALLINT, 
HAHA_COUNT SMALLINT, 
SAD_COUNT SMALLINT, 
THANKFUL_COUNT SMALLINT, 
ANGRY_COUNT SMALLINT, 
LINK STRING, 
IMAGE_LINK STRING, 
POSTED_AT STRING 
) 
ROW FORMAT DELIMITED FIELDS TERMINATED BY "," ESCAPED BY '\\'; 

テーブルをロードするためのcsvファイルがである:

enter image description here

をこれは私がテーブルを作成する方法であります以下のパス: https://www.dropbox.com/s/fiwygyqt8u9eo5s/abc-news-86680728811.csv?dl=0

+0

(1)あなたはモジュールを使用していることもはや維持されていませんhttps://github.com/BradRuderman/pyhs2(2)これは明らかにhiveserver2側のエンコーディングの問題です。 (3)投稿タグを修正します。あなたが与えられた恐ろしい答えの受け入れを取り消す場合、あなたは合理的な答えを得るチャンスを持つかもしれません。 –

+0

@DuduMarkovitz:こんにちはDudu、問題の解決策を提供してください –

答えて

0
  1. テキストが修飾されているので(")と修飾テキストの中に区切り文字(,)がある場合は、CSV Serde
  2. を入力してください。cur.fetchone()は文字列ではなくリストであるため、リストの最初の要素 - cur.fetchone()[0]

create external table abc_news 
(
    id    string 
    ,page_id   int 
    ,name   string 
    ,message   string 
    ,description  string 
    ,caption   string 
    ,post_type  string 
    ,status_type  string 
    ,likes_count  smallint 
    ,comments  smallint 
    ,shares_count smallint 
    ,love_count  smallint 
    ,wow_count  smallint 
    ,haha_count  smallint 
    ,sad_count  smallint 
    ,thankful_count smallint 
    ,angry_count  smallint 
    ,link   string 
    ,image_link  string 
    ,posted_at  string 
) 
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde' 
with serdeproperties 
(
    'separatorChar' = ',' 
    ,'quoteChar'  = '"' 
) 
stored as textfile 
; 

>>> import pyhs2 
>>> 
>>> with pyhs2.connect(host='localhost',port=10000,authMechanism='PLAIN',user='cloudera',password='cloudera',database='local_db') as conn: 
...  with conn.cursor() as cur: 
...   cur.execute('SELECT message FROM ABC_NEWS LIMIT 10') 
...   for i in cur.fetch(): 
...    print i[0] 
...    
...    
... 
"message" 
"Roberts took the unusual step of devoting the majority of his annual report to the issue of judicial ethics." 
"Do you agree with the new law?" 
"Some pretty cool confetti will rain down on New York City celebrators." 
NULL 
"The pharmacy was held up by a man seeking prescription medication. " 
NULL 
"There were no immediate reports of damage or injuries." 
"Were you an LCD screen early adopter? A settlement may be headed your way." 
"As Americans get bigger, passenger limits are becoming more restrictive." 
>>> 
関連する問題