I've put a HashMap<String, Set<Long>> object into a MongoDB document under "disabled_channels" but I can't figure out how to retrieve it and turn it back into a HashMap<String, Set<Long>> object in local memory. I'm usually very good at reading in lists, individual values, etc, with something like found.getList("disabled_commands", String.class) but I'm really lost on how to approach this.
MongoCollection<Document> collection = bot.getDataManager().getConfig();
Document config = new Document("guild", guild.getIdLong());
Document found = collection.find(config).first();
// I get lost here
Document itself is a map implementation internally. Reference
You need to use get function on found document and cast it to Document as below
Document channels = (Document)found.get("disabled_channels")
Then you can access elements in channels using the same get method and cast it as per the need.
Related
Using Kafka Admin from Java I'm trying to find how can I get the retention.bytes and retention.ms from a topic
The only thing I found in the API is this
adminClient.describeConfigs(Collection<ConfigRsources>???).values().get(ConfigResource???).get().get(String).
In case this is the way, not sure where I can get where it supposed to be passed.
The last string I guess it would be the config name retention.byte
where I can get
Read through the Javadoc. ConfigResource has a Type enum.
Then describing accepts a collection and returns a map via values, keyed by items in that collection. Therefore, pull out the reference to its own variable for reuse.
Then I suggest using constants
rather than strings.
Map<String, Object> config = new HashMap<>();
config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
AdminClient client = AdminClient.create(config);
ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, "example");
Config topicConfig = client.describeConfigs(Collections.singletonList(resource)).values().get(resource);
topicConfig.get(TopicConfig.RETENTION_BYTES_CONFIG);
topicConfig.get(TopicConfig.RETENTION_MS_CONFIG);
EDIT: I changed my mind. I would find a way to generate the Java class and load the JSON as an object of that class.
I just discovered that exists a variant of JSON called JSON-LD.
It seems to me a more structured way of defining JSON, that reminds me XML with an associated schema, like XSD.
Can I create a Java class from JSON-LD, load it at runtime and use it to convert JSON-LD to an instantiation of that class?
I read the documentation of both the implementations but I found nothing about it. Maybe I read them bad?
Doing a Google search brought me to a library that will decode the JSON-LD into an "undefined" Object.
// Open a valid json(-ld) input file
InputStream inputStream = new FileInputStream("input.json");
// Read the file into an Object (The type of this object will be a List, Map, String, Boolean,
// Number or null depending on the root object in the file).
Object jsonObject = JsonUtils.fromInputStream(inputStream);
// Create a context JSON map containing prefixes and definitions
Map context = new HashMap();
// Customise context...
// Create an instance of JsonLdOptions with the standard JSON-LD options
JsonLdOptions options = new JsonLdOptions();
// Customise options...
// Call whichever JSONLD function you want! (e.g. compact)
Object compact = JsonLdProcessor.compact(jsonObject, context, options);
// Print out the result (or don't, it's your call!)
System.out.println(JsonUtils.toPrettyString(compact));
https://github.com/jsonld-java/jsonld-java
Apparently, it can take it from just a string as well, as if reading it from a file or some other source. How you access the contents of the object, I can't tell. The documentation seems to be moderately decent, though.
It seems to be an active project, as the last commit was only 4 days ago and has 30 contributors. The license is BSD 3-Clause, if that makes any difference to you.
I'm not in any way associate with this project. I'm not an author nor have I made any pull requests. It's just something I found.
Good luck and I hope this helped!
see this page: JSON-LD Module for Jackson
I just started apache-spark with java. There is many documents saved in a collection I want to find a document on the basis of some key and update it.
Simply I want to do find and update in apache-spark with java
This is the code to read a document form mongo
Map<String, String> readOverrides = new HashMap<String, String>();
readOverrides.put("collection", "bb_playing22");
ReadConfig readConfig = ReadConfig.create(createJavaSparkContext()).withOptions(readOverrides);
JavaMongoRDD<Document> customRdd = MongoSpark.load(createJavaSparkContext(), readConfig);
JavaRDD<Document> rdd = customRdd.filter(((t1) -> {
return t1.getLong("playing22_id") == 3; //To change body of generated lambdas, choose Tools | Templates.
}));
but not able to update this document
Spark does a lazy evaluation of your transformations hence unless you call an action over an rdd, it won't execute any transformations you code.
See common actions here.
Also, this does not seem like a use case where you'd like to use spark.
If your actual requirement is just to update a document as per a value then you should look into indexing in mongo and just update a document through a mongo driver in java directly.
How do I filter an Array of an object which also has objects again using Java (Bson Documents).
For example I've a user which has an array = followed Users. Now I want to filter a specific user from that list how do I do that ?
here is a picture
I tried smth like this but I just don't have any idea since this is getting more complex :
Document filter = new Document("follows", new Document("$elemMatch", eq("uID", uID)));
Hi I need to convert Mongo Document to DBObject (BasicDBObject).
I am uploading a file to mongo using GridFS and I want to set metadata, which I get in document. I know Document is pretty much the same as DBObject. I know I can do something like this:
Document doc = new Document();
BasicDBObject.parse(doc.toJson());
But isn't this needlessly performance heavy?
The gridFS method setMetaData() accepts only DBObject so i have to convert it.
Is there a better way of doing that rather then converting it to string and back ?
You are kind of micro-optimizing here.
However, since both classes are implementations of Map, you can just do:
Document document = new Document();
BasicDBObject basicDBObject = new BasicDBObject(document);
Internally this does a Map#putAll operation that puts all entries of the Document map into the BasicDbObject map.
I know this is an old question and there is an accepted answer however it is not correct.
The proposed answer only does a shallow conversion between Document and DBOject. If your Json object contains nested objects or lists they will not be converted properly.
I got around this problem by serialising to JSON string. It is not efficient at all but might be enough in most cases, and at least it is correct:
public final class BsonConverter {
public static Document toDocument(DBObject dbObject) {
return Document.parse(dbObject.toString());
}
public static DBObject toDBObject(Document document) {
return BasicDBObject.parse(document.toJson());
}
}