Migrar documentos de MongoDB a Couchbase con jmoordb
Los pasos son:
Definimos el Entity
@Getter
@Setter
public class Planetas {
@Id
private String idplaneta;
private String planeta;
@Ignore
private Date fecha;
public Planetas() {
}
public Planetas(String idplaneta, String planeta, Date fecha) {
this.idplaneta = idplaneta;
this.planeta = planeta;
this.fecha = fecha;
}
}
Definir el facade MongoDB
public class PlanetasFacade extends AbstractFacade<Planetas> {
MongoClientProvider mongoclientProvider = new MongoClientProvider();
public PlanetasFacade() {
super(Planetas.class, "fantasy", "planetas");
}
@Override
protected MongoClient getMongoClient() {
return mongoclientProvider.getMongoClient();
}
@Override
public Object findById(String key, String value) {
return search(key,value);
}
@Override
public Object findById(String key, Integer value) {
return search(key,value);
}
}
Definir el Facade Couchbase
public class PlanetasFacade extends CouchbaseAbstractFacade<Planetas> {
CouchbaseClientProvider couchbseclientProvider = new CouchbaseClientProvider();
public PlanetasFacade() {
super(Planetas.class, "planetas", "planetas");
}
@Override
protected Cluster getCluster() {
return couchbseclientProvider.getCluster();
}
@Override
public Object findById(String key, String value) {
return search(key, value);
}
@Override
public Object findById(String key, Integer value) {
return search(key, value);
}
}
Definir la clase MongoClientProvider
public class MongoClientProvider {
private MongoClient mongoClient = null;
public MongoClient getMongoClient() {
mongoClient = new MongoClient();
try {
} catch (Exception e) {
System.out.println("getMongoClient() " + e.getLocalizedMessage());
}
return mongoClient;
}
}
Definir la clase CouchbaseClientProvider
public class CouchbaseClientProvider {
private Cluster cluster;
public Cluster getCluster() {
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
.connectTimeout(10000) //10000ms = 10s, default is 5s
.build();
cluster = CouchbaseCluster.create("localhost");
return cluster;
}
}
Los pasos son:
- Definir el Entity
- Definir el Facade MongoDB
- Definir el Facade Couchbase
- Definir el Provider para MongoDB
- Definir el Provider para Couchbase
Definimos el Entity
@Getter
@Setter
public class Planetas {
@Id
private String idplaneta;
private String planeta;
@Ignore
private Date fecha;
public Planetas() {
}
public Planetas(String idplaneta, String planeta, Date fecha) {
this.idplaneta = idplaneta;
this.planeta = planeta;
this.fecha = fecha;
}
}
Definir el facade MongoDB
public class PlanetasFacade extends AbstractFacade<Planetas> {
MongoClientProvider mongoclientProvider = new MongoClientProvider();
public PlanetasFacade() {
super(Planetas.class, "fantasy", "planetas");
}
@Override
protected MongoClient getMongoClient() {
return mongoclientProvider.getMongoClient();
}
@Override
public Object findById(String key, String value) {
return search(key,value);
}
@Override
public Object findById(String key, Integer value) {
return search(key,value);
}
}
Definir el Facade Couchbase
public class PlanetasFacade extends CouchbaseAbstractFacade<Planetas> {
CouchbaseClientProvider couchbseclientProvider = new CouchbaseClientProvider();
public PlanetasFacade() {
super(Planetas.class, "planetas", "planetas");
}
@Override
protected Cluster getCluster() {
return couchbseclientProvider.getCluster();
}
@Override
public Object findById(String key, String value) {
return search(key, value);
}
@Override
public Object findById(String key, Integer value) {
return search(key, value);
}
}
Definir la clase MongoClientProvider
public class MongoClientProvider {
private MongoClient mongoClient = null;
public MongoClient getMongoClient() {
mongoClient = new MongoClient();
try {
} catch (Exception e) {
System.out.println("getMongoClient() " + e.getLocalizedMessage());
}
return mongoClient;
}
}
Definir la clase CouchbaseClientProvider
public class CouchbaseClientProvider {
private Cluster cluster;
public Cluster getCluster() {
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
.connectTimeout(10000) //10000ms = 10s, default is 5s
.build();
cluster = CouchbaseCluster.create("localhost");
return cluster;
}
}
CĆ³digo para ejecutar la migraciĆ³n
com.migrador.couchbase.ejb.PlanetasFacade planetascouchbaseFacade = new PlanetasFacade() ;
com.migrador.mongodb.ejb.PlanetasFacade planetasmongodbFacade = new com.migrador.mongodb.ejb.PlanetasFacade() ;
List<Planetas> list = planetasmongodbFacade.findAll();
if(!list.isEmpty()){
list.forEach((p) -> {
planetascouchbaseFacade.save(p,false);
});
}
Base de datos en MongoDB
Couchbase despuĆ©s de la migraciĆ³n:
Comments