Search in sources :

Example 66 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class ClientKeyValueBinaryViewTest method testGetAndPut.

@Test
public void testGetAndPut() {
    KeyValueView<Tuple, Tuple> kvView = defaultTable().keyValueView();
    kvView.put(null, tupleKey(1L), tupleVal("1"));
    Tuple res1 = kvView.getAndPut(null, tupleKey(2L), tupleVal("2"));
    Tuple res2 = kvView.getAndPut(null, tupleKey(1L), tupleVal("3"));
    assertNull(res1);
    assertEquals("2", kvView.get(null, tupleKey(2L)).stringValue(0));
    assertNotNull(res2);
    assertEquals("1", res2.stringValue(0));
    assertEquals("3", kvView.get(null, tupleKey(1L)).stringValue(0));
}
Also used : Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 67 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class ClientKeyValueBinaryViewTest method testGetMissingRowReturnsNull.

@Test
public void testGetMissingRowReturnsNull() {
    Table table = defaultTable();
    KeyValueView<Tuple, Tuple> kvView = table.keyValueView();
    assertNull(kvView.get(null, defaultTupleKey()));
}
Also used : Table(org.apache.ignite.table.Table) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 68 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class ClientTableTest method testUpsertGet.

@Test
public void testUpsertGet() {
    var table = defaultTable().recordView();
    var tuple = tuple();
    table.upsert(null, tuple);
    Tuple key = tuple(DEFAULT_ID);
    var resTuple = table.get(null, key);
    assertEquals(DEFAULT_NAME, resTuple.stringValue("name"));
    assertEquals(DEFAULT_ID, resTuple.longValue("id"));
    assertEquals("foo", resTuple.valueOrDefault("bar", "foo"));
    assertEquals(DEFAULT_NAME, resTuple.value(1));
    assertEquals(DEFAULT_ID, resTuple.value(0));
    assertEquals(2, resTuple.columnCount());
    assertEquals("ID", resTuple.columnName(0));
    assertEquals("NAME", resTuple.columnName(1));
    var iter = tuple.iterator();
    assertTrue(iter.hasNext());
    assertEquals(DEFAULT_ID, iter.next());
    assertTrue(iter.hasNext());
    assertEquals(DEFAULT_NAME, iter.next());
    assertFalse(iter.hasNext());
    assertNull(iter.next());
    assertTupleEquals(tuple, resTuple);
}
Also used : Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 69 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class KeyValueViewExample method main.

/**
 * Main method of the example.
 *
 * @param args The command line arguments.
 * @throws Exception If failed.
 */
public static void main(String[] args) throws Exception {
    try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800/");
        Statement stmt = conn.createStatement()) {
        stmt.executeUpdate("CREATE TABLE accounts (" + "accountNumber INT PRIMARY KEY," + "firstName     VARCHAR," + "lastName      VARCHAR," + "balance       DOUBLE)");
    }
    // --------------------------------------------------------------------------------------
    // 
    // Creating a client to connect to the cluster.
    // 
    // --------------------------------------------------------------------------------------
    System.out.println("\nConnecting to server...");
    try (IgniteClient client = IgniteClient.builder().addresses("127.0.0.1:10800").build()) {
        // --------------------------------------------------------------------------------------
        // 
        // Creating a key-value view for the 'accounts' table.
        // 
        // --------------------------------------------------------------------------------------
        KeyValueView<Tuple, Tuple> kvView = client.tables().table("PUBLIC.accounts").keyValueView();
        // --------------------------------------------------------------------------------------
        // 
        // Performing the 'put' operation.
        // 
        // --------------------------------------------------------------------------------------
        System.out.println("\nInserting a key-value pair into the 'accounts' table...");
        Tuple key = Tuple.create().set("accountNumber", 123456);
        Tuple value = Tuple.create().set("firstName", "Val").set("lastName", "Kulichenko").set("balance", 100.00d);
        kvView.put(null, key, value);
        // --------------------------------------------------------------------------------------
        // 
        // Performing the 'get' operation.
        // 
        // --------------------------------------------------------------------------------------
        System.out.println("\nRetrieving a value using KeyValueView API...");
        value = kvView.get(null, key);
        System.out.println("\nRetrieved value:\n" + "    Account Number: " + key.intValue("accountNumber") + '\n' + "    Owner: " + value.stringValue("firstName") + " " + value.stringValue("lastName") + '\n' + "    Balance: $" + value.doubleValue("balance"));
    } finally {
        System.out.println("\nDropping the table...");
        try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800/");
            Statement stmt = conn.createStatement()) {
            stmt.executeUpdate("DROP TABLE accounts");
        }
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) Statement(java.sql.Statement) Connection(java.sql.Connection) Tuple(org.apache.ignite.table.Tuple)

Example 70 with Tuple

use of org.apache.ignite.table.Tuple in project ignite-3 by apache.

the class RebalanceExample method main.

/**
 * Main method of the example.
 *
 * @param args The command line arguments.
 * @throws Exception If failed.
 */
public static void main(String[] args) throws Exception {
    try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800/");
        Statement stmt = conn.createStatement()) {
        stmt.executeUpdate("CREATE TABLE rebalance (" + "key   INT PRIMARY KEY," + "value VARCHAR)");
    }
    // --------------------------------------------------------------------------------------
    // 
    // Creating a client to connect to the cluster.
    // 
    // --------------------------------------------------------------------------------------
    System.out.println("\nConnecting to server...");
    try (IgniteClient client = IgniteClient.builder().addresses("127.0.0.1:10800").build()) {
        KeyValueView<Tuple, Tuple> kvView = client.tables().table("PUBLIC.rebalance").keyValueView();
        // --------------------------------------------------------------------------------------
        // 
        // Inserting several key-value pairs into the table.
        // 
        // --------------------------------------------------------------------------------------
        System.out.println("\nInserting key-value pairs...");
        for (int i = 0; i < 10; i++) {
            Tuple key = Tuple.create().set("key", i);
            Tuple value = Tuple.create().set("value", "test_" + i);
            kvView.put(null, key, value);
        }
        // --------------------------------------------------------------------------------------
        // 
        // Retrieving the newly inserted data.
        // 
        // --------------------------------------------------------------------------------------
        System.out.println("\nRetrieved key-value pairs:");
        for (int i = 0; i < 10; i++) {
            Tuple key = Tuple.create().set("key", i);
            Tuple value = kvView.get(null, key);
            System.out.println("    " + i + " -> " + value.stringValue("value"));
        }
        // --------------------------------------------------------------------------------------
        // 
        // Scaling out by adding two more nodes into the topology.
        // 
        // --------------------------------------------------------------------------------------
        System.out.println("\n" + "Run the following commands using the CLI tool to start two more nodes, and then press 'Enter' to continue...\n" + "    ignite node start --config=examples/config/ignite-config.json my-first-additional-node\n" + "    ignite node start --config=examples/config/ignite-config.json my-second-additional-node");
        System.in.read();
        // --------------------------------------------------------------------------------------
        // 
        // Updating baseline to initiate the data rebalancing process.
        // 
        // New topology includes the following five nodes:
        // 1. 'my-first-node' -- the first node started prior to running the example
        // 2. 'my-second-node' -- the second node started prior to running the example
        // 3. 'additional-node-1' -- the first node added to the topology
        // 4. 'additional-node-2' -- the second node added to the topology
        // 5. 'example-node' -- node that is embedded into the example
        // 
        // NOTE: An embedded server node is started here for the sole purpose of setting
        // the baseline. In the future releases, this API will be provided by the
        // clients as well. In addition, the process will be automated where applicable
        // to eliminate the need for this manual step.
        // 
        // --------------------------------------------------------------------------------------
        System.out.println("Starting a server node... Logging to file: example-node.log");
        System.setProperty("java.util.logging.config.file", "config/java.util.logging.properties");
        try (Ignite server = IgnitionManager.start("example-node", Files.readString(Path.of("config", "ignite-config.json")), Path.of("work"))) {
            System.out.println("\nUpdating the baseline and rebalancing the data...");
            server.setBaseline(Set.of("my-first-node", "my-second-node", "my-first-additional-node", "my-second-additional-node", "example-node"));
            // --------------------------------------------------------------------------------------
            // 
            // Retrieving data again to validate correctness.
            // 
            // --------------------------------------------------------------------------------------
            System.out.println("\nKey-value pairs retrieved after the topology change:");
            for (int i = 0; i < 10; i++) {
                Tuple key = Tuple.create().set("key", i);
                Tuple value = kvView.get(null, key);
                System.out.println("    " + i + " -> " + value.stringValue("value"));
            }
        }
    }
    System.out.println("\nDropping the table...");
    try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800/");
        Statement stmt = conn.createStatement()) {
        stmt.executeUpdate("DROP TABLE rebalance");
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) Statement(java.sql.Statement) Connection(java.sql.Connection) Ignite(org.apache.ignite.Ignite) Tuple(org.apache.ignite.table.Tuple)

Aggregations

Tuple (org.apache.ignite.table.Tuple)130 Test (org.junit.jupiter.api.Test)101 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)33 Table (org.apache.ignite.table.Table)27 Column (org.apache.ignite.internal.schema.Column)25 Row (org.apache.ignite.internal.schema.row.Row)21 IgniteAbstractTest (org.apache.ignite.internal.testframework.IgniteAbstractTest)19 Ignite (org.apache.ignite.Ignite)17 TupleMarshaller (org.apache.ignite.internal.schema.marshaller.TupleMarshaller)17 TupleMarshallerImpl (org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl)17 DummySchemaManagerImpl (org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)17 InternalTransaction (org.apache.ignite.internal.tx.InternalTransaction)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 ByteBufferRow (org.apache.ignite.internal.schema.ByteBufferRow)10 TableDefinition (org.apache.ignite.schema.definition.TableDefinition)9 ArrayList (java.util.ArrayList)8 Disabled (org.junit.jupiter.api.Disabled)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 BigDecimal (java.math.BigDecimal)6 Transaction (org.apache.ignite.tx.Transaction)6