Creating Wave Dataset through Apex

Uploading csv file to wave dataset using Apex

The External Data API is available in API version 31 and later and it enables you to upload external data files (.CSV) into Wave Analytics. If there are any complex relationships in your org and can't able to create a dataset through data flow editor then this is the best way to do it. Analytics External Data API enables you to configure and control external data uploads through InsightsExternalData and InsightsExternalDataPart objects.The InsightsExternalData object is used with the InsightsExternalDataPart object, which holds the parts of the data to be uploaded. Together, they provide a programmatic way to upload a large file in parts and trigger a dataflow into a dataset. 


Below code runs as:
  1. Query the data and create csv file to upload.
  2. Create a JSON metadata file that describes the content.
  3. Create InsightsExternalData and InsightsExternalDataPart to upload csv.
  4. System jobs created and runs.
  5. Dataset created.
/* Create wave analytics data set from apex */
public class wave_accountReport {
    public void accountCSV(){
        String datasetName = 'SampleAccountdt';
        String strCSV = '';
        list<String> csvLst = new list<String>();
// csv header
string strCSVheader = '"Account Name","Industry","Phone"' + '\r\n';
// Query data from account object then add to csv file
        for(Account ac: [SELECT Name, Industry, Phone FROM Account LIMIT 50000]){
            string csvRow = '"'+ac.Name+'","'+ac.Industry+'",'+ac.Phone+''+'\r\n';
// make list of csv rows into string
            for(String str: csvLst)
                strCSV +=str;
// define data types for csv fields, it's optional
        String metadataJson ='{"fileFormat":{"charsetName":"UTF-8","fieldsDelimitedBy":",","numberOfLinesToIgnore":1},"objects":[{"connector":"CIConnector","description":"","fullyQualifiedName":"Account","label":"Account Data","name":"Account","fields":[{"description":"","fullyQualifiedName":"Account.Name","label":"Name","name":"Name","isSystemField":false,"isUniqueId":false,"isMultiValue":false,"type":"Text"},{"description":"","fullyQualifiedName":"Account.Industry","label":"Industry","name":"Industry","isSystemField":false,"isUniqueId":false,"isMultiValue":false,"type":"Text"},{"description":"","fullyQualifiedName":"Account.Phone","label":"Phone","name":"Phone","isSystemField":false,"isUniqueId":false,"isMultiValue":false,"type":"Numeric","defaultValue":"0","precision":3,"scale":0}]}]}';
Blob metadataJsonBlob = Blob.valueof(metadataJson);
// convert csv string to blob
        Blob csvBlob = Blob.valueof(strCSV);
        UploadToWaveAnalytics(csvBlob, metadataJsonBlob,datasetName);
    public void UploadToWaveAnalytics(blob csv, blob metadataJson, string datasetName){
        if(csv != null && csv.size() > 0){
// Create InsightsExternalData
            InsightsExternalData extDataObject = new InsightsExternalData();
            extDataObject.put('Format', 'csv');
            extDataObject.put('EdgemartAlias', datasetName);
            extDataObject.put('Operation', 'Overwrite');
            if(metadataJson != null){
                extDataObject.put('metadataJson', metadataJson);
            extDataObject.put('Action', 'None');
            Insert extDataObject;
// Create InsightsExternalDataPart
            InsightsExternalDataPart extDataPartObject = new InsightsExternalDataPart();
            extDataPartObject.put('DataFile', csv);
            extDataPartObject.put('InsightsExternalDataId', extDataObject.Id);
            extDataPartObject.put('PartNumber', 1);
            Insert extDataPartObject;
            extDataObject.put('Action',  'Process');
            Upsert extDataObject;
To know more about Analytics External Data API. 

Popular posts from this blog

Redirect Visualforce pages in Salesforce Lightning

Apex Test Classes Interview Questions and Answers

How to use Database.Query and Database.countQuery