MapRTemplate is the central support class for MapR database operations.
To simplify the creation of data repositories Spring Data MapR DB provides a generic repository programming model. It will automatically create a repository proxy for you.
public interface UserRepository extends MapRRepository <User, String> {
}You can use Java to configure your Spring Data environment as show below. Make sure that folder for database is created and you have permissions for write in this folder.
@Configuration
@EnableMapRRepository
class Config extends AbstractMapRConfiguration {
@Override
protected String getDatabaseName() {
return "DB_FOLDER_NAME";
}
@Override
protected String getHost() {
return "HOST";
}
@Override
protected String getUsername() {
return "USERNAME";
}
@Override
protected String getPassword() {
return "PASSWORD";
}
}Mark your entity with annotation @Document, then table's name will be generated by class name, or you can specify custom name for table by annotation's parameter(@Document("TABLE_NAME")).
Add @Id annotation before ID field.
@Document
//@Document("TABLE_NAME")
public class User {
@Id
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}You can use repository as shown below:
@Service
public class MyService {
private final UserRepository userRepository;
@Autowired
public MyService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void doWork() {
userRepository.deleteAll();
User user = new User();
user.setId("test_id");
user.setName("Test user");
userRepository.save(user);
User userById = userRepository.findById("test_id").get();
}
}As Spring Data implementation for MaprDB uses OJAI you have to configure Drill for ordering. Querying in OJAI Applications