zoukankan      html  css  js  c++  java
  • Apache Ignite Java瘦客户端入门使用-键值/SQL存储

    简介

    Apache Ignite是一个水平可扩展以及容错的分布式SQL数据库,分布式数据是以复制或者分区的形式提供的。

    作为一个SQL数据库,Ignite支持所有的DML指令,包括SELECT、UPDATE、INSERT和DELETE,它还实现了一个与分布式系统有关的DDL指令的子集。

    可以像其它的SQL存储一样,根据需要与Ignite进行交互,比如通过外部的工具或者应用使用JDBC或者ODBC驱动进行连接。
    在这之上,Java、.NET和C++开发者也可以使用Ignite的原生SQL API。

    Apache Ignite中文文档

    Apache Ignite英文文档

    Maven

    版本要跟集群版本一致

            <dependency>
                <groupId>org.apache.ignite</groupId>
                <artifactId>ignite-core</artifactId>
                <version>2.8.0</version>
            </dependency>
    

    键值存储

        public void kv() {
            //第一种
            // Ignite Addr.
            ClientConfiguration cfg = new ClientConfiguration().setAddresses("127.0.0.1:10800");
            try (IgniteClient client = Ignition.startClient(cfg)) {
                //ClientCache<String, String> cache = client.getOrCreateCache("myCache");
                ClientCache<String, String> cache = client.cache("myCache");//获取已存在的cache
                // Get data from the cache
                cache.put("key", "value");
                final String value = cache.get("key");
                cache.remove("key");
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            //第二种,自定义策略
            ClientCacheConfiguration cacheCfg = new ClientCacheConfiguration();
            cacheCfg.setName("myCache");
            cacheCfg.setOnheapCacheEnabled(true);
            cacheCfg.setBackups(1); //备份数1
            cacheCfg.setCacheMode(CacheMode.PARTITIONED); //集群数据采用分区模式
            cacheCfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
            cacheCfg.setPartitionLossPolicy(PartitionLossPolicy.READ_WRITE_SAFE);
            cacheCfg.setDataRegionName("Default_Region"); //使用默认Region
            // Ignite Addr.
            ClientConfiguration cfg2 = new ClientConfiguration().setAddresses("127.0.0.1:10800");
            try (IgniteClient client = Ignition.startClient(cfg2)) {
                final ClientCache<String, String> cache = client.getOrCreateCache(cacheCfg);
                // Get data from the cache
                cache.put("key", "value");
                final String value = cache.get("key");
                cache.remove("key");
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    

    SQL存储

    策略参考:https://www.cnblogs.com/peppapigdaddy/p/10997068.html

         public void sql() {
            ClientCacheConfiguration cacheCfg2 = new ClientCacheConfiguration();
            cacheCfg2.setName("mySqlCacheName"); //缓存名
            cacheCfg2.setOnheapCacheEnabled(true);
            cacheCfg2.setBackups(1);
            cacheCfg2.setCacheMode(CacheMode.PARTITIONED);
            cacheCfg2.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
            cacheCfg2.setPartitionLossPolicy(PartitionLossPolicy.READ_ONLY_SAFE);
            cacheCfg2.setDataRegionName("Default_Region");
            cacheCfg2.setSqlSchema("mySqlSchemaName"); //相当于Database name, 可设置与缓存名一致
            // Ignite Addr.
            ClientConfiguration cfg2 = new ClientConfiguration().setAddresses("127.0.0.1:10800");
            try (IgniteClient client = Ignition.startClient(cfg2)) {
                final ClientCache<String, String> cache = client.getOrCreateCache(cacheCfg2);
                //建表
                List<List<?>> result = cache.query(new SqlFieldsQuery("" +
                        "CREATE TABLE IF NOT EXISTS  TEST_TABLE (" +
                        "  `id` varchar(40)," +
                        "  `name` varchar(64)," +
                        "  `age` varchar(32)," +
                        "  `creator` varchar(15)," +
                        "  `create_time` timestamp," +
                        "  PRIMARY KEY (`id`)" +
                        ") WITH "TEMPLATE=cacheTemplate"")).getAll();
    
                //INSERT
                String insert = "INSERT INTO TEST_TABLE (id, name, age, creator, create_time) " +
                        "VALUES(?, ?, ?, ?, ?)";
                List<List<?>> res = cache.query(new SqlFieldsQuery(insert).setArgs("id", "name", "17", "levi", new Date())).getAll();
                //SELECT
                final List<List<?>> all = cache.query(new SqlFieldsQuery("select count(1) from TEST_TABLE")).getAll();
                System.out.println(all.get(0).get(0));
                //UPDATE、 DELETE 同理
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    
  • 相关阅读:
    elasticsearch操作命令
    mysql-清除binlog日志命令
    TensorFlow 基础 (03)
    TensorFlow 基础 (02)
    TensorFlow 基础 (01)
    Python 基础练手 32 道
    Pandas 批量处理文本表
    Pandas 清除 Excel 特殊字符
    MNIST实例-Tensorflow 初体验
    字符串和文本 (02)
  • 原文地址:https://www.cnblogs.com/levi125/p/14011654.html
Copyright © 2011-2022 走看看