Socrata 0.0.3-beta
See the version list below for details.
dotnet add package Socrata --version 0.0.3-beta
NuGet\Install-Package Socrata -Version 0.0.3-beta
<PackageReference Include="Socrata" Version="0.0.3-beta" />
paket add Socrata --version 0.0.3-beta
#r "nuget: Socrata, 0.0.3-beta"
// Install Socrata as a Cake Addin #addin nuget:?package=Socrata&version=0.0.3-beta&prerelease // Install Socrata as a Cake Tool #tool nuget:?package=Socrata&version=0.0.3-beta&prerelease
tyler-socrata-net-sdk
.NET Socrata SDK for reading and writing to Socrata APIs
SocrataClient
// Create the client
ISocrataClient client = new SocrataClient(new Uri("https://{domain}.data.socrata.com"), "apikey", "apisecret");
// Optionally validate the client connection
bool IsValidConnection = client.ValidateConnection();
Socrata Resources
// Get a resource by its ID*
Resource dataset = client.GetResource("abcd-1234");
Note Dataset IDs
Working with Resources
Data Manipulation
Socrata Data Types and You
Socrata Data Type | Rough SQL Mapping | Example |
---|---|---|
Text | varchar, text | "Text" |
Number | int, float, double | 1, 3.14 |
Date | convert(date, 426) | "2020-01-01" |
Timestamp | convert(datetime, 426) | "2020-01-01T13:42:10" |
Boolean | boolean, bit | 1, true |
Null | null | null |
Notes:
- The escape character for text values is
\
- Dates must be passed in as string
- Datetimes will not be converted to specific timezones, so be sure all the dates are in the correct/same timezone when they are exported
- Decimals are demarcated with a
.
- All of these will be converted to JSON when sent to Socrata, so when in doubt, consult the
Geospatial Data
While the Socrata platform does support geocoding, it is not supported via this SDK currently. Any geospatial data will need to converted into Well-Known Text (WKT) with WGS84 (ESPG 4326) Latitude and Longitude values.
In general, a decimal precision of 5 is sufficient.
Socrata Data Type | Example |
---|---|
Point | "POINT (37.192 -168.223)" |
MultiPoint | "MULTIPOINT ((37.192 -168.223),(42.1110 -172.9339))" |
LineString | "LINESTRING (30 10, 10 30, 40 40)" |
MultiLineString | "MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))" |
Polygon | "POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))" |
MultiPolygon | "MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))" |
Direct Row Manipulation
Insert
// Insert Functionality
List<Dictionary<string, string>> newRecords = new List<Dictionary<string, string>>
{
new Dictionary<string, string>{{"api_field", "nextvalue"},{"other_field", "test"}}
};
Result result = dataset.Rows().Insert(newRecords);
// Check if it was successful
Assert.IsFalse(Result.IsError);
// Check the results
Assert.AreEqual(newRecords.Length, result.Inserted);
Update
// Update Functionality
List<Dictionary<string, object>> recordsToUpdate = new List<Dictionary<string, object>>
{
new Dictionary<string, object>{{"row_identifier", 1},{"column_that_changed", "newvalue"}}
};
Result result = dataset.Rows().Update(recordsToUpdate);
// Check if it was successful
Assert.IsFalse(Result.IsError);
// Check the results
Assert.AreEqual(recordsToUpdate.Length, result.Updated);
Delete
// Delete Functionality
List<Dictionary<string, string>> recordsToDelete = new List<Dictionary<string, string>>
{
new Dictionary<string, string>{{"row_identifier", "ID-1"}}
};
Result result = dataset.Rows().Delete(recordsToDelete);
// Check if it was successful
Assert.IsFalse(Result.IsError);
// Check the results
Assert.AreEqual(recordsToDelete.Length, result.Deleted);
Dataset Management API
Revision Types
Replace: A full replace of the dataset Update: A upsert of the contents based off the row identifier Delete: A delete of the contents based off the row identifier
File Types
file extension | content type |
---|---|
CSV | text/csv |
TSV | text/tsv |
KML | google-kml |
ZIP (shapefile) | application/octet-stream |
BLOB | application/octet-stream |
Using an Import Config
Small (in memory) file uploads
Resource resource = socrataClient.GetResource("tzmz-8bnb");
Revision revision = resource.OpenRevision(RevisionType.REPLACE);
Source source = revision.CreateUploadSource("test-csv.csv");
string filepath = @"C:\Path\To\MyFile.csv";
string csv = System.IO.File.ReadAllText(filepath);
source.AddBytesToSource(csv);
source.AwaitCompletion(status => Console.WriteLine(status));
revision.Apply();
revision.AwaitCompletion(status => Console.WriteLine(status));
Large (Streaming) file uploads
Resource resource = socrataClient.GetResource("tzmz-8bnb");
Revision revision = resource.OpenRevision(RevisionType.REPLACE);
Source source = revision.CreateStreamingSource("test-csv.csv");
ByteSink sink = source.StreamingSource(ContentType.CSV);
//Open the stream and read the file.
sink.FileSink(@"C:\Path\To\MyFile.csv");
source.AwaitCompletion(status => Console.WriteLine(status));
revision.Apply();
revision.AwaitCompletion(status => Console.WriteLine(status));
Handling error rows
if(source.HasErrorRows())
{
string errorFile = "C:\Path\To\ErrorRows.csv";
int numberOfErrors = source.NumberOfErrors();
source.ExportErrorRows(errorFile);
// Throw an exception
throw new Exception($"{numberOfErrors} Error(s) were found in the upload");
}
Dataset Metadata Only Revisions
Resource resource = socrataClient.GetResource("tzmz-8bnb");
Revision revision = resource.OpenRevision(RevisionType.REPLACE);
revision.CreateViewSource();
revision.SetDescription("New description for the <b>resource</b>");
revision.RenameResource("New Name");
revision.Apply();
revision.AwaitCompletion(status => Console.WriteLine(status));
Column Metadata Revisions
Resource resource = socrataClient.GetResource("tzmz-8bnb");
Revision revision = resource.OpenRevision(RevisionType.REPLACE);
Source source = revision.CreateViewSource();
revision.Apply();
Reading Data
You can also use the Rows() to access records in the dataset
/*
Example custom class
[DataContract]
class MyResource
{
[DataMember(Name = "column_field")]
public string ColumnField { get; internal set; }
}
*/
Resource resource = socrataClient.GetResource("tzmz-8bnb");
Rows rows = resource.Rows();
List<MyResource> allRecords = rows.FetchAll<MyResource>();
long limit = 1000;
long offset = 0;
List<MyResource> someRecords = rows.Fetch<MyResource>(limit, offset);
// Conversely, you can just use a Map<string, object>
List<Map<string, object>> allRecords = rows.FetchAll<Map<string, object>>();
Pagination
You might have a really big dataset, which will require paginating over the result set.
Resource resource = socrataClient.GetResource("tzmz-8bnb");
Rows rows = resource.Rows();
// Get the total number of records
long total = rows.Count();
long limit = 1000;
long offset = 0;
while(offset < total)
{
List<Map<string, object>> allRecords = rows.Fetch<Map<string, object>>(limit, offset);
offset += limit;
}
Metadata
Accessing Resource Metadata
Resource dataset = client.GetResource("abcd-1234");
string resourceName = dataset.metadata.Name;
Running Tests
dotnet test
Or a specific test:
dotnet test --filter {testname}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net5.0
- Microsoft.NET.Test.Sdk (>= 16.5.0)
- Newtonsoft.Json (>= 12.0.3)
- NUnit (>= 3.12.0)
- NUnit3TestAdapter (>= 3.16.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.5.1 | 123 | 8/8/2024 |
2.5.0 | 81 | 8/5/2024 |
2.4.0 | 330 | 12/21/2023 |
2.3.0 | 160 | 10/9/2023 |
2.2.0 | 368 | 4/11/2023 |
2.1.0 | 194 | 4/7/2023 |
2.0.1 | 197 | 4/6/2023 |
2.0.0 | 211 | 3/28/2023 |
1.0.1 | 407 | 9/20/2021 |
1.0.0 | 338 | 5/17/2021 |
0.0.7-beta | 269 | 5/3/2021 |
0.0.6-beta | 218 | 5/3/2021 |
0.0.5-beta | 206 | 5/3/2021 |
0.0.4-beta | 234 | 5/3/2021 |
0.0.3-beta | 203 | 4/29/2021 |