<jupyter_start><jupyter_text>Export Data Sources and Collections
This notebook is an example of how to export sources to CSV.<jupyter_code>purviewName = "purview-sandbox"
# Helper Methods
import json
def getJSON(raw_output):
    output = ''.join(raw_output)
    json_obj = json.loads(output)
    return json_obj

import csv
def export(table):
    with open('output.csv', 'w',newline='', encoding="utf-8") as output:
        csv_writer = csv.writer(output)
        csv_writer.writerows(table)
# Get list of all data sources
data = !pv scan readDatasources --purviewName {purviewName}
sources = getJSON(data)
count = sources['count']
print(f'We found {count} sources in account {purviewName}.')
# Get source attributes (name, kind, parent)
mapCollectionParent = {}
sourcesList = []
for source in sources['value']:
    name = source['name']
    kind = source['kind']
    if source['properties']['parentCollection'] is not None:
        parent = source['properties']['parentCollection']['referenceName']
    else:
        parent = None

    if kind == 'Collection' and parent is not None:
        mapCollectionParent[name] = parent
    elif kind != 'Collection':
        item = {
            'name': name,
            'kind': kind,
            'parent': parent
        }
        sourcesList.append(item)

numberOfSources = len(sourcesList)
numberOfCollections = count - numberOfSources
print(f'There are {numberOfSources} sources and {numberOfCollections} collections.')
    
# Build a map of collection name to collection path
mapCollectionPath = {}
for collection in mapCollectionParent:
    hasParent = True
    path = collection
    parentCollection = mapCollectionParent[collection]
    while hasParent:
        path = parentCollection + " > " + path
        if parentCollection in mapCollectionParent:
            parentCollection = mapCollectionParent[parentCollection]
        else:
            hasParent = False
    mapCollectionPath[collection] = path
print('Constructed full path names for each collection.')
# Export to CSV
table = []
headers = ('name','kind','path')
table.append(headers)

for source in sourcesList:
    parent = source['parent']
    path = mapCollectionPath[parent]
    row = (source['name'], source['kind'], path)
    table.append(row) 

export(table)
print('Sources exported to CSV.')<jupyter_output>Sources exported to CSV.
{'fraction_non_alphanumeric': 0.10550268928423666, 'fraction_numerical': 0.0004137360364087712, 'mean_word_length': 3.294849023090586, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '476264', 'license': 'MIT', 'n_tokens_mistral': 688, 'n_tokens_neox': 661, 'n_words': 196, 'path': 'samples/notebooks (plus)/export_sources.ipynb', 'repo_name': 'mdrakiburrahman/purviewcli', 'size': '2417'}