Created
September 1, 2022 13:04
-
-
Save adamcbuckley/05ac4e4115b483024532e6f44e6da1a7 to your computer and use it in GitHub Desktop.
Creates an AWS QuickSight dataset, where the source data is supplied as JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
CreateDataSetCommand, | |
CreateDataSourceCommand, | |
DataSetImportMode, | |
DataSourceType, | |
DescribeDataSourceCommand, | |
QuickSightClient, | |
ResourceStatus | |
} from "@aws-sdk/client-quicksight"; | |
(async function () { | |
const principalId = "arn:aws:quicksight:eu-west-2:111111111111:user/default/AWSReservedSSO_AdministratorAccess_3ff56372d9718f05/[email protected]"; | |
const accountId = "111111111111" | |
const quickSightClient = new QuickSightClient({}); | |
const dataSourceId = "MyDataSource"; | |
const dataSetId = "MyDataSet"; | |
// Create the underlying data source | |
const createDataSourceCommandOutput = await quickSightClient.send(new CreateDataSourceCommand({ | |
AwsAccountId: accountId, | |
DataSourceId: dataSourceId, | |
Name: dataSourceId, | |
Type: DataSourceType.S3, | |
DataSourceParameters: { | |
S3Parameters: { | |
ManifestFileLocation: { | |
Bucket: "MyBucketName", | |
Key: "manifest.json" | |
} | |
} | |
}, | |
Permissions: [{ | |
"Actions": [ | |
"quicksight:UpdateDataSourcePermissions", | |
"quicksight:DescribeDataSourcePermissions", | |
"quicksight:PassDataSource", | |
"quicksight:DescribeDataSource", | |
"quicksight:DeleteDataSource", | |
"quicksight:UpdateDataSource" | |
], | |
"Principal": principalId | |
}] | |
})); | |
console.log("\ncreateDataSourceCommandOutput", JSON.stringify(createDataSourceCommandOutput, null, 4), "\n"); | |
const dataSourceArn = createDataSourceCommandOutput.Arn!; | |
// Wait until it has been created | |
let status = createDataSourceCommandOutput.CreationStatus; | |
console.log("DataSource status: " + status); | |
while (status !== ResourceStatus.CREATION_SUCCESSFUL) { | |
await new Promise(f => setTimeout(f, 1000)); | |
const describeDataSourceCommandOutput = await quickSightClient.send(new DescribeDataSourceCommand({ | |
AwsAccountId: accountId, | |
DataSourceId: dataSourceId, | |
})); | |
// console.log("\ndescribeDataSourceCommandOutput", JSON.stringify(describeDataSourceCommandOutput, null, 4), "\n"); | |
status = describeDataSourceCommandOutput.DataSource?.Status; | |
console.log("DataSource status: " + status); | |
} | |
// Create DataSet | |
const createDataSetCommandOutput = await quickSightClient.send(new CreateDataSetCommand({ | |
AwsAccountId: accountId, | |
DataSetId: dataSetId, | |
ImportMode: DataSetImportMode.SPICE, | |
Name: dataSetId, | |
PhysicalTableMap: { | |
"S3SourceMember": { | |
"S3Source": { | |
"DataSourceArn": dataSourceArn, | |
"InputColumns": [ | |
{ | |
"Name": "ColumnId-1", | |
"Type": "STRING" | |
}, | |
{ | |
"Name": "ColumnId-2", | |
"Type": "STRING" | |
}, | |
{ | |
"Name": "ColumnId-3", | |
"Type": "STRING" | |
}, | |
{ | |
"Name": "ColumnId-4", | |
"Type": "STRING" | |
}, | |
{ | |
"Name": "ColumnId-5", | |
"Type": "STRING" | |
} | |
], | |
"UploadSettings": { | |
"ContainsHeader": true, | |
"Format": "JSON", | |
"StartFromRow": 1, | |
"TextQualifier": "DOUBLE_QUOTE" | |
} | |
} | |
} | |
}, | |
"DataSetUsageConfiguration": { | |
"DisableUseAsDirectQuerySource": false, | |
"DisableUseAsImportedSource": false | |
}, | |
"FieldFolders": {}, | |
"LogicalTableMap": { | |
"S3LogicalTableMap": { | |
"Alias": "DataSet_LEAVER", | |
"DataTransforms": [ | |
{ | |
"RenameColumnOperation": { | |
"ColumnName": "ColumnId-1", | |
"NewColumnName": "projectId" | |
} | |
}, | |
{ | |
"RenameColumnOperation": { | |
"ColumnName": "ColumnId-2", | |
"NewColumnName": "completed" | |
} | |
}, | |
{ | |
"CastColumnTypeOperation": { | |
"ColumnName": "completed", | |
"Format": "yyyy-MM-dd'T'HH:mm:ss.SSSZZ", | |
"NewColumnType": "DATETIME" | |
} | |
}, | |
{ | |
"RenameColumnOperation": { | |
"ColumnName": "ColumnId-3", | |
"NewColumnName": "dataType" | |
} | |
}, | |
{ | |
"RenameColumnOperation": { | |
"ColumnName": "ColumnId-4", | |
"NewColumnName": "raterType" | |
} | |
}, | |
{ | |
"RenameColumnOperation": { | |
"ColumnName": "ColumnId-5", | |
"NewColumnName": "status" | |
} | |
}, | |
{ | |
"ProjectOperation": { | |
"ProjectedColumns": [ | |
"projectId", | |
"completed", | |
"dataType", | |
"raterType", | |
"status" | |
] | |
} | |
} | |
], | |
"Source": { | |
"PhysicalTableId": "S3SourceMember" | |
} | |
} | |
}, | |
Permissions: [{ | |
"Principal": principalId, | |
"Actions": [ | |
"quicksight:PassDataSet", | |
"quicksight:DescribeIngestion", | |
"quicksight:CreateIngestion", | |
"quicksight:UpdateDataSet", | |
"quicksight:DeleteDataSet", | |
"quicksight:DescribeDataSet", | |
"quicksight:CancelIngestion", | |
"quicksight:DescribeDataSetPermissions", | |
"quicksight:ListIngestions", | |
"quicksight:UpdateDataSetPermissions" | |
] | |
}], | |
})); | |
console.log("\ncreateDataSetCommandOutput", JSON.stringify(createDataSetCommandOutput, null, 4), "\n"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment