Data Extraction and Analysis
This use case demonstrates how to use the NeoSpace API to extract, process, and analyze data in different programming languages.
Overview
Data extraction and analysis is a common use case for APIs. In this example, we will:
- Retrieve a dataset from the NeoSpace API
- Process and analyze this data
- Visualize or export the results
Examples in Different Languages
Node.js
// Simple example of data extraction and analysis with Node.js
const axios = require('axios');
// API Configuration
const apiKey = 'your_api_key_here';
const baseUrl = 'https://api.neospace.ai/v1';
// Function to fetch data
async function fetchData(datasetId) {
try {
const response = await axios.get(`${baseUrl}/datasets/${datasetId}/data`, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
return response.data;
} catch (error) {
console.error('Error fetching data:', error.message);
throw error;
}
}
// Function to analyze data
function analyzeData(data) {
// Simple example: calculate average of values
const values = data.items.map(item => item.value);
const sum = values.reduce((acc, val) => acc + val, 0);
const average = sum / values.length;
return {
count: values.length,
sum,
average,
min: Math.min(...values),
max: Math.max(...values)
};
}
// Main function
async function main() {
try {
// 1. Fetch data
const datasetId = 'dataset_12345';
const data = await fetchData(datasetId);
console.log(`Data retrieved: ${data.items.length} items`);
// 2. Analyze data
const analysis = analyzeData(data);
console.log('Analysis:', analysis);
// 3. Display results
console.log(`
Analysis Report:
---------------------
Number of items: ${analysis.count}
Total value: ${analysis.sum}
Average: ${analysis.average.toFixed(2)}
Minimum value: ${analysis.min}
Maximum value: ${analysis.max}
`);
} catch (error) {
console.error('Error in analysis:', error);
}
}
main();
Python
# Simple example of data extraction and analysis with Python
import requests
import statistics
# API Configuration
api_key = "your_api_key_here"
base_url = "https://api.neospace.ai/v1"
# Function to fetch data
def fetch_data(dataset_id):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
response = requests.get(
f"{base_url}/datasets/{dataset_id}/data",
headers=headers
)
response.raise_for_status()
return response.json()
# Function to analyze data
def analyze_data(data):
values = [item["value"] for item in data["items"]]
return {
"count": len(values),
"sum": sum(values),
"average": statistics.mean(values),
"median": statistics.median(values),
"stdev": statistics.stdev(values) if len(values) > 1 else 0
}
# Main function
def main():
try:
# 1. Fetch data
dataset_id = "dataset_12345"
data = fetch_data(dataset_id)
print(f"Data retrieved: {len(data['items'])} items")
# 2. Analyze data
analysis = analyze_data(data)
# 3. Display results
print("\nAnalysis Report:")
print("---------------------")
print(f"Number of items: {analysis['count']}")
print(f"Total value: {analysis['sum']}")
print(f"Average: {analysis['average']:.2f}")
print(f"Median: {analysis['median']:.2f}")
print(f"Standard deviation: {analysis['stdev']:.2f}")
except Exception as e:
print(f"Error in analysis: {str(e)}")
if __name__ == "__main__":
main()
Java
// Simple example of data extraction and analysis with Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
import java.util.DoubleSummaryStatistics;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
public class NeoSpaceDataAnalysis {
private static final String API_KEY = "your_api_key_here";
private static final String BASE_URL = "https://api.neospace.ai/v1";
public static void main(String[] args) {
try {
// 1. Fetch data
String datasetId = "dataset_12345";
Map<String, Object> data = fetchData(datasetId);
List<Map<String, Object>> items = (List<Map<String, Object>>) data.get("items");
System.out.println("Data retrieved: " + items.size() + " items");
// 2. Analyze data
Map<String, Object> analysis = analyzeData(items);
// 3. Display results
System.out.println("\nAnalysis Report:");
System.out.println("---------------------");
System.out.println("Number of items: " + analysis.get("count"));
System.out.println("Total value: " + analysis.get("sum"));
System.out.println("Average: " + String.format("%.2f", analysis.get("average")));
System.out.println("Minimum value: " + analysis.get("min"));
System.out.println("Maximum value: " + analysis.get("max"));
} catch (Exception e) {
System.err.println("Error in analysis: " + e.getMessage());
}
}
private static Map<String, Object> fetchData(String datasetId) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL + "/datasets/" + datasetId + "/data"))
.header("Authorization", "Bearer " + API_KEY)
.header("Content-Type", "application/json")
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new RuntimeException("API error: " + response.statusCode());
}
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(response.body(), Map.class);
}
private static Map<String, Object> analyzeData(List<Map<String, Object>> items) {
// Extract numeric values
List<Double> values = items.stream()
.map(item -> ((Number) item.get("value")).doubleValue())
.collect(Collectors.toList());
// Calculate statistics
DoubleSummaryStatistics stats = values.stream().mapToDouble(v -> v).summaryStatistics();
return Map.of(
"count", stats.getCount(),
"sum", stats.getSum(),
"average", stats.getAverage(),
"min", stats.getMin(),
"max", stats.getMax()
);
}
}
Go
// Simple example of data extraction and analysis with Go
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
)
const (
apiKey = "your_api_key_here"
baseURL = "https://api.neospace.ai/v1"
)
// Structures for data
type DatasetResponse struct {
Items []DataItem `json:"items"`
}
type DataItem struct {
ID string `json:"id"`
Value float64 `json:"value"`
}
// Structure for analysis
type DataAnalysis struct {
Count int `json:"count"`
Sum float64 `json:"sum"`
Average float64 `json:"average"`
Median float64 `json:"median"`
Min float64 `json:"min"`
Max float64 `json:"max"`
}
// Function to fetch data
func fetchData(datasetID string) (*DatasetResponse, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/datasets/%s/data", baseURL, datasetID), nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+apiKey)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API error: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var data DatasetResponse
if err := json.Unmarshal(body, &data); err != nil {
return nil, err
}
return &data, nil
}
// Function to analyze data
func analyzeData(data *DatasetResponse) DataAnalysis {
// Extract values
values := make([]float64, len(data.Items))
var sum float64
for i, item := range data.Items {
values[i] = item.Value
sum += item.Value
}
// Sort for median calculation
sort.Float64s(values)
// Calculate median
var median float64
count := len(values)
if count == 0 {
median = 0
} else if count%2 == 0 {
median = (values[count/2-1] + values[count/2]) / 2
} else {
median = values[count/2]
}
// Calculate statistics
analysis := DataAnalysis{
Count: count,
Sum: sum,
Average: 0,
Median: median,
Min: 0,
Max: 0,
}
if count > 0 {
analysis.Average = sum / float64(count)
analysis.Min = values[0]
analysis.Max = values[count-1]
}
return analysis
}
func main() {
// 1. Fetch data
datasetID := "dataset_12345"
data, err := fetchData(datasetID)
if err != nil {
fmt.Printf("Error fetching data: %s\n", err)
return
}
fmt.Printf("Data retrieved: %d items\n", len(data.Items))
// 2. Analyze data
analysis := analyzeData(data)
// 3. Display results
fmt.Println("\nAnalysis Report:")
fmt.Println("---------------------")
fmt.Printf("Number of items: %d\n", analysis.Count)
fmt.Printf("Total value: %.2f\n", analysis.Sum)
fmt.Printf("Average: %.2f\n", analysis.Average)
fmt.Printf("Median: %.2f\n", analysis.Median)
fmt.Printf("Minimum value: %.2f\n", analysis.Min)
fmt.Printf("Maximum value: %.2f\n", analysis.Max)
}
Benefits
Simplified data access: Easily retrieve data from the NeoSpace platform Automated analysis: Process data without manual intervention Data-driven decisions: Obtain insights for informed decision-making
Next Steps
Explore the Data Processing API for more advanced features Learn to create data visualizations with the results Implement automated workflows for periodic analysis