I want to use Hazelcast in my Java application, but I also have .net applications which need to get/set data to/from the Hazelcast cache. I thought to use the "rest" approach. I have 2 questions:
1) How can I post and get a complex type? If I have a Person object with fields name (String), age (Integer), birthDate (Date), and sex (Enum), how should I post this info and how should I parse person info?
2) I have a cached IMap<String, String>. After I post data "three" with key "3" from a Poster plugin, on the Java side map.get("3") returns something like:
RestValue{contentType='text/plain;charset=utf-8', value="three"}"
I expect this code to return just "three" without any cast operation.
I will be pleased if you give information about this issues.
Thanks in advance...
It sounds like you have a couple of different types of issues here with your current setup.
1) I don't have a good answer for this because I don't actually know much about rest clients in .net. You also may also get better results if you change your architecture as mentioned below.
2) It looks like the problem you have here is that you're just storing the raw request object(RestValue) in the IMap rather than storing the content of that object. Usually requests to a rest api contain more information than just the value sent to your server so you'll have to extract the value from the RestValue in your rest api. Hazelcast RestValue has a method called getValue(), so you should just be able to call getValue() which returns a byte[]. You should then just convert that byte[] to a String (or whatever datatype you prefer to store, maybe int in this case) and store the result in your IMap instead of just storing the entire RestValue object.
As far as having .net + java architecture, it may be best to run a Hazelcast-server node in whichever language you prefer and then have a .net hazelcast-client node and a java hazelcast-client node that are all connected to the same cluster. This way you can have all of your .net code run on your .net client completely separattd from your java infrastructure and communicate between the separate languages using hazelcast.
Related
I'm working on a springboot project and having some trouble with ElasticSearch.
The user will put some JSON-format elasticsearch DSL query strings in the database and they are black-box to me. What I need to do is get the query strings and use them so search information in elasticsearch.
In python, the DSL can be a parameter like this:
body = {
"query":{
"match_all":{}
}
}
es.search(index="my_index",doc_type="test_type",body=body)
How can I perform the search without knowing the details of the string and just using the JSON format query in Java?
I believe there are two ways to do it in modern ES client libraries. I haven't tried them myself, but first one seems to be pretty straightforward.
First one is using low-level client:
Request request = new Request("POST", "/index/_search");
request.setJsonEntity(jsonString);
Response response = client.performRequest(request);
Seems like it's enough just to push JSON as string into setJsonEntity, and you're already set.
Second one is to use high level client, and this gets tricky, though it can provide more robust API. As you might know, elasticsearch has concept of XContent, which is serialization/deserialization to/from different formats, including JSON. Theoretically, it is possible to create JsonXContentParser, which then can be used to instantiate SearchSourceBuilder:
SearchSourceBuilder.fromXContent(jsonXContentParser);
The problem is only that JsonXContentParser requires number of arguments to be instantiated, and i'm not sure how to properly create those dependencies.
Sometimes, we need to create some thrift objects in unit tests. We can do it by manually create object using Java code, like:
MyObj myObj = new MyObj();
myObj.setName("???");
myObj.setAge(111);
But which is not convenient. I'm looking for a way to create objects with some readable text.
We can convert thrift objects to JSON with TSimpleJSONProtocol, and get very readable JSON string, like:
{ "name": "???", "age": 111 }
But the problem is TSimpleJSONProtocol is write only, thrift can't read it back to construct an instance of MyObj.
Although there is a TJSONProtocol which supports to serialize and deserialize, but the generated JSON is not readable, it uses a very simplified JSON format and most of the field names are missing. Not convenient to construct it in tests.
Is there any way to convert thrift objects to readable string and also can convert it back? If TSimpleJSONProtocol supports converting back, which is just what I'm looking for
The main goal of Thrift is to provide efficient serialization and RPC mechanisms. What you want is something that is - at least partially - contrary to that. Human-readable data structures and machine processing efficiency are to a good extent conflicting goals, and Thrift favors the latter over the former.
You already found out about the TSimpleJson and TJson protocols and about their pros and cons, so there is not much to add. The only thing that is left to say is this: the protocol/transport stack of Thrift is simple enough.
This simplicity makes it possible to add another protocol based on your specific needs without much or overly complicated work. One could probably even write an XML protocol (if anyone really wants such bloatware) in short time.
The only caveat, especially vis-à-vis your specific case, is the fact that Thrift needs the field ID to deserialize the data. So you either need to store them in the data, or you need some other mechanism which is able to retrieve that field ID based on the field and structure names.
Is a Data Transfer Object the same as a Value Object or are they different? If they are different then where should we use a DTO and where should we use a VO?
The programming language we are talking about is Java and the context is - there is a web application, which fetches data from a database and then processes it and ultimately the processed information is displayed on the front-end.
A value object is a simple object whose equality isn't based on identity.
A data transfer object is an object used to transfer data between software application subsystems, usually between business layers and UI. It is focused just on plain data, so it doesn't have any behaviour.
A Data Transfer Object is a kludge for moving a bunch of data from one layer or tier to another, the goal is to minimize the number of calls back and forth by packing a bunch of stuff into the same data structure and sending it together. Some people also use it, like Michael points out in his post here, so that the classes used by one layer are not exposed to the layer calling it. When I refer to DTO as a kludge, I mean there's not a precise abstract concept getting implemented, it's a practical workaround for helping with communication between application layers.
A Value Object is something where we're only interested in its value, like a monetary amount, a date range, or a code from a lookup table. It does not have an identity, meaning you would not be concerned, if you had several of them, of keeping track of which is which, because they are not things in themselves.
Contrast Value Objects to things that do have a unique identity in your system, which are called Entities. If you have a system where it tracks a customer making a payment, the customer and the payment are entities, because they represent specific things, but the monetary amount on the payment is just a value, it doesn't have an existence by itself, as far as your system is concerned. How something relates to your system determines if it is a Value Object or an Entity.
use a DTO at the boundary of your services if you don't want to send the actual domain object to the service's clients - this helps reduce dependencies between the client and service.
values objects are simply objects whose equality isn't based on identity e.g. java.lang.Integer
DTOs and value objects aren't really alternatives to each other.
They are different, but I've even used the two interchangeably in the past, which is wrong. I read that DTO (Data Transfer Object) was called a VO ( Value Object) in the first edition of the Core J2EE Patterns book, but wasn't able to find that reference.
A DTO, which I've sometimes called a Dumb Transfer Object to help me remember it's a container and shouldn't have any business logic is used to transport data between layers and tiers. It just should be an object with attributes that has getters/setters.
A VO however is similar to a JAVA Enum and represents a fixed set of data. A VO doesn't have object identity (the address of the object instance in memory), it is identified by its value and is immutable.
Martin Fowler, talking about Data Transfer Objects (DTOs):
Many people in the Sun community use the term "Value Object" for this pattern. I use it to mean something else.
So the term "Value Object" has been used to mean DTO, but as of him (and the other posters), its use as a DTO seems discouraged.
Good detailed answer in Matthias Noback article Is it a DTO or a Value Object?
In short a DTO:
Declares and enforces a schema for data: names and types
Offers no guarantees about correctness of values
A value object:
Wraps one or more values or value objects
Provides evidence of the correctness of these values
Maybe because of lack of experience, but I would put it this way: It's the matter of scope.
DTO has word transfer in it so it means some parts of the system will communicate using it.
Value object has smaller scope, you will pass set of data in value object instead in array from one service to the other.
As much as I understood niether of them is "object whose equality isn't based on identity".
I wanted to use Infinispan today in a sort of probably unfamiliar matter.
I want to save a Variable, let's call it x a couple of times in the cache - while being able to adress it as X.
Plain, old MVCC. However, it seems that infinispan uses MVCC on the backend - but I wasn't able to use it in my little test-application.
This is the corresponding code:
acTest.put("test", "blubber", 0, TimeUnit.MILLISECONDS );
acTest.put("test", "nothing", 0, TimeUnit.MILLISECONDS );
if( acTest.containsKey("test") )
{
Object foo = acTest.get("test"); // don't know how to get the "blubber" out of that
String name = (String) test2.get("name");
System.out.println(name);
}
Sure enough, acTest contains the key - but I was not able to adress the value "blubber" of that key - when I higher the numerical value of "nothing" foo holds "nothing" ... but I want to get the first version of "foo" - hence "blubber"
I want to be able to adress the different versions of test. I think that I can create different versions of "test" with the different parameters in the put operation - however eclipse has absolutely no documentation for that matter ...
Could somebody help me?
Infinispan uses MVCC in it's container for internal purposes, this is currently not a feature exposed via user API, besides via writeSkewCheck.
In version 5.1 the API will expose Optimistic locking, which might be useful for some use cases needing to take advantage of the MVCC capabilities, but you still won't be able to extract a previous value.
You could use AtomicMap to store multiple values, or use custom key objects containing the version, building what you need on top of Infinispan's API.
DeltaAware is another option, but it's a low-level interface meant for experts.
I amusing Flex for front end building. I have one doubt regarding sending two array of strings from java to flex.
How can I do that?
For example I have a value object defined like below.
class Test
{
String value1[]={"1","2","3"};
String value2[]={"narendra","mani","suresh","kane"};
//Getter and setters goes here
}
Can any one help me on this?
Thanks,
Narendra
This is really a matter of how do you want Flex to communicate with your Java layer, I think the closest to a turn key solution to this is using the BlazeDS jar on the server to do the ActionScript Message Format conversion between Java DTOs and Actionscript DTOs, basically how it works is you code up the Java side then you can have it generate the Java equivalent DTOs that will be tagged with Metadata so when the Java objects are serialized and sent across the wire the client has them as typed objects.
http://opensource.adobe.com/wiki/display/blazeds/BlazeDS
Alternatively you could expose the data as XML using a JSP to generate the XML then just using an HTTPService call to the JSP to get the XML data then since AS3 makes use of E4X parsing the XML into AS3 objects is very easy, lots of examples of doing this just search for e4x AS3 for examples.
Hope this helps,
Shaun