1、数据准备
db.person.insert({"name":"neo","age":"26","sex":"male"})
db.person.insert({"name":"joe","age":"28","sex":"male"})
2、使用Codec
class Person
{
public ObjectId _id;
public double Age;
public String Name;
public String Sex;
public Person(ObjectId _id, String Name, double Age, String Sex)
{
this._id=_id;
this.Name=Name;
this.Age=Age;
this.Sex=Sex;
}
}
class PersonCodec implements Codec<Person>
{
private final CodecRegistry codecRegistry;
public PersonCodec(final CodecRegistry codecRegistry) {
this.codecRegistry = codecRegistry;
}
@Override
public void encode(BsonWriter writer, Person t, EncoderContext ec) {
writer.writeStartDocument();
writer.writeName("_id");
writer.writeObjectId(t._id);
writer.writeName("name");
writer.writeString(t.Name);
writer.writeName("age");
writer.writeDouble(t.Age);
writer.writeName("sex");
writer.writeString(t.Sex);
writer.writeEndDocument();
}
@Override
public Class<Person> getEncoderClass() {
return Person.class;
}
@Override
public Person decode(BsonReader reader, DecoderContext dc)
{
reader.readStartDocument();
reader.readName();
ObjectId _id = reader.readObjectId();
reader.readName();
String name = reader.readString();
reader.readName();
double age = reader.readDouble();
reader.readName();
String sex =reader.readString();
reader.readEndDocument();
return new Person(_id,name,age,sex);
}
}
class PersonCodecProvider implements CodecProvider
{
@Override
public <T> Codec<T> get(Class<T> type, CodecRegistry cr)
{
if (type == Person.class)
{
return (Codec<T>) new PersonCodec(cr);
}
return null;
}
}
public class CodecTest
{
private static void testCodec()
{
String[] hosts = {"127.0.0.1"};
int port = 27017;
String user = null;
String password = null;
String database = "test";
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
CodecRegistries.fromProviders(new PersonCodecProvider()),
MongoClient.getDefaultCodecRegistry());
MongoClient mongoClient = getConnection(hosts,port,user,password,database,codecRegistry);
MongoDatabase db = mongoClient.getDatabase("test");
MongoCollection<Person> collection = db.getCollection("person",Person.class);
FindIterable<Person> iterable = collection.find();
MongoCursor<Person> cursor = iterable.iterator();
while (cursor.hasNext())
{
Person p = cursor.next();
System.out.println("personName: " + p.Name);
}
}
private static MongoClient getConnection(String[] hosts, int port, String user, String password, String database, CodecRegistry codecRegistry)
{
MongoClientOptions mongoClientOptions = new MongoClientOptions.Builder()
.connectionsPerHost(100)
.threadsAllowedToBlockForConnectionMultiplier(5)
.maxWaitTime(1000 * 60 * 2)
.connectTimeout(1000 * 10)
.socketTimeout(0)
.socketKeepAlive(false)
.readPreference(ReadPreference.primary())
.writeConcern(WriteConcern.ACKNOWLEDGED)
.codecRegistry(codecRegistry)
.build();
List<ServerAddress> mongoAddresses = new ArrayList<ServerAddress>();
for (String host : hosts) {
mongoAddresses.add(new ServerAddress(host, port));
}
List<MongoCredential> mongoCredentials = null;
if (user != null && !user.isEmpty() && password != null && !password.isEmpty()) {
mongoCredentials = new ArrayList<MongoCredential>();
mongoCredentials.add(MongoCredential.createMongoCRCredential(user, database, password.toCharArray()));
}
if(mongoCredentials==null)
{
return new MongoClient(mongoAddresses, mongoClientOptions);
}
else
{
return new MongoClient(mongoAddresses, mongoCredentials, mongoClientOptions);
}
}
}