@SuppressWarnings("unchecked") private static <T, K> ProcessorSupplier distinctP(DistributedFunction<? super T, ? extends K> keyFn) { return filterUsingContextP(ContextFactory.withCreateFn(jet -> new HashSet<>()), (seenItems, item) -> seenItems.add(keyFn.apply((T) item))); } }
@Nonnull private static ContextFactory<SentimentAnalyzer> sentimentAnalyzerContext() { return ContextFactory .withCreateFn(jet -> new SentimentAnalyzer()) .shareLocally(); }
/** * Loads the pre-trained model from the specified path * * @param modelPath path of the model */ private static ContextFactory<ImageClassifierVggCifar10> classifierContext(String modelPath) { return ContextFactory.withCreateFn(jet -> { ImageClassifierVggCifar10 classifier = new ImageClassifierVggCifar10(); classifier.loadModel(new File(modelPath)); return classifier; }); } }
/** * Returns a factory that provides an {@link IMapJet} as the context. This * is useful if you are enriching an event stream with the data stored in * the Hazelcast Jet cluster. Unlike in a {@code hashJoin} transformation, * the data in the map can change while the job is running so you can keep * the enriching dataset up-to-date. * <p> * Instead of using this factory, you can call {@link * GeneralStage#mapUsingIMapAsync(IMap, DistributedBiFunction)} or {@link * GeneralStageWithKey#mapUsingIMapAsync(IMap, DistributedBiFunction)}. * <p> * If you plan to use a sync method on the map, call {@link * ContextFactory#nonCooperative()} on the returned factory. * * @param mapName name of the map used as context * @param <K> key type * @param <V> value type * @return the context factory */ @Nonnull public static <K, V> ContextFactory<IMapJet<K, V>> iMapContext(@Nonnull String mapName) { return ContextFactory .withCreateFn(jet -> jet.<K, V>getMap(mapName)) .shareLocally(); }
/** * Returns a factory that provides a {@link ReplicatedMap} as the context * object. A replicated map is a particularly good choice if you are * enriching an event stream with the data stored in the Hazelcast Jet * cluster. Unlike in a {@code hashJoin} transformation, the data in the * map can change while the job is running so you can keep the enriching * dataset up-to-date. Unlike {@code IMap}, the data you access is local so * you won't do any blocking calls using it (important for performance). * <p> * If you want to destroy the map after the job finishes, call * {@code factory.destroyFn(ReplicatedMap::destroy)} on the object you get * from this method. * <p> * Example usage (without destroyFn): * <pre> * p.drawFrom( /* a batch or streaming source */ ) * .mapUsingContext(replicatedMapContext("fooMapName"), * (map, item) -> tuple2(item, map.get(item.getKey()))) * .destroyFn(ReplicatedMap::destroy); * </pre> * * @param mapName name of the {@link ReplicatedMap} to use as the context * @param <K> type of the map key * @param <V> type of the map value */ @Nonnull public static <K, V> ContextFactory<ReplicatedMap<K, V>> replicatedMapContext(@Nonnull String mapName) { return ContextFactory .withCreateFn(jet -> jet.getHazelcastInstance().getReplicatedMap(mapName)); }
Processors.combineToSlidingWindowP(winPolicy, counting(), TimestampedEntry::fromWindowResult)); Vertex formatOutput = dag.newVertex("format-output", mapUsingContextP( ContextFactory.withCreateFn(x -> DateTimeFormatter.ofPattern("HH:mm:ss.SSS")), (DateTimeFormatter timeFormat, TimestampedEntry<String, Long> tse) -> String.format("%s %5s %4d",
ContextFactory.withCreateFn(jet -> null).nonCooperative(), (Object ctx, Entry<?, String> e) -> traverseStream(docLines(e.getValue()))) );
ContextFactory.withCreateFn(jet -> null).nonCooperative(), (Object ctx, Entry<Long, String> e) -> traverseStream(docLines("books/" + e.getValue())