Package: citybrain-platform
Citybrain Platform Python LibraryThe Citybrain Platform Python Library provides convenient access to the Citybrain Platform API from applications written in the Python language.InstallationIf you just want to use the package, just run:pipinstall--upgradecitybrain-platformUsageThe library needs to be configured with your account's secret key which is available onwww.citybrain.org. Either set it as theCITYBRAIN_APIKEYenvironment variable before using the library:exportCITYBRAIN_APIKEY='...'Or setcitybrain_platform.api_keyto its value:importcitybrain_platformcitybrain_platform.api_key="..."Example CodeExamples of how to use this Python library to accomplish various tasks.StorageUpload Fileimportcitybrain_platformres=citybrain_platform.Storage.upload_file(remote_path="test/test/test.file",local_file="test.localfile")print(res)Download Fileimportcitybrain_platformcitybrain_platform.Storage.download_file(remote_path="test/test/test.file",local_file="test.localfile")Delete Fileimportcitybrain_platformres=citybrain_platform.Storage.delete_file(remote_path="test/test/test.file")print(res)List Filesimportcitybrain_platformres=citybrain_platform.Storage.list_files(prefix="testprefix",direct_only=False)print(res)User Defined Function OperationsCreate Functionresult=citybrain_platform.Computing.create_function(name="test_udf",# function nameclass_file="testudf.py",# the file where the class is locatedclass_name="TestUDF2",# class namecomment="test function",resources=["testudf.py","other.py"]# resources used by this function)print(result)Create Resourcefromcitybrain_platformimportResourceTyperesult=citybrain_platform.Computing.create_resource(name="testudf.py",# resource name, must contain extensiontype=ResourceType.PY,# resource typecomment="test resource",local_file="udf.py"# local resource file)print(result)Table OperationsCreate Tableimportcitybrain_platformfromcitybrain_platform.computing.data_typesimportColumn,ColumnTypecolumns=[Column("col_str",ColumnType.STRING,"this is a comment"),Column("col_id",ColumnType.BIGINT),]partition_columns=[Column("col_pt",ColumnType.INT,"a column for partitioning")]ok=citybrain_platform.Computing.create_table(name="test_tbl",columns=columns,partition_columns=partition_columns,# Set those parameters if you want to create external table from Storage files# storage_filesource="datasource directory", # the directory of source files# storage_filetype=data_types.ExternalFiletype.PARQUET # file format of the source, current support PARQUET and CSV)print(ok)Get Table Schemaimportcitybrain_platformschema=citybrain_platform.Computing.get_table_schema(name="test_tbl")print(schema)Upload Table Dataimportcitybrain_platformresult=citybrain_platform.Computing.upload_table_data(name="test_tbl",append=True,csv_filepath="aa.csv",partition_key={"col_pt":"19"})print(result)Truncate Tableimportcitybrain_platformresult=citybrain_platform.Computing.truncate_table(name="test_tbl",partition_key={"col_pt":"19"})print(result)Drop Tableimportcitybrain_platformresult=citybrain_platform.Computing.drop_table(name="test_tbl")print(result)Make Table Public Or Private To Othersimportcitybrain_platformpublic_table_name=citybrain_platform.Computing.update_table_status(name="test_tbl",public=False)print(public_table_name)List Available Tablesimportcitybrain_platformtables=citybrain_platform.Computing.list_tables()print(tables)Job OperationsCreate Jobimportcitybrain_platformjob_id=citybrain_platform.Computing.create_job(sql="select col_str from test_tbl limit 12;")print(job_id)Get Job Statusimportcitybrain_platformjob_status=citybrain_platform.Computing.get_job_status(job_id="...")print(job_status)Stop Running Jobimportcitybrain_platformresult=citybrain_platform.Computing.stop_job(job_id="...")print(result)Download Terminated Job Resultsimportcitybrain_platformcitybrain_platform.Computing.get_job_results(job_id="...",filepath="results.csv")Dataset OperationsAdd Storage Dataimportcitybrain_platformcitybrain_platform.Data.add_storage(name="",description="",file_path="")Add Remote Dataimportcitybrain_platformcitybrain_platform.Data.add_remote(name="",description="",remote_url="")Download Dataimportcitybrain_platformcitybrain_platform.Data.download(data_address="...",save_file="test.txt")