Skip to content

krishcdbry/nexaclient-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

NexaDB Java Client

Enterprise-grade Java client for NexaDB with full Spring Boot support.

Features

  • Binary Protocol: MessagePack-based binary protocol for maximum performance (10x faster than REST)
  • Auto-Reconnection: Automatic reconnection with exponential backoff
  • Thread-Safe: Production-ready concurrency support
  • Spring Boot Integration: Auto-configuration and seamless integration
  • Type-Safe: Full type safety with Java generics
  • Comprehensive API: All NexaDB operations supported
  • Change Streams: Real-time event notifications (MongoDB-style)
  • Vector Search: HNSW-based similarity search for AI/ML applications

Installation

Maven

<dependency>
    <groupId>com.nexadb</groupId>
    <artifactId>nexadb-java-client</artifactId>
    <version>2.3.0</version>
</dependency>

Gradle

implementation 'com.nexadb:nexadb-java-client:2.3.0'

Quick Start

Standalone Java Application

import com.nexadb.client.NexaClient;
import java.util.Map;
import java.util.List;

public class Example {
    public static void main(String[] args) {
        // Create and connect
        try (NexaClient client = new NexaClient("localhost", 6970, "root", "nexadb123")) {
            client.connect();

            // Create document
            Map<String, Object> user = Map.of(
                "name", "Alice",
                "email", "alice@example.com",
                "age", 28
            );
            Map<String, Object> result = client.create("users", user);
            String docId = (String) result.get("document_id");
            System.out.println("Created: " + docId);

            // Query documents
            List<Map<String, Object>> users = client.query("users",
                Map.of("age", Map.of("$gte", 18)), 10);
            System.out.println("Found " + users.size() + " users");

            // Update document
            client.update("users", docId, Map.of("age", 29));

            // Delete document
            client.delete("users", docId);
        }
    }
}

Spring Boot Application

1. Add Dependency

<dependency>
    <groupId>com.nexadb</groupId>
    <artifactId>nexadb-java-client</artifactId>
    <version>2.3.0</version>
</dependency>

2. Configure in application.properties

# NexaDB Configuration
nexadb.host=localhost
nexadb.port=6970
nexadb.username=root
nexadb.password=nexadb123
nexadb.timeout=30000
nexadb.max-retries=3

Or in application.yml:

nexadb:
  host: localhost
  port: 6970
  username: root
  password: nexadb123
  timeout: 30000
  max-retries: 3

3. Use in Your Service

import com.nexadb.client.NexaClient;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;

@Service
public class UserService {

    private final NexaClient nexaClient;

    public UserService(NexaClient nexaClient) {
        this.nexaClient = nexaClient;
    }

    public String createUser(String name, String email) {
        Map<String, Object> user = Map.of("name", name, "email", email);
        Map<String, Object> result = nexaClient.create("users", user);
        return (String) result.get("document_id");
    }

    public List<Map<String, Object>> getAllUsers() {
        return nexaClient.query("users");
    }

    public Map<String, Object> getUserById(String id) {
        return nexaClient.get("users", id);
    }

    public void updateUser(String id, Map<String, Object> updates) {
        nexaClient.update("users", id, updates);
    }

    public void deleteUser(String id) {
        nexaClient.delete("users", id);
    }
}

4. REST Controller Example

import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/users")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping
    public Map<String, String> createUser(@RequestBody Map<String, String> request) {
        String id = userService.createUser(
            request.get("name"),
            request.get("email")
        );
        return Map.of("id", id);
    }

    @GetMapping
    public List<Map<String, Object>> listUsers() {
        return userService.getAllUsers();
    }

    @GetMapping("/{id}")
    public Map<String, Object> getUser(@PathVariable String id) {
        return userService.getUserById(id);
    }

    @PutMapping("/{id}")
    public void updateUser(@PathVariable String id, @RequestBody Map<String, Object> updates) {
        userService.updateUser(id, updates);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable String id) {
        userService.deleteUser(id);
    }
}

API Reference

CRUD Operations

// Create
Map<String, Object> result = client.create("users", Map.of("name", "Alice"));

// Read
Map<String, Object> user = client.get("users", "doc_id");

// Update
client.update("users", "doc_id", Map.of("age", 30));

// Delete
client.delete("users", "doc_id");

// Query with filters
List<Map<String, Object>> users = client.query("users",
    Map.of("age", Map.of("$gte", 25)), 10);

// Count
long count = client.count("users", Map.of("age", Map.of("$lt", 30)));

Query Operators

NexaDB supports MongoDB-style query operators:

// Greater than
Map.of("age", Map.of("$gt", 25))

// Greater than or equal
Map.of("age", Map.of("$gte", 25))

// Less than
Map.of("age", Map.of("$lt", 50))

// Less than or equal
Map.of("age", Map.of("$lte", 50))

// Equal
Map.of("status", "active")

// Not equal
Map.of("status", Map.of("$ne", "deleted"))

// In array
Map.of("role", Map.of("$in", List.of("admin", "user")))

// Not in array
Map.of("role", Map.of("$nin", List.of("guest", "banned")))

Vector Search

// Vector similarity search (for AI/ML embeddings)
double[] queryVector = new double[]{0.1, 0.2, 0.3, /* ... 768 dimensions */};

List<Map<String, Object>> results = client.vectorSearch("embeddings",
    queryVector, 10, 768);

for (Map<String, Object> result : results) {
    System.out.println("Similarity: " + result.get("similarity"));
    System.out.println("Document: " + result.get("document"));
}

Batch Operations

// Batch insert
List<Map<String, Object>> documents = List.of(
    Map.of("name", "Alice", "age", 28),
    Map.of("name", "Bob", "age", 32),
    Map.of("name", "Charlie", "age", 25)
);

Map<String, Object> result = client.batchWrite("users", documents);
System.out.println("Inserted: " + result.get("count"));

Collection Management

// List all collections
List<String> collections = client.listCollections();

// Drop collection
boolean dropped = client.dropCollection("old_data");

// Get collection stats
Map<String, Object> stats = client.getCollectionStats("users");

Change Streams (Real-Time Events)

// Watch for changes on a collection
new Thread(() -> {
    client.watch("orders", null, event -> {
        String operation = (String) event.get("operationType");
        Map<String, Object> document = (Map<String, Object>) event.get("fullDocument");

        System.out.println("Operation: " + operation);
        System.out.println("Document: " + document);

        if ("insert".equals(operation)) {
            // Handle new order
            processNewOrder(document);
        }
    });
}).start();

User Management

// Create user (admin only)
client.createUser("alice", "password123", "write");

// List users
List<Map<String, Object>> users = client.listUsers();

// Change password
client.changePassword("alice", "newpassword");

// Delete user
client.deleteUser("alice");

Configuration

Builder Pattern

For fine-grained control:

NexaClient client = new NexaClient.Builder()
    .host("localhost")
    .port(6970)
    .username("root")
    .password("nexadb123")
    .timeout(30000)  // 30 seconds
    .maxRetries(3)
    .build();

client.connect();

Connection Pooling

For production applications with high concurrency:

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.BasePooledObjectFactory;

// Implement a connection pool for NexaClient
// (Example implementation available in examples directory)

Error Handling

import com.nexadb.client.*;

try {
    client.create("users", Map.of("name", "Alice"));
} catch (ConnectionException e) {
    // Handle connection errors
    logger.error("Connection failed", e);
} catch (AuthenticationException e) {
    // Handle authentication errors
    logger.error("Authentication failed", e);
} catch (OperationException e) {
    // Handle operation errors
    logger.error("Operation failed", e);
} catch (NexaDBException e) {
    // Handle general NexaDB errors
    logger.error("NexaDB error", e);
}

Performance Tips

  1. Reuse Connections: NexaClient is thread-safe, reuse instances
  2. Batch Writes: Use batchWrite() for bulk inserts
  3. Binary Protocol: Already using MessagePack (10x faster than REST)
  4. Connection Pooling: For high-concurrency applications
  5. Appropriate Limits: Don't query more documents than needed

Requirements

  • Java 11 or higher
  • NexaDB Server 2.3.0+

License

MIT License

Support

About

Official Java client for NexaDB - The high-performance, easy-to-use database.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages