31 Ekim 2023 Salı

Hazelcast Jet Sources.mapJournal metodu

Örnek
Şöyle yaparız
IMap<Long, String> myMap = ...;
Pipeline p = Pipeline.create();
p.readFrom(Sources.mapJournal(myMap, START_FROM_CURRENT))
  .withoutTimestamps()
  .writeTo(Sinks.jdbc("%some update query%", () -> {
    BaseDataSource dataSource = new PGXADataSource();
    dataSource.setUrl("jdbc:postgresql://localhost:5432/my_db");
    dataSource.setUser("postgres");
    dataSource.setPassword("postgres");
    dataSource.setDatabaseName("my_db");
    return dataSource;
  }, (stmt, record) -> {
    // fill query params and execute                         
}));


Hazelcast Jet CDC CdcSinks Sınıfı

Giriş
Şu satırı dahil ederiz 
import com.hazelcast.jet.cdc.CdcSinks;
CDC ile yakalanan değişiklikleri bir sink nesnesine yazar

map metodu
Örnek
Şöyle yaparız
StreamSource<ChangeRecord> theDataBase = ...;,
IMap<Long, String> myMap = ...;

var pipeline = Pipeline.create();
pipeline.readFrom(theDataBase)
  .withoutTimestamps()
  .writeTo(CdcSinks.map(myMap, 
    record -> Long.parseLong(record.key().toMap().get("id").toString()), 
    record -> record.value().toJson()));
record nesnesi com.hazelcast.jet.cdc.ChangeRecord tipinden. key() metodu ile 
com.hazelcast.jet.cdc.RecordPart nesnesi elde edilir. toMap() çağrısı ile JSON nesnesi map haline getirilir


24 Ekim 2023 Salı

Hazelcast Jet QueryResultProducer Arayüzü

Giriş
Şu satırı dahil ederiz 
import com.hazelcast.jet.sql.impl.QueryResultProducer;
SQL içindeki LIMIT N şeklinde sonuçlar burada gerçekleşir

Hazelcast Jet PipelineImpl Sınıfı

Giriş
Şu satırı dahil ederiz 
import com.hazelcast.jet.impl.pipeline.PipelineImpl;
Pipeline arayüzünden kalıtır. readFrom(), writeTo() gibi işlemler adjacencyMap içindedir. Bu map içindeki nesneler Serializable olmak zorunda
Kod şöyle
public class PipelineImpl implements Pipeline {

  private static final long serialVersionUID = 1L;

  private final Map<Transform, List<Transform>> adjacencyMap = new LinkedHashMap<>();
  private final Map<String, File> attachedFiles = new HashMap<>();
  private boolean preserveOrder;
  ...
}


23 Ekim 2023 Pazartesi

Hazelcast Jet Sources.remoteMap

Giriş
İmzası şöyle
public static <K, V> BatchSource<Entry<K, V>> remoteMap(
  String mapName,
  ClientConfig clientConfig
)

public static <T, K, V> BatchSource<T> remoteMap(
  String mapName,
  ClientConfig clientConfig,
  Predicate<K, V> predicate,
  Projection<? super Entry<K, V>, ? extends T> projection
)

public static <K, V> BatchSource<Entry<K, V>> remoteMap(
  String mapName,
  DataConnectionRef dataConnectionRef
)

public static <T, K, V> BatchSource<T> remoteMap(
  String mapName,
  DataConnectionRef dataConnectionRef,
  Predicate<K, V> predicate,
  Projection<? super Entry<K, V>, ? extends T> projection
)
Altta şöyle yapar
1. RemoteMapQueryReaderFunction : ReadMapOrCacheP : com.hazelcast.jet.impl.connector.Reader ile okuma yapar

2. RemoteMapReaderFunction : RemoteMapReader : Entry<Data, Data> olarak okur

20 Ekim 2023 Cuma

Hazelcast Discovery SPI MemberAddressProvider Arayüzü

Giriş
Şu satırı dahil ederiz
import com.hazelcast.spi.MemberAddressProvider;
Açıklaması şöyle
This class tries to work out which interface to choose as the address of the member


12 Ekim 2023 Perşembe

hazelcast.xml NearCache Ayarları

Örnek
Şöyle yaparız
hazelcast-client:
  ...
  near-cache:
    stargatedemo-internal:
      in-memory-format: BINARY
      invalidate-on-change: true
      eviction:
        eviction-policy: LRU
        max-size-policy: ENTRY_COUNT
        size: 50000

HazelcastAPI ClientConfigXmlGenerator Sınıfı

Giriş
Şu satırı dahil ederiz
import com.hazelcast.client.config.ClientConfigXmlGenerator;
ClientConfig nesnesini XML'e çevirir

Örnek
Şöyle yaparız
ClientConfig config = ...;
String str = ClientConfigXmlGenerator.generate(config, 2);


try (InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))) {
  // CLASS LOADER IS SET by XmlClientConfigBuilder
  ClientConfig newConfig = new XmlClientConfigBuilder(is).build();
}

11 Ekim 2023 Çarşamba

HazelcastExecutor IExecutorService Arayüzü - Callable

Giriş
Şunlar kullanılabilir.
submitToAllMembers //Tüm üyelere gönder
submitToKeyOwner //Belirtilen key sahibi üyeye gönder
submitToMember //Belirtilen üyeye gönder
submitToMembers //Belirtilen üyelere gönder
İmzalar şöyle
Future<T> submit(Callable<T> task, MemberSelector memberSelector);
void submit(Callable<T> task,ExecutionCallback<T> callback);
void submit(Callable<T> task, MemberSelector memberSelector,
            ExecutionCallback<T> callback);

Future<T> submitToKeyOwner(Callable<T> task, Object key);
void submitToKeyOwner(Callable<T> task, Object key,
                      ExecutionCallback<T> callback);

Future<T> submitToMember(Callable<T> task, Member member);
void submitToMember(Callable<T> task, Member member,
                    ExecutionCallback<T> callback);

Map<Member, Future<T>> submitToMembers(Callable<T> task,
                                       Collection<Member> members);
Map<Member, Future<T>> submitToMembers(Callable<T> task,
                                       MemberSelector memberSelector);
void submitToMembers(Callable<T> task,Collection<Member> members,
                     MultiExecutionCallback callback);

void submitToMembers(Callable<T> task,
                     MemberSelector memberSelector,
                     MultiExecutionCallback callback);

Map<Member, Future<T>> submitToAllMembers(Callable<T> task);
void submitToAllMembers(Callable<T> task,
                        MultiExecutionCallback callback);

submit metodu
Örnek
Şöyle yaparız
public class DemoTask implements Callable<Map<String, Object>>, Serializable { 
  ... 
  @Override 
  public Map<String, Object> call() throws Exception { 
    ...
    Map<String, Object> result = new HashMap<>(); 
    result.put("...", "..."
    return result; 
  } 
} 

DemoTask task = new DemoTask(); 
executorService.submit(task, new ExecutionCallback<Map<String, Object>>() { 
  @Override
  public void onResponse(Map<String, Object> incomingResponse) { 
   ...
  } 
  @Override  
  public void onFailure(Throwable t) { 
    ...
  } 
});

HazelcastAPI ExtendedMapEntry Arayüzü

Giriş
Şu satırı dahil ederiz
import com.hazelcast.map.ExtendedMapEntry;
setValue metodu
Örnek
Şöyle yaparız
class IncrementWithOptionalTtl implements EntryProcessor<Integer, Integer, Void> {
  private final long ttlSeconds;
   
  public IncrementWithOptionalTtl(long ttlSeconds) {
    this.ttlSeconds = ttlSeconds;
  }
 
  @Override
  public Void process(Map.Entry<Integer, Integer> e) {
    ExtendedMapEntry<Integer, Integer> entry = (ExtendedMapEntry<Integer, Integer>) e;
    int newValue = entry.getValue() + 1;
    if (ttlSeconds > 0) {
      entry.setValue(newValue, ttlSeconds, TimeUnit.SECONDS);
    } else {
      entry.setValue(newValue);
    }
    return null;
  }
}
Örnek
Şöyle yaparız
public Object process(Entry entry) {
  Object oldValue = entry.getValue();
  ExtendedMapEntry extendedMapEntry = (com.hazelcast.map.ExtendedMapEntry) entry;
  extendedMapEntry.setValue(oldValue, 30, TimeUnit.SECONDS);
  return oldValue;
}


Hazelcast SQL GENERATE_STREAM + IMap JOIN

Örnek
Şöyle bir VIEW yaratırız.
CREATE OR REPLACE VIEW orders AS
SELECT id, CASE WHEN orderRand BETWEEN 0 AND 0.2 THEN '11001' WHEN orderRand BETWEEN 0.2 AND 0.4 THEN '11002' WHEN orderRand BETWEEN 0.4 AND 0.6 THEN '11003' WHEN orderRand BETWEEN 0.6 AND 0.8 THEN '11004' ELSE '11005' END as cust_id, CASE WHEN orderRand*1.5 BETWEEN 0 AND 0.25 THEN '501' WHEN orderRand*1.5 BETWEEN 0.25 AND 0.5 THEN '502' WHEN orderRand*1.5 BETWEEN 0.5 AND 0.75 THEN '503' ELSE '504' END as item_num, CASE WHEN orderRand*.66 BETWEEN 0 AND .25 THEN CAST(1 AS SMALLINT) WHEN orderRand*.66 BETWEEN .25 AND .5 THEN CAST(2 AS SMALLINT) ELSE CAST(3 AS SMALLINT) END as quantity, order_ts FROM (SELECT v as id, RAND(v*v*v) as orderRand, TO_TIMESTAMP_TZ(v*10 + 1645484400000) as order_ts FROM TABLE(generate_stream(7)));
Şöyle bir customers IMap yaratırız ve doldururuz
CREATE or REPLACE MAPPING customers (
     __key BIGINT,
     cust_id VARCHAR,
     last_name VARCHAR,
     first_name VARCHAR,
     address1 VARCHAR,
     address2 VARCHAR,
     phone VARCHAR )
TYPE IMap
OPTIONS (
'keyFormat'='bigint',
'valueFormat'='json-flat');

// Populate the customers map with data.

SINK INTO customers VALUES
(1,'11001', 'Smith', 'John', '123 Main St', 'Ames, IA 50012', '515-555-1212'),
(2,'11002', 'Li', 'Guo', '456 Powell St', 'San Francisco, CA 94108', '415-555-1212'),
(3,'11003', 'Ivanov', 'Sergei', '999 Brighton Blvd', 'New York, NY 11235','212-555-1212'),
(4,'11004', 'Mohammed', 'Ibrahim', '42 Elm St', 'Dearborn, MI 48126', '313-555-1212'),
(5,'11005', 'Patel', 'Ram', '5151 Market St', 'Trenton, NJ 08615', '609-555-1212');
Şöyle bir inventory IMap yaratırız ve doldururuz
CREATE or REPLACE MAPPING inventory (
     __key BIGINT,
     item_num VARCHAR,
     unit_price DECIMAL,
     quantity SMALLINT)
TYPE IMap
OPTIONS (
'keyFormat'='bigint',
'valueFormat'='json-flat');
SQL
Populate the inventory database.

SINK INTO inventory VALUES
(1, '501', 1.99, 500),
(2, '502', 3.99, 500),
(3, '503', 5.99, 500),
(4, '504', 7.99, 500);
 Hedef IMap'i yaratırız
CREATE OR REPLACE MAPPING PickOrder (
   __key BIGINT,
   ts TIMESTAMP,
  item_num VARCHAR,
  quantity SMALLINT,
  cust_id VARCHAR,
  last_name VARCHAR,
  first_name VARCHAR,
  address1 VARCHAR,
  address2 VARCHAR,
  phone VARCHAR)
TYPE IMap
OPTIONS (
'keyFormat'='bigint',
'valueFormat'='json-flat');
Bunu dolduracak iş için şöyle yaparız. Gelen order ile ilişkili customer ve inventory bulunur
CREATE JOB PickOrder AS
SINK INTO PickOrder
     SELECT
          ord.id AS __key,
          ord.order_ts AS ts,
          ord.item_num AS item_num,
          ord.quantity AS quantity,
          ord.cust_id AS cust_id,
          cust.last_name AS last_name,
          cust.first_name AS first_name,
          cust.address1 AS address1,
          cust.address2 AS address2,
          cust.phone AS phone
     FROM orders AS ord
     JOIN customers AS cust ON ord.cust_id = cust.cust_id
     JOIN inventory ON ord.item_num = inventory.item_num
     WHERE ord.quantity < inventory.quantity;



HazelcastAPI DefaultWriteBehindProcessor Sınıfı

Giriş
Şu satırı dahil ederiz
import com.hazelcast.map.impl.mapstore.DefaultWriteBehindProcessor;
Kalıtım şöyle
WriteBehindProcessor
  AbstractWriteBehindProcessor
    DefaultWriteBehindProcessor

Bu sınıfın etkin olması için şöyle yaparız. Yani setWriteDelaySeconds() çağrısı gerekir
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig
  .setWriteCoalescing (true)
  .setImplementation(myMapStore)
  .setWriteDelaySeconds(10) 
  .setWriteBatchSize(1_000)

config.getMapConfig(...).setMapStoreConfig(mapStoreConfig)
process metodu
İmzası şöyle. StoreWorker tarafından çağrılır
@Override
public Map<Integer, List<DelayedEntry>> process(List<DelayedEntry> delayedEntries)



10 Ekim 2023 Salı

HazelcastExecutor DurableExecutorService Arayüzü

Giriş
Şu satırı dahil ederiz
import com.hazelcast.durableexecutor.DurableExecutorService;
Eğer durability atandıysa, member kapanırsa bile çalışmakta olan edilen iş kaybolmaz. Başka bir member'da tekrar başlatılır

Örnek
Şöyle yaparız
Config config = new Config();
config.getDurableExecutorConfig("MyService").setDurability(2).setCapacity(200).setPoolSize(2);

HazelcastInstance hc = Hazelcast.newHazelcastInstance(config);
hc.getMap("MyMap").put("Key-1", "Value-1");

DurableExecutorService service = hc.getDurableExecutorService("MyService");
service.executeOnKeyOwner(new MyRunnable(), "Key-1"); com.hazelcast.durableexecutor.DurableExecutorService;aa. aa


hazelcast.xml - CP Subsytem Ayarları

Örnek
Şöyle yaparız
<hazelcast>
  <cp-subsystem>
   <!-- set cp-member-count with value 0 to run in unsafe mode -->
    <cp-member-count>3</cp-member-count>
    <!-- configuration options here -->
  </cp-subsystem>
</hazelcast>


7 Ekim 2023 Cumartesi

HazelcastAPI Max Idle Seconds ve TTL

Max Idle Seconds - Belli Bir Süre Kullanılmazsa
Açıklaması şöyle
Max Idle time specifies the maximum time in seconds for an entry to stay idle in the map before it is evicted. An entry is considered idle if no get, put, or containsKey operation is performed on it.
Belli bir süre kullanılmayan nesneleri siler
1. MapConfig#setsetMaxIdleSeconds() metodu sağlar
2 . NearCacheConfig#setsetMaxIdleSeconds() metodu sağlar

TTL - Güncellemeden Sonraki En Fazla Süre
Açıklaması şöyle
TTL, or Time To Live, specifies the maximum lifespan of an entry from the moment it is created or updated. It controls how long an entry can exist in the map regardless of whether it's actively being used or not.
Session verisi gibi şeylerde işe yarar. Açıklaması şöyle. 
 Suppose you have an IMap for caching user sessions:
- If you set a Max Idle Time of 30 minutes, it means that if a user's session is not accessed (no read or update operations) for 30 minutes, the session entry will be automatically removed from the map, even if the user is still logged in.
- If you set a TTL of 1 hour for the same session entries, it means that regardless of whether the user is actively using the session or not, it will be automatically removed from the map after 1 hour from the time of creation.
1. Eğer kullanıcı 30 dakika etkin değilse Session silinir. Böylece bellekten tasarruf edilir.
2. Eğer kullanıcı etkin olsa bile 1 saat sonra Session silinir.  Böylece kullanıcı tekrar giriş yapmak zorunda. 

5 Ekim 2023 Perşembe

HazelcastSpringBoot Session Kullanımı

Giriş
Bir soru burada

Map İsmi
Map ismi şöyle
spring:session:sessions

Map yapısı 
Şöyle
key : sessionid
value : MapSession


4 Ekim 2023 Çarşamba

ConsoleApp

Giriş
Açıklaması şöyle
The console tool enables you execute commands on the cluster. You can read or write on instances but first you should set namespace. For example if you have a map with name "mapCustomers". To get a customer with key "Jack" you should first set the namespace with command "ns mapCustomers". Then you can take the object by "m.get Jack"
Map Komutları
Açıklaması şöyle
m.put <key> <value>                  //puts an entry to the map
m.remove <key>                       //removes the entry of given key from the map
m.get <key>                          //returns the value of given key from the map
m.putmany <number> [<size>] [<index>]//puts indicated number of entries to the map ('key<i>':byte[<size>], <index>+(0..<number>)
m.removemany <number> [<index>]      //removes indicated number of entries from the map ('key<i>', <index>+(0..<number>)
     When using &x with m.putmany and m.removemany, each thread will get a different share of keys unless a start key <index> is specified
m.keys                               //iterates the keys of the map
m.values                             //iterates the values of the map
m.entries                            //iterates the entries of the map
m.iterator [remove]                  //iterates the keys of the map, remove if specified
m.size                               //size of the map
m.clear                              //clears the map
m.destroy                            //destroys the map
m.lock <key>                         //locks the key
m.tryLock <key>                      //tries to lock the key and returns immediately
m.tryLock <key> <time>               //tries to lock the key within given seconds
m.unlock <key>                       //unlocks the key
clear 
Örnek
Şöyle yaparız
default> ns spring:session:sessions
spring:session:sessions> m.clear
Örnek
Şöyle yapabiliriz
m.entries
m.size
Örnek
Şöyle yaparız
> ns my-distributed-map

m.put "1" "John"

m.put "2" "Mary"

m.put "3" "Jane"



HazelcastClientAPI EvictionConfig Sınıfı

Giriş
Şu satırı dahil ederiz
import com.hazelcast.config.EvictionConfig;
EvictionConfig Nedir?
Bir veri yapısına üst sınır koymak içindir. Veri yapısı üst sınıra erişince hangi nesnelerin çıkarılacağını belirtir. Eğer EvictionConfig tanımlı değilse veri yapısı çok büyüyebilir

setMaxSizePolicy
setSize ile belirtilen değere ulaşınca kullanılacak eviction yöntemini belirtir

Örnek - ENTRY_COUNT
Şöyle yaparız. Veri yapısının eleman sayısı 100'ü geçince eviction başlar.
EvictionConfig evictionConfig = new EvictionConfig()
  .setMaxSizePolicy(MaxSizePolicy.ENTRY_COUNT)
  .setEvictionPolicy(EvictionPolicy.LRU)
  .setSize(100);
Not : IMap için kullanılamaz. Hata şöyle
IMap eviction config doesn't support max size policy `ENTRY_COUNT`.

Örnek - USED_HEAP_PERCENTAGE
Şöyle yaparız. Veri yapısı Heap'in yüzde 10'unu geçince eviction başlar.
EvictionConfig evictionConfig = new EvictionConfig()
  .setMaxSizePolicy(MaxSizePolicy.USED_HEAP_PERCENTAGE)
  .setEvictionPolicy(EvictionPolicy.LRU)
  .setSize(10);

THIRD-PARTY.txt Dosyası

Kullanılan harici kütüphanelerin sürümleri bu dosyada Dosyanın yolu şöyle hazelcast/licenses/THIRD-PARTY.txt