Skip to main content
Glama
orneryd

M.I.M.I.R - Multi-agent Intelligent Memory & Insight Repository

by orneryd
import.go10.4 kB
// Package import provides APOC import functions. // // This package implements all apoc.import.* functions for importing // graph data from various formats (JSON, CSV, GraphML). package apocimport import ( "encoding/csv" "encoding/json" "fmt" "io" "strconv" "strings" ) // Node represents a graph node. type Node struct { ID int64 Labels []string Properties map[string]interface{} } // Relationship represents a graph relationship. type Relationship struct { ID int64 Type string StartNode int64 EndNode int64 Properties map[string]interface{} } // ImportResult represents the result of an import operation. type ImportResult struct { Nodes int Relationships int Properties int Time int64 Errors []string } // Json imports data from JSON format. // // Example: // // apoc.import.json('/path/to/data.json') => import result func Json(filePath string) (*ImportResult, error) { // Placeholder - would read and parse JSON file return &ImportResult{ Nodes: 0, Relationships: 0, Properties: 0, Time: 0, Errors: []string{}, }, nil } // JsonData imports JSON data from a string. // // Example: // // apoc.import.json.data('{"nodes": [...]}') => import result func JsonData(jsonStr string) (*ImportResult, error) { var data map[string]interface{} if err := json.Unmarshal([]byte(jsonStr), &data); err != nil { return nil, err } result := &ImportResult{ Nodes: 0, Relationships: 0, Properties: 0, Errors: []string{}, } // Parse nodes if nodes, ok := data["nodes"].([]interface{}); ok { result.Nodes = len(nodes) for _, n := range nodes { if nodeMap, ok := n.(map[string]interface{}); ok { if props, ok := nodeMap["properties"].(map[string]interface{}); ok { result.Properties += len(props) } } } } // Parse relationships if rels, ok := data["relationships"].([]interface{}); ok { result.Relationships = len(rels) for _, r := range rels { if relMap, ok := r.(map[string]interface{}); ok { if props, ok := relMap["properties"].(map[string]interface{}); ok { result.Properties += len(props) } } } } return result, nil } // Csv imports data from CSV format. // // Example: // // apoc.import.csv('/path/to/data.csv', {delimiter: ','}) => import result func Csv(filePath string, config map[string]interface{}) (*ImportResult, error) { // Placeholder - would read and parse CSV file return &ImportResult{ Nodes: 0, Relationships: 0, Properties: 0, Time: 0, Errors: []string{}, }, nil } // CsvData imports CSV data from a string. // // Example: // // apoc.import.csv.data('id,name\n1,Alice\n2,Bob', {}) => import result func CsvData(csvStr string, config map[string]interface{}) (*ImportResult, error) { delimiter := ',' if d, ok := config["delimiter"].(string); ok && len(d) > 0 { delimiter = rune(d[0]) } reader := csv.NewReader(strings.NewReader(csvStr)) reader.Comma = delimiter result := &ImportResult{ Nodes: 0, Properties: 0, Errors: []string{}, } // Read header _, err := reader.Read() if err != nil { return nil, err } // Read rows for { row, err := reader.Read() if err == io.EOF { break } if err != nil { result.Errors = append(result.Errors, err.Error()) continue } result.Nodes++ result.Properties += len(row) } return result, nil } // GraphML imports data from GraphML format. // // Example: // // apoc.import.graphml('/path/to/data.graphml', {}) => import result func GraphML(filePath string, config map[string]interface{}) (*ImportResult, error) { // Placeholder - would read and parse GraphML file return &ImportResult{ Nodes: 0, Relationships: 0, Properties: 0, Time: 0, Errors: []string{}, }, nil } // GraphMLData imports GraphML data from a string. // // Example: // // apoc.import.graphml.data('<graphml>...</graphml>', {}) => import result func GraphMLData(graphMLStr string, config map[string]interface{}) (*ImportResult, error) { // Placeholder - would parse GraphML XML return &ImportResult{ Nodes: 0, Relationships: 0, Properties: 0, Errors: []string{}, }, nil } // Cypher imports data from Cypher statements. // // Example: // // apoc.import.cypher('/path/to/statements.cypher', {}) => import result func Cypher(filePath string, config map[string]interface{}) (*ImportResult, error) { // Placeholder - would read and execute Cypher statements return &ImportResult{ Nodes: 0, Relationships: 0, Properties: 0, Time: 0, Errors: []string{}, }, nil } // CypherData imports Cypher statements from a string. // // Example: // // apoc.import.cypher.data('CREATE (n:Person {name: "Alice"})', {}) => import result func CypherData(cypherStr string, config map[string]interface{}) (*ImportResult, error) { // Split by semicolon statements := strings.Split(cypherStr, ";") result := &ImportResult{ Nodes: 0, Properties: 0, Errors: []string{}, } for _, stmt := range statements { stmt = strings.TrimSpace(stmt) if stmt == "" { continue } // Simple parsing to count CREATE statements if strings.HasPrefix(strings.ToUpper(stmt), "CREATE") { result.Nodes++ } } return result, nil } // File imports data from a file (auto-detects format). // // Example: // // apoc.import.file('/path/to/data.json', {}) => import result func File(filePath string, config map[string]interface{}) (*ImportResult, error) { // Detect format from extension if strings.HasSuffix(filePath, ".json") { return Json(filePath) } else if strings.HasSuffix(filePath, ".csv") { return Csv(filePath, config) } else if strings.HasSuffix(filePath, ".graphml") || strings.HasSuffix(filePath, ".xml") { return GraphML(filePath, config) } else if strings.HasSuffix(filePath, ".cypher") || strings.HasSuffix(filePath, ".cql") { return Cypher(filePath, config) } return nil, fmt.Errorf("unsupported file format: %s", filePath) } // Url imports data from a URL. // // Example: // // apoc.import.url('http://example.com/data.json', {}) => import result func Url(url string, config map[string]interface{}) (*ImportResult, error) { // Placeholder - would fetch and import from URL return &ImportResult{ Nodes: 0, Relationships: 0, Properties: 0, Time: 0, Errors: []string{}, }, nil } // Stream imports data from a stream/reader. // // Example: // // apoc.import.stream(reader, 'json', {}) => import result func Stream(reader io.Reader, format string, config map[string]interface{}) (*ImportResult, error) { // Placeholder - would read from stream and import return &ImportResult{ Nodes: 0, Relationships: 0, Properties: 0, Time: 0, Errors: []string{}, }, nil } // Batch imports data in batches for better performance. // // Example: // // apoc.import.batch(data, 1000, func) => import result func Batch(data []interface{}, batchSize int, importFunc func([]interface{}) error) (*ImportResult, error) { result := &ImportResult{ Nodes: 0, Errors: []string{}, } for i := 0; i < len(data); i += batchSize { end := i + batchSize if end > len(data) { end = len(data) } batch := data[i:end] if err := importFunc(batch); err != nil { result.Errors = append(result.Errors, err.Error()) } else { result.Nodes += len(batch) } } return result, nil } // ParseCsvLine parses a single CSV line. // // Example: // // apoc.import.parseCsvLine('1,Alice,30', ',') => ['1', 'Alice', '30'] func ParseCsvLine(line string, delimiter rune) []string { reader := csv.NewReader(strings.NewReader(line)) reader.Comma = delimiter record, err := reader.Read() if err != nil { return []string{} } return record } // ParseJsonLine parses a single JSON line (JSONL format). // // Example: // // apoc.import.parseJsonLine('{"name":"Alice"}') => map func ParseJsonLine(line string) (map[string]interface{}, error) { var data map[string]interface{} if err := json.Unmarshal([]byte(line), &data); err != nil { return nil, err } return data, nil } // ConvertType converts a string value to the specified type. // // Example: // // apoc.import.convertType('42', 'int') => 42 func ConvertType(value string, targetType string) (interface{}, error) { switch strings.ToLower(targetType) { case "int", "integer": return strconv.ParseInt(value, 10, 64) case "float", "double": return strconv.ParseFloat(value, 64) case "bool", "boolean": return strconv.ParseBool(value) case "string": return value, nil default: return value, nil } } // ValidateSchema validates imported data against a schema. // // Example: // // apoc.import.validateSchema(data, schema) => {valid: true, errors: []} func ValidateSchema(data interface{}, schema map[string]interface{}) map[string]interface{} { // Placeholder - would implement schema validation return map[string]interface{}{ "valid": true, "errors": []string{}, } } // Transform applies transformations to imported data. // // Example: // // apoc.import.transform(data, transformFunc) => transformed data func Transform(data []interface{}, transformFunc func(interface{}) interface{}) []interface{} { result := make([]interface{}, len(data)) for i, item := range data { result[i] = transformFunc(item) } return result } // Filter filters imported data based on a predicate. // // Example: // // apoc.import.filter(data, filterFunc) => filtered data func Filter(data []interface{}, filterFunc func(interface{}) bool) []interface{} { result := make([]interface{}, 0) for _, item := range data { if filterFunc(item) { result = append(result, item) } } return result } // Merge merges multiple import results. // // Example: // // apoc.import.merge(result1, result2) => merged result func Merge(results ...*ImportResult) *ImportResult { merged := &ImportResult{ Nodes: 0, Relationships: 0, Properties: 0, Time: 0, Errors: []string{}, } for _, result := range results { merged.Nodes += result.Nodes merged.Relationships += result.Relationships merged.Properties += result.Properties merged.Time += result.Time merged.Errors = append(merged.Errors, result.Errors...) } return merged }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/orneryd/Mimir'

If you have feedback or need assistance with the MCP directory API, please join our Discord server