在Java8中,如何提取一个可以将函数作为这些链接lambda的参数传递的函数?
我在一段代码中有一个代码模式,使用卡夫卡流不断重复,我做一个映射,然后按键分组,然后减少。看起来是这样的:在Java8中,如何提取一个可以将函数作为这些链接lambda的参数传递的函数?,java,lambda,apache-kafka-streams,method-chaining,Java,Lambda,Apache Kafka Streams,Method Chaining,我在一段代码中有一个代码模式,使用卡夫卡流不断重复,我做一个映射,然后按键分组,然后减少。看起来是这样的: KTable<ProjectKey, EventConfigurationIdsWithDeletedState> eventConfigurationsByProjectTable = eventConfigurationStream .map((key, value) -> { Map<String, Boolean>
KTable<ProjectKey, EventConfigurationIdsWithDeletedState> eventConfigurationsByProjectTable = eventConfigurationStream
.map((key, value) -> {
Map<String, Boolean> eventConfigurationUpdates = new HashMap<>();
eventConfigurationUpdates.put(key.getEventConfigurationId(), value != null);
ProjectKey projectKey = ProjectKey.newBuilder().setId(key.getProjectId()).build();
EventConfigurationIdsWithDeletedState eventConfigurationIdsWithDeletedState = EventConfigurationIdsWithDeletedState.newBuilder().setEventConfigurations(eventConfigurationUpdates).build();
return KeyValue.pair(projectKey, eventConfigurationIdsWithDeletedState);
})
.groupByKey()
.reduce((aggValue, newValue) -> {
Map<String, Boolean> newEventConfigurations = newValue.getEventConfigurations();
Map<String, Boolean> aggEventConfigurations = aggValue.getEventConfigurations();
Map.Entry<String, Boolean> newEntry = newEventConfigurations.entrySet().iterator().next();
if (newEntry.getValue())
aggEventConfigurations.putAll(newEventConfigurations);
else
aggEventConfigurations.remove(newEntry.getKey());
if (aggEventConfigurations.size() == 0)
return null;
return aggValue;
});
<K, V, T, U> U mapGroupReduce(final KStream<K, V> stream, final BiFunction<K, V, T> mapper, final BiFunction<U, U, U> reducer) {
return stream.map(mapper).groupByKey().reduce(reducer);
}
KTable eventConfigurationsByProjectTable=eventConfigurationStream
.map((键、值)->{
Map eventConfigurationUpdates=新建HashMap();
eventConfigurationUpdates.put(key.getEventConfigurationId(),value!=null);
ProjectKey ProjectKey=ProjectKey.newBuilder().setId(key.getProjectId()).build();
EventConfigurationDSWithDeletedState EventConfigurationDSWithDeletedState=EventConfigurationDSWithDeletedState.newBuilder().setEventConfigurations(eventConfigurationUpdates).build();
返回KeyValue.pair(projectKey,eventconfigurationdswithdeletedstate);
})
.groupByKey()
.reduce((aggValue,newValue)->{
Map newEventConfigurations=newValue.getEventConfigurations();
Map aggEventConfigurations=aggValue.getEventConfigurations();
Map.Entry newEntry=newEventConfigurations.entrySet().iterator().next();
if(newEntry.getValue())
aggEventConfigurations.putAll(newEventConfigurations);
其他的
aggEventConfigurations.remove(newEntry.getKey());
if(aggEventConfigurations.size()==0)
返回null;
返回值;
});
(eventConfigurationStream的类型为KStream
)
另一个遵循此模式的示例。注意这里也有一个过滤器,但情况并非总是如此:
KTable<ProjectKey, NotificationSettingsTransition> globalNotificationSettingsPerProjectTable = notificationSettingTable.toStream()
.filter((key, value) -> {
return key.getEventConfigurationId() == null;
})
.map((key, value) -> {
ProjectKey projectKey = ProjectKey.newBuilder().setId(key.getProjectId()).build();
Map<String, NotificationSetting> notificationSettingsMap = new HashMap<>();
notificationSettingsMap.put(getAsCompoundKeyString(key), value);
NotificationSettingsTransition notificationSettingTransition = NotificationSettingsTransition
.newBuilder()
.setNotificationSettingCompoundKeyLastUpdate(getAsCompoundKey(key))
.setNotificationSettingLastUpdate(value)
.setEventConfigurationIds(new ArrayList<>())
.setNotificationSettingsMap(notificationSettingsMap)
.build();
return KeyValue.pair(projectKey, notificationSettingTransition);
})
.groupByKey()
.reduce((aggValue, newValue) -> {
Map<String, NotificationSetting> notificationSettingMap = aggValue.getNotificationSettingsMap();
String compoundKeyAsString = getAsString(newValue.getNotificationSettingCompoundKeyLastUpdate());
if (newValue.getNotificationSettingLastUpdate() != null)
notificationSettingMap.put(compoundKeyAsString, newValue.getNotificationSettingLastUpdate());
else
notificationSettingMap.remove(compoundKeyAsString);
aggValue.setNotificationSettingCompoundKeyLastUpdate(newValue.getNotificationSettingCompoundKeyLastUpdate());
aggValue.setNotificationSettingLastUpdate(newValue.getNotificationSettingLastUpdate());
aggValue.setNotificationSettingsMap(notificationSettingMap);
return aggValue;
});
KTable globalNotificationSettingsPerProjectTable=notificationSettingTable.toStream()
.filter((键、值)->{
return key.getEventConfigurationId()==null;
})
.map((键、值)->{
ProjectKey ProjectKey=ProjectKey.newBuilder().setId(key.getProjectId()).build();
Map notificationSettingsMap=new HashMap();
notificationSettingsMap.put(getAsCompoundKeyString(键),值);
NotificationSettingTranslation notificationSettingTransition=NotificationSettingTranslation
.newBuilder()
.setNotificationSettingCompoundKeyLastUpdate(getAsCompoundKey(键))
.setNotificationSettingLastUpdate(值)
.SetEventConfigurationId(新的ArrayList())
.setNotificationSettingsMap(notificationSettingsMap)
.build();
返回KeyValue.pair(projectKey,notificationSettingTransition);
})
.groupByKey()
.reduce((aggValue,newValue)->{
Map notificationSettingMap=aggValue.getNotificationSettingsMap();
字符串compoundKeyAsString=getAsString(newValue.getNotificationSettingCompoundKeyLastUpdate());
if(newValue.getNotificationSettingLastUpdate()!=null)
notificationSettingMap.put(compoundKeyAsString,newValue.getNotificationSettingLastUpdate());
其他的
notificationSettingMap.remove(compoundKeyAsString);
aggValue.setNotificationSettingCompoundKeyLastUpdate(newValue.getNotificationSettingCompoundKeyLastUpdate());
aggValue.setNotificationSettingLastUpdate(newValue.getNotificationSettingLastUpdate());
aggValue.setNotificationSettingsMap(notificationSettingMap);
返回值;
});
(notificationSettingsTable的类型为KTable notificationSettingTable
,但也会立即转换为KStream。)
我如何将其提取到一个函数中,在该函数中,我为map代码和reduce代码传递一个函数,但不必重复
.map().groupByKey().reduce()的模式?尽管返回类型不同,并且取决于map函数中的代码,并且应该保持类型化。理想情况下使用Java8,但可能会有更高的版本。当map代码中的KeyValuePair
的内部类型不会更改时,我想我有一个很好的主意如何做,但不确定现在如何做。您可以将函数参数化以接受两个泛型函数,在调用函数时将推断(或明确设置,如果不可能的话)
对于map
的输入,您需要一个BiFunction
,对于reduce
您需要一个BiFunction
,其中:
K
是map
函数中的键的类型
V
是map
函数中值的类型
T
是map
函数的返回类型
U
是聚合器的类型,reduce
函数的值和返回类型
查看和,可以获得更详细的类型信息以进一步约束函数
这将使您的自定义函数如下所示:
KTable<ProjectKey, EventConfigurationIdsWithDeletedState> eventConfigurationsByProjectTable = eventConfigurationStream
.map((key, value) -> {
Map<String, Boolean> eventConfigurationUpdates = new HashMap<>();
eventConfigurationUpdates.put(key.getEventConfigurationId(), value != null);
ProjectKey projectKey = ProjectKey.newBuilder().setId(key.getProjectId()).build();
EventConfigurationIdsWithDeletedState eventConfigurationIdsWithDeletedState = EventConfigurationIdsWithDeletedState.newBuilder().setEventConfigurations(eventConfigurationUpdates).build();
return KeyValue.pair(projectKey, eventConfigurationIdsWithDeletedState);
})
.groupByKey()
.reduce((aggValue, newValue) -> {
Map<String, Boolean> newEventConfigurations = newValue.getEventConfigurations();
Map<String, Boolean> aggEventConfigurations = aggValue.getEventConfigurations();
Map.Entry<String, Boolean> newEntry = newEventConfigurations.entrySet().iterator().next();
if (newEntry.getValue())
aggEventConfigurations.putAll(newEventConfigurations);
else
aggEventConfigurations.remove(newEntry.getKey());
if (aggEventConfigurations.size() == 0)
return null;
return aggValue;
});
<K, V, T, U> U mapGroupReduce(final KStream<K, V> stream, final BiFunction<K, V, T> mapper, final BiFunction<U, U, U> reducer) {
return stream.map(mapper).groupByKey().reduce(reducer);
}
在您的情况下,您需要使用以下选项,而不是使用BiFunction
s:
KeyValueMapper
用于映射器
减速器
用于减速器
然而,这真的比每次编写stream.map(M).groupByKey().reduce(R)
要好得多吗?更详细的版本更明确,并且考虑到映射器和reducer的相对大小,您并没有真正节省那么多。我可能误解了您的问题:您是否在问是否可以编写一个运行map(a).gBK().reduce(B)
?如果是这样的话,我不相信这会给你带来任何实质性的好处:大部分代码都在A
和B
代码块中,你仍然需要保留它。@AndyTurner是的,就是这样,因为我重复了3到4次这种模式