@@ -2,6 +2,7 @@ import {Command, flags} from '@oclif/command'
22import * as avro from 'avsc'
33import * as chalk from 'chalk'
44import * as fs from 'fs' // includes all from avro-js and some more
5+ import * as Json2Csv from 'json-2-csv'
56
67import Logger from '../utilities/logger'
78import Utilities from '../utilities/utilities'
@@ -59,8 +60,8 @@ export default class Avro extends Command {
5960 return this . toJson ( flags , args )
6061 case Avro . TO_AVRO :
6162 return this . toAvro ( flags , args )
62- // case Avro.TO_CSV:
63- // return this.toCsv(flags, args)
63+ case Avro . TO_CSV :
64+ return this . toCsv ( flags , args )
6465 default :
6566 Logger . error ( this , 'Unsupported Command, supported: ' + Avro . SupportedCommands )
6667 }
@@ -96,26 +97,27 @@ export default class Avro extends Command {
9697 }
9798
9899// // tslint:disable-next-line:no-unused
99- // private toCsv(flags: any, args: any) {
100- // var json2Csv = require("json-2-csv")
101- //
102- // let json=`
103- // {
104- // "created_at": "Thu May 10 15:24:15 +0000 2018",
105- // "id_str": "850006245121695744",
106- // "text": "Here is the Tweet message.",
107- // "user": {
108- // },
109- // "place": {
110- // },
111- // "entities": {
112- // },
113- // "extended_entities": {
114- // }
115- // }
116- // `
117- //
118- // }
100+ private toCsv ( flags : any , args : any ) {
101+ Logger . progressStart ( this , '' )
102+ Utilities . truncateFile ( this , flags . output )
103+
104+ let prependHeader = true // only write on the first line
105+
106+ avro . createFileDecoder ( flags . file )
107+ . on ( 'data' , function ( recordStr ) {
108+ // @ts -ignore
109+ let json = JSON . parse ( JSON . stringify ( recordStr ) )
110+ Json2Csv . json2csv ( json , ( err ?: Error , csv ?: string ) => {
111+ if ( csv )
112+ Utilities . appendStringToFile ( this , flags . output , csv + '\n' )
113+ if ( err )
114+ Logger . error ( this , err )
115+ } , { prependHeader} )
116+ prependHeader = false
117+ } )
118+ Logger . progressStop ( this , "done" )
119+ Logger . success ( this , `${ chalk . blue ( 'Csv' ) } written to file: ${ chalk . green ( flags . output ) } ` ) // this will output error and exit command
120+ }
119121
120122 private toAvro ( flags : any , args : any ) {
121123 if ( ! flags . schemaType )
@@ -129,9 +131,9 @@ export default class Avro extends Command {
129131// We write the records to the block encoder, which will take care of serializing them
130132// into an object container file.
131133
132- let jsonStr = '[' + Utilities . getInputString ( this , flags , args ) + ']'
133- jsonStr = jsonStr . replace ( / [ \s \n ] + / mg , '' )
134- jsonStr = jsonStr . replace ( / \} \{ / mg , '},{' )
134+ let inputString = Utilities . getInputString ( this , flags , args )
135+ let jsonStr = this . convertAvroJsonToValidJson ( inputString )
136+
135137 let jsonObjects = JSON . parse ( jsonStr )
136138
137139 jsonObjects . forEach ( function ( data : any ) {
@@ -145,4 +147,11 @@ export default class Avro extends Command {
145147 Logger . success ( this , `${ chalk . blue ( 'Avro' ) } written to file: ${ chalk . green ( flags . output ) } ` ) // this will output error and exit command
146148 avroEncoder . end ( )
147149 }
150+
151+ private convertAvroJsonToValidJson ( json : string ) {
152+ let jsonStr = '[' + json + ']'
153+ jsonStr = jsonStr . replace ( / [ \s \n ] + / mg, '' )
154+ jsonStr = jsonStr . replace ( / \} \{ / mg, '},{' )
155+ return jsonStr
156+ }
148157}
0 commit comments