2016-07-11 28 views
0

Ich empfange csv-formatierte Dateien (Felder sind durch Trennzeichen getrennt und doppelt zitiert) in HDFS und haben ein Schwein-Skript entwickelt, das die Headerzeilen entfernt und die doppelten Anführungszeichen entfernt, bevor ich die Daten in Hive einfüge HQL-Skript.Pig CSVExcelStorage DoubleQuoted Kommas

Dieser Prozess hat gut funktioniert; Heute habe ich jedoch ein Datenproblem mit einer der Tabellen entdeckt. Die Dateien für diese Tabelle haben insbesondere ein Zeichenfolgenfeld, das mehrere Kommata innerhalb der Anführungszeichen enthalten kann. Dies führt dazu, dass die Daten für einige der Datensätze falsch in die falschen Spalten in Hive geladen werden.

Ich kann das Format der Dateien an der Quelle nicht ändern.

Derzeit verwende ich die PiggyBank CSVExcelStorage, um die CSV-Formatierung wie folgt zu verarbeiten. Kann dies geändert werden, um das richtige Ergebnis zu erzielen? Welche anderen Optionen habe ich? Ich habe festgestellt, dass es jetzt auch einen CSVLoader gibt, aber ich habe keine Beispiele gefunden, die zeigen, wie man es benutzt/implementiert.

Beispieleingabedatei Daten::

"P_NAME","P_ID","C_ID","C_NAME","C_TYPE","PROT","I_NAME","I_ID","A_NAME","A_IDS","C_NM","CO"  
"SAMPLEPNAME","123456","789123","SAMPLECNAME","Upload","SAMPLEINAME","This Sample Name of A, B, and C","3234","This Sample Name of A, B, and C","3234","c_name","R" 
"SAMPLEPNAME2","123457","789124","SAMPLECNAME2","Download","SAMPLEINAME2","This Sample Name","3235","This Sample Name","3235","c_name2","Q" 

Mit CSVExcelLoader mit Formatierung oben gesetzt: Pig CSVLoader

USING org.apache.pig.piggybank.storage.CSVExcelStorage(',','NO_MULTILINE','NOCHANGE','SKIP_INPUT_HEADER') 

Bearbeitung zusätzliche Probedaten und Resultate von Tests hinzufügen

SAMPLEPNAME,123456,789123,SAMPLECNAME,Upload,SAMPLEINAME,This Sample Name of A, B, and C,3234,This Sample Name of A, B, and C,3234,c_name,R 
SAMPLEPNAME2,123457,789124,SAMPLECNAME2,Download,SAMPLEINAME2,This Sample Name,3235,This Sample Name,3235,c_name2,Q 

Usi ng CSVLoader als CSVLoader(): Hinweis - Haben keine Optionen für die Parameter an den Konstruktor zur Verfügung gestellt werden

P_NAME,,,C_NAME,C_TYPE,PROT,I_NAME,,A_NAME,,C_NM,CO 
SAMPLEPNAME,123456,789123,SAMPLECNAME,Upload,SAMPLEINAME,This Sample Name of A, B, and C,3234,This Sample Name of A, B, and C,3234,c_name,R 
SAMPLEPNAME2,123457,789124,SAMPLECNAME2,Download,SAMPLEINAME2,This Sample Name,3235,This Sample Name,3235,c_name2,Q 

Der einzige wirkliche Unterschied, den ich sehe, ist, dass CSVLoader nicht die Kopfzeile zu entfernen, wie ich sah, keine Option, um dies auszuwählen und stattdessen einige der Kopfzeilennamen zu entfernen.

Mache ich etwas falsch? Eine funktionierende Lösung wird geschätzt.

+0

die Daten in Hive müssen diese Kommas im Feld haben Eine Möglichkeit, dies zu handhaben macht, ist das Komma in den Bereichen mit einem anderen Charakter ersetzen sagen ‚|‘ und laden Sie dann die Daten –

+0

@inquisitive_mind Ja, ich muss die ursprüngliche Formatierung der Daten beibehalten. – HendPro12

Antwort

2

Um das Problem der Kommas in den Bereichen zu umgehen, können Sie versuchen, diese Arbeit zu umgehen.

Laden Sie die Daten als Linie.
Behandeln Sie "," als Trennzeichen und ersetzen Sie es durch ein Pipe-Zeichen, z. B. "|".
Ersetzen Sie das Anfangs- und Ende-Anführungszeichen "durch eine leere Zeichenfolge.
Laden Sie die Zeile mit '|' als Trennzeichen.

A = LOAD 'test1.csv' AS (lines:chararray); 
ranked = rank A; 
B = FILTER ranked BY (rank_A > 1); 
C = FOREACH B GENERATE REPLACE($1,'","','|'); 
D = FOREACH C GENERATE REPLACE($0,'"',''); 
DUMP D; 

A = LOAD ‚Test1.csv 'AS (Zeilen: Chararray);

"P_NAME","P_ID","C_ID","C_NAME","C_TYPE","PROT","I_NAME","I_ID","A_NAME","A_IDS","C_NM","CO" 
"SAMPLEPNAME","123456","789123","SAMPLECNAME","Upload","SAMPLEINAME","This Sample Name of A, B, and C","3234","This Sample Name of A, B, and C","3234","c_name","R" 
"SAMPLEPNAME2","123457","789124","SAMPLECNAME2","Download","SAMPLEINAME2","This Sample Name","3235","This Sample Name","3235","c_name2","Q" 

Platz = Rang A;

(1,"P_NAME","P_ID","C_ID","C_NAME","C_TYPE","PROT","I_NAME","I_ID","A_NAME","A_IDS","C_NM","CO") 
(2,"SAMPLEPNAME","123456","789123","SAMPLECNAME","Upload","SAMPLEINAME","This Sample Name of A, B, and C","3234","This S 
ample Name of A, B, and C","3234","c_name","R") 
(3,"SAMPLEPNAME2","123457","789124","SAMPLECNAME2","Download","SAMPLEINAME2","This Sample Name","3235","This Sample Name 
","3235","c_name2","Q") 

B = FILTER gewählt BY (rank_A> 1);

(2,"SAMPLEPNAME","123456","789123","SAMPLECNAME","Upload","SAMPLEINAME","This Sample Name of A, B, and C","3234","This S 
ample Name of A, B, and C","3234","c_name","R") 
(3,"SAMPLEPNAME2","123457","789124","SAMPLECNAME2","Download","SAMPLEINAME2","This Sample Name","3235","This Sample Name 
","3235","c_name2","Q") 

C = FOREACH B GENE REPLACE ($ 1, ' ""', '|');

("SAMPLEPNAME|123456|789123|SAMPLECNAME|Upload|SAMPLEINAME|This Sample Name of A, B, and C|3234|This S 
ample Name of A, B, and C|3234|c_name|R") 
("SAMPLEPNAME2|123457|789124|SAMPLECNAME2|Download|SAMPLEINAME2|This Sample Name|3235|This Sample Name 
|3235|c_name2|Q") 

D = FOREACH C GENE REPLACE ($ 0, '"', '');

(SAMPLEPNAME|123456|789123|SAMPLECNAME|Upload|SAMPLEINAME|This Sample Name of A, B, and C|3234|This S 
ample Name of A, B, and C|3234|c_name|R) 
(SAMPLEPNAME2|123457|789124|SAMPLECNAME2|Download|SAMPLEINAME2|This Sample Name|3235|This Sample Name 
|3235|c_name2|Q) 

Sie können nun diese Daten laden mit Hive '|' .? Als Trennzeichen

enter image description here