Skip to content

Commit

Permalink
#19 flatten avro data objects for data view display
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed Jun 1, 2019
1 parent 65d0f75 commit d5bb922
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
27 changes: 23 additions & 4 deletions src/data.preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ export class DataPreview {
break;
case '.avro':
data = this.getAvroData(dataFilePath);
console.log('avro data:', JSON.stringify(data, null, 2));
break;
case '.parquet':
// TODO: add parquet data read
Expand Down Expand Up @@ -422,15 +423,16 @@ export class DataPreview {
* @param dataFilePath Avro data file path.
* @returns Array of row objects.
*/
private async getAvroData(dataFilePath: string) {
private async getAvroData(dataFilePath: string): Promise<any[]> {
let dataRows: Array<any> = [];
let dataSchema: any = {};
const dataBlockDecoder: avro.streams.BlockDecoder = avro.createFileDecoder(dataFilePath);
dataBlockDecoder.on('metadata', (type: any) => dataSchema = type);
dataBlockDecoder.on('data', (data: any) => dataRows.push(data));
await new Promise(resolve => dataBlockDecoder.on('end', () => resolve()));
this.logAvroDataStats(dataSchema, dataRows);
return dataRows;
await new Promise(resolve => dataBlockDecoder.on('end', () => resolve()));
const rows = dataRows.map(rowObject => this.flattenObject(rowObject));
this.logAvroDataStats(dataSchema, rows);
return rows;
} // end of getAvroData()

/**
Expand All @@ -453,6 +455,23 @@ export class DataPreview {
}
}

/**
* Flattens objects with nested properties for data view display.
* @param obj Object to flatten.
* @returns Flat Object.
*/
private flattenObject (obj: any): any {
const flatObject: any = {};
Object.keys(obj).forEach((key) => {
if (typeof obj[key] === 'object' && obj[key] !== null) {
Object.assign(flatObject, this.flattenObject(obj[key]));
} else {
flatObject[key] = obj[key];
}
});
return flatObject;
}

/**
* Disposes this preview resources.
*/
Expand Down
24 changes: 12 additions & 12 deletions templates/data.view.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@
// try to parse JSON array data
tableData = JSON.parse(data);
break;
case '.xls':
case '.xlsb':
case '.xlsx':
case '.xlsm':
case '.slk':
case '.ods':
case '.prn':
case '.dif':
case '.xml':
case '.html':
// pass through Excel files data json
case '.xls':
case '.xlsb':
case '.xlsx':
case '.xlsm':
case '.slk':
case '.ods':
case '.prn':
case '.dif':
case '.xml':
case '.html':
case '.avro':
// pass through loaded data json
tableData = data;
console.log(`data.view:getData(): records count: ${tableData.length}`);
break;
case '.arrow':
case '.avro':
// pass through arrow table data json
tableData = data;
console.log(`data.view:getData(): schema: ${JSON.stringify(schema, null, 2)}`);
Expand Down

0 comments on commit d5bb922

Please sign in to comment.