如何为风暴计算提供价值(How to provide values to storm for calculation)
我很难理解如何为暴风雨提供价值,因为我是暴风雨的新手。
我从入门套件开始。 我浏览了
TestWordSpout
,以下代码提供了新值public void nextTuple() { Utils.sleep(100); final String[] words = new String[] {"nathan", "mike", "jackson", "golda", "bertels"}; final Random rand = new Random(); final String word = words[rand.nextInt(words.length)]; _collector.emit(new Values(word)); }
所以我看到它一次
_collector.emit(new Values(word));
一个字_collector.emit(new Values(word));
我怎样才能直接提供一系列单词。这可能吗?
当我从nextTuple中调用一个新单词时,我的意思是从列表中随机选择并发出。 在某个时间间隔之后,随机列表可能看起来像这样
@100ms: nathan @200ms: golda @300ms: golda @400ms: jackson @500ms: mike @600ms: nathan @700ms: bertels
如果我已经拥有此列表的集合并将其提供给风暴,该怎么办?
I have a hard time understanding how to provide values to storm since i am a newbie to storm.
I started with the starter kit. I went through the
TestWordSpout
and in that the following code provides new valuespublic void nextTuple() { Utils.sleep(100); final String[] words = new String[] {"nathan", "mike", "jackson", "golda", "bertels"}; final Random rand = new Random(); final String word = words[rand.nextInt(words.length)]; _collector.emit(new Values(word)); }
So i see it's taking one word at a time
_collector.emit(new Values(word));
How i can provide a collection of words directly.Is this possible?
What I mean when nextTuple is called a new words is selected at random from the list and emitted. The random list may look like this after certain time interval
@100ms: nathan @200ms: golda @300ms: golda @400ms: jackson @500ms: mike @600ms: nathan @700ms: bertels
What if i already have a collection of this list and just feed it to storm.
原文:https://stackoverflow.com/questions/22012008
最满意答案
很少有事情需要修复或添加(在您的案例中不确定,因为有些零件丢失)
资源:让我自己去测试,因为你的东西缺少一些物品
@Path("/") public class AccountResource { @GET @Path("/account") // route to a specific method.re @Produces(MediaType.APPLICATION_JSON) public Response saveDataIntoHash() { List<Account> accounts = new ArrayList<Account>(); accounts.add(new Account("Stack", "Savings")); accounts.add(new Account("Overflow", "Checkings")); GenericEntity generic = new GenericEntity<List<Account>>(accounts){}; return Response.status(201).entity(generic).build(); } }
假设你有这种依赖性:
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>${jersey-version}</version> </dependency>
测试用例:注意客户端配置。 这是必要的。
public void testMyResource() { ClientConfig config = new DefaultClientConfig(); config.getClasses().add(JacksonJaxbJsonProvider.class); config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); Client c = Client.create(config); WebResource resource = c.resource(Main.BASE_URI); ClientResponse response = resource.path("account") .accept("application/json").get(ClientResponse.class); List<Account> accounts = response.getEntity(new GenericType<List<Account>>(){}); StringBuilder builder = new StringBuilder("=== Accounts ===\n"); for (Account account: accounts) { builder.append("Name: ").append(account.getName()).append(", ") .append("Type: ").append(account.getType()).append("\n"); } builder.append("=================="); System.out.println(builder.toString()); }
帐户(客户)类缺少一个注释。 当您使用字段注释时,它是必需的。 另一个选择是为
id
添加一个getter和setter@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) // <======= This Here public class Account { // added toString for testing @Override public String toString() { return "Account{" + "name=" + name + ", type=" + type + ", id=" + id + '}'; } }
测试结果 :
=== Accounts === Name: Stack, Type: Savings Name: Overflow, Type: Checkings ==================
注意:此测试基于服务器端没有任何问题的假设。
Few things either need to be fixed or added (not sure in your case as some parts are missing)
Resource: Made my own to test, as yours was missing a few items
@Path("/") public class AccountResource { @GET @Path("/account") // route to a specific method.re @Produces(MediaType.APPLICATION_JSON) public Response saveDataIntoHash() { List<Account> accounts = new ArrayList<Account>(); accounts.add(new Account("Stack", "Savings")); accounts.add(new Account("Overflow", "Checkings")); GenericEntity generic = new GenericEntity<List<Account>>(accounts){}; return Response.status(201).entity(generic).build(); } }
Assumed you have this dependency:
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>${jersey-version}</version> </dependency>
Test case: Notice the client configuration. This is needed.
public void testMyResource() { ClientConfig config = new DefaultClientConfig(); config.getClasses().add(JacksonJaxbJsonProvider.class); config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); Client c = Client.create(config); WebResource resource = c.resource(Main.BASE_URI); ClientResponse response = resource.path("account") .accept("application/json").get(ClientResponse.class); List<Account> accounts = response.getEntity(new GenericType<List<Account>>(){}); StringBuilder builder = new StringBuilder("=== Accounts ===\n"); for (Account account: accounts) { builder.append("Name: ").append(account.getName()).append(", ") .append("Type: ").append(account.getType()).append("\n"); } builder.append("=================="); System.out.println(builder.toString()); }
Account (client) class is missing one annotation. It is required as you are using field annotations. Another option is to add a getter and setter for the
id
@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) // <======= This Here public class Account { // added toString for testing @Override public String toString() { return "Account{" + "name=" + name + ", type=" + type + ", id=" + id + '}'; } }
Result from test:
=== Accounts === Name: Stack, Type: Savings Name: Overflow, Type: Checkings ==================
Note: this test is based on the assumption that nothing is wrong on your server side.
相关问答
更多-
要配置ObjectMapper以与Jersey一起使用,您可以 创建一个ContextResolver ,如此处所示,并向客户端注册解析器。 ClientConfig config = new DefaultClientConfig(); config.register(new ObjectMapperContextResolver()); Client client = Client.create(config); 或实例化作为构造函数参数传递ObjectMapper的JacksonJsonProvid ...
-
很少有事情需要修复或添加(在您的案例中不确定,因为有些零件丢失) 资源:让我自己去测试,因为你的东西缺少一些物品 @Path("/") public class AccountResource { @GET @Path("/account") // route to a specific method.re @Produces(MediaType.APPLICATION_JSON) public Response saveDataIntoHash() { Li ...
-
@GET确实支持字符串列表 设置 : Java:1.7 泽西版本:1.9 资源 @Path("/v1/test") 子资源 : // receive List of Strings @GET @Path("/receiveListOfStrings") public Response receiveListOfStrings(@QueryParam("list") final List
list){ log.info("receieved list of size="+list.s ... -
据我所知,没有便利的方法。 你可以这样做: public Response getDomainGroups(@PathParam("ownerID") String ownerID) { WebResource r = client.resource(URL_BASE + "/" + URL_GET_GROUPS + "/" + ownerID); ClientResponse resp = r.get(ClientResponse.class); return clientResp ...
-
你不使用ClientResponse 。 你应该使用Response 。 当你希望响应实体自动反序列化时,你只需要使用.post()的第二个参数。 当你不使用第二个参数时,返回类型是Response 。 然后你可以用Response#readEntity()读取/反序列化实体 Response response = target .request(MediaType.TEXT_PLAIN) .post(Entity.form(form)); String data = response.r ...
-
在Resteasy 3及以上版本中ClientResponse的替代方案是什么?(What is the alternative to ClientResponse in Resteasy 3 and above?)[2022-11-14]
您不再需要泛型参数,因为您可以在客户端接口方法中直接使用您自己的类型,如下所示: @GET @Path("matrixParam") @Produces("application/json") Matrix getMatrix(@MatrixParam("param")String param); 要查询远程服务,您可以使用resteasy客户端代理框架: Client client = ClientBuilder.newClient(); WebTarge ... -
GenericType jersey客户端中的java.util.List。
()(java.util.List. [2022-05-08]() in GenericType jersey client) 看起来Jersey似乎试图通过反射实例化一个接口(List),并因为无法找到构造函数而失败。 泽西岛代码应该是 new GenericType>() Looks like Jersey is trying to instantiate an interface (List) via reflection, and failing because it cannot find the constructor. The Jersey code should be new Gen ... -
在这里,这是一个解决方案,至少在jersey-client.jar API方面。 我需要添加以下行: List
g = resource.get(new GenericType - >(){});
EntityJAXBxml entity = g.get(1);
System.out.println(g.size()+" : "+entity.getdValue()+" : Element count: "+entity.get ...
-
getEntity()方法不接受任何参数,因此您无法传递(new GenericType(){}) 请改用以下内容 Version version = commonClient.authorizedRequestBuilder(commonClient.webTarget .path("/apps/blabla/default/" + appName + "/" + appName) .queryParam("object_type", "app")) .ac ...
-
如何使用jersey客户端使用https RestFul Webservice(How to consume https RestFul Webservice using jersey client)[2023-02-11]
在您的java安装文件夹中有一个名为cacerts的文件。 这是您的JRE的“Keystore”或“Truststore”。 它包含JRE信任的所有证书。 您可以从信任库中添加/删除证书。 要轻松添加/删除证书,您可以使用GUI Programm Keystore Explorer 。 选项1使用Keystore Explorer和默认的Truststore 使用Keystore Explorer打开信任库。 (信任库应位于/lib/security/cacerts ,默认密码应为“ch ...