Search in sources :

Example 56 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class ClientKubernetesPutGetExample method main.

/**
 * Entry point.
 *
 * @param args Command line arguments.
 */
public static void main(String[] args) {
    KubernetesConnectionConfiguration kcfg = new KubernetesConnectionConfiguration();
    kcfg.setNamespace("ignite");
    ClientConfiguration cfg = new ClientConfiguration();
    cfg.setAddressesFinder(new ThinClientKubernetesAddressFinder(kcfg));
    try (IgniteClient igniteClient = Ignition.startClient(cfg)) {
        System.out.println();
        System.out.println(">>> Thin client put-get example started.");
        final String CACHE_NAME = "put-get-example";
        ClientCache<Integer, Address> cache = igniteClient.getOrCreateCache(CACHE_NAME);
        System.out.format(">>> Created cache [%s].\n", CACHE_NAME);
        Integer key = 1;
        Address val = new Address("1545 Jackson Street", 94612);
        cache.put(key, val);
        System.out.format(">>> Saved [%s] in the cache.\n", val);
        Address cachedVal = cache.get(key);
        System.out.format(">>> Loaded [%s] from the cache.\n", cachedVal);
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) Address(org.apache.ignite.examples.model.Address) KubernetesConnectionConfiguration(org.apache.ignite.kubernetes.configuration.KubernetesConnectionConfiguration) ThinClientKubernetesAddressFinder(org.apache.ignite.client.ThinClientKubernetesAddressFinder) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration)

Example 57 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class ClientSizeCacheCreationDestructionTest method createCache.

/**
 * Create cache with specified configuration through thin/thick client or jdbc thin.
 *
 * @param node Cluster node or jdbc connection.
 * @param cacheCfg Cache or ClientCache configuration
 * @throws SQLException If failed to create cache through Jdbc Thin connection.
 */
private void createCache(AutoCloseable node, Serializable cacheCfg) throws SQLException {
    if (node instanceof IgniteClient)
        ((IgniteClient) node).createCache((ClientCacheConfiguration) cacheCfg);
    else if (node instanceof Ignite)
        ((Ignite) node).createCache((CacheConfiguration) cacheCfg);
    else if (node instanceof JdbcThinConnection) {
        CacheConfiguration jdbcCacheCfg = (CacheConfiguration) cacheCfg;
        srv.addCacheConfiguration(jdbcCacheCfg);
        try (Statement stmt = jdbcConn.createStatement()) {
            stmt.execute("CREATE TABLE " + jdbcCacheCfg.getName() + " (id int, name varchar, primary key (id)) WITH \"template=" + jdbcCacheCfg.getName() + "\"");
        }
    } else
        fail(" Unexpected node/client type");
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) Statement(java.sql.Statement) Ignite(org.apache.ignite.Ignite) JdbcThinConnection(org.apache.ignite.internal.jdbc.thin.JdbcThinConnection) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration)

Example 58 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class NodeSslConnectionMetricTest method testClientConnector.

/**
 * Tests SSL metrics produced by thin client connection.
 */
@Test
public void testClientConnector() throws Exception {
    MetricRegistry reg = mreg(startClusterNode(0), CLIENT_CONNECTOR_METRICS);
    assertEquals(0, reg.<LongMetric>findMetric(SENT_BYTES_METRIC_NAME).value());
    assertEquals(0, reg.<LongMetric>findMetric(RECEIVED_BYTES_METRIC_NAME).value());
    try (IgniteClient ignored = startClient(clientConfiguration("thinClient", "trusttwo", CIPHER_SUITE, "TLSv1.2"))) {
        checkSslCommunicationMetrics(reg, 1, 1, 0);
    }
    assertTrue(reg.<LongMetric>findMetric(SENT_BYTES_METRIC_NAME).value() > 0);
    assertTrue(reg.<LongMetric>findMetric(RECEIVED_BYTES_METRIC_NAME).value() > 0);
    checkSslCommunicationMetrics(reg, 1, 0, 0);
    // Tests untrusted certificate.
    assertThrowsWithCause(() -> startClient(clientConfiguration("client", "trustboth", CIPHER_SUITE, "TLSv1.2")), ClientConnectionException.class);
    checkSslCommunicationMetrics(reg, 2, 0, 1);
    // Tests unsupported cipher suites.
    assertThrowsWithCause(() -> startClient(clientConfiguration("thinClient", "trusttwo", UNSUPPORTED_CIPHER_SUITE, "TLSv1.2")), ClientConnectionException.class);
    checkSslCommunicationMetrics(reg, 3, 0, 2);
    // Tests mismatched protocol versions.
    assertThrowsWithCause(() -> startClient(clientConfiguration("thinClient", "trusttwo", null, "TLSv1.1")), ClientConnectionException.class);
    checkSslCommunicationMetrics(reg, 4, 0, 3);
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) LongMetric(org.apache.ignite.spi.metric.LongMetric) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 59 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class RunningQueryInfoCheckInitiatorTest method testThinClientInitiatorId.

/**
 * @throws Exception If failed.
 */
@Test
public void testThinClientInitiatorId() throws Exception {
    Consumer<String> sqlExec = sql -> {
        GridTestUtils.runAsync(() -> {
            try (IgniteClient cli = Ignition.startClient(new ClientConfiguration().setAddresses("127.0.0.1:" + clientPort(grid(0))).setUserName("ignite").setUserPassword("ignite"))) {
                cli.query(new SqlFieldsQuery(sql)).getAll();
            } catch (Exception e) {
                log.error("Unexpected exception", e);
            }
        });
    };
    Consumer<String> initiatorChecker = initiatorId -> {
        assertTrue("Invalid initiator ID: " + initiatorId, Pattern.compile("cli:127\\.0\\.0\\.1:[0-9]+@ignite").matcher(initiatorId).matches());
    };
    check(sqlExec, initiatorChecker);
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Connection(java.sql.Connection) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteEx(org.apache.ignite.internal.IgniteEx) QuerySqlFunction(org.apache.ignite.cache.query.annotations.QuerySqlFunction) SQLException(java.sql.SQLException) IgniteClient(org.apache.ignite.client.IgniteClient) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) JdbcThinAbstractSelfTest(org.apache.ignite.jdbc.thin.JdbcThinAbstractSelfTest) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Test(org.junit.Test) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Consumer(java.util.function.Consumer) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) CFG_URL_PREFIX(org.apache.ignite.IgniteJdbcDriver.CFG_URL_PREFIX) Phaser(java.util.concurrent.Phaser) Statement(java.sql.Statement) Pattern(java.util.regex.Pattern) DriverManager(java.sql.DriverManager) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) IgniteClient(org.apache.ignite.client.IgniteClient) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SQLException(java.sql.SQLException) JdbcThinAbstractSelfTest(org.apache.ignite.jdbc.thin.JdbcThinAbstractSelfTest) Test(org.junit.Test)

Example 60 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class JavaThinClient method clientAddressFinder.

void clientAddressFinder() throws Exception {
    // tag::client-address-finder[]
    ClientAddressFinder finder = () -> {
        String[] dynamicServerAddresses = fetchServerAddresses();
        return dynamicServerAddresses;
    };
    ClientConfiguration cfg = new ClientConfiguration().setAddressesFinder(finder).setPartitionAwarenessEnabled(true);
    try (IgniteClient client = Ignition.startClient(cfg)) {
        ClientCache<Integer, String> cache = client.cache("myCache");
    // Put, get, or remove data from the cache...
    } catch (ClientException e) {
        System.err.println(e.getMessage());
    }
// end::client-address-finder[]
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) ClientAddressFinder(org.apache.ignite.client.ClientAddressFinder) ClientException(org.apache.ignite.client.ClientException) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration)

Aggregations

IgniteClient (org.apache.ignite.client.IgniteClient)106 Test (org.junit.Test)76 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)43 ThinClientConfiguration (org.apache.ignite.configuration.ThinClientConfiguration)26 UUID (java.util.UUID)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 Ignite (org.apache.ignite.Ignite)14 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)14 HashSet (java.util.HashSet)13 T2 (org.apache.ignite.internal.util.typedef.T2)13 Set (java.util.Set)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)12 List (java.util.List)10 BinaryObject (org.apache.ignite.binary.BinaryObject)10 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)10 GridTestUtils.assertThrowsWithCause (org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause)10 Map (java.util.Map)9 Duration (javax.cache.expiry.Duration)9 ClientCacheConfiguration (org.apache.ignite.client.ClientCacheConfiguration)9