Skip to content

Instantly share code, notes, and snippets.

@tgrall
Created October 3, 2015 12:55
Show Gist options
  • Save tgrall/3a8eb90c7939201ed9c0 to your computer and use it in GitHub Desktop.
Save tgrall/3a8eb90c7939201ed9c0 to your computer and use it in GitHub Desktop.
OJAI/MapR-DB Sample code to save/get binary content
package demo;
import com.mapr.db.MapRDB;
import com.mapr.db.Table;
import org.ojai.Document;
import javax.print.Doc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
public class MapRDBSJSONType {
static final String TABLE_NAME = "/apps/demo";
static Table table = null;
public static void main(String[] args) throws Exception {
String employeePic = "/Users/tgrall/Downloads/jdoe.png";
String imageRestoreTo = "/Users/tgrall/Downloads/tmp/";
MapRDBSJSONType app = new MapRDBSJSONType();
app.insertEmployee("jdoe", "John", "Doe", employeePic);
app.readEmployee("jdoe", imageRestoreTo );
if (table != null) {
table.close();
}
}
public MapRDBSJSONType() {
if (MapRDB.tableExists(TABLE_NAME)) {
table = MapRDB.getTable(TABLE_NAME);
} else {
table = MapRDB.createTable(TABLE_NAME);
}
}
private void insertEmployee(String id, String firstName, String lastName, String picture) throws IOException {
System.out.println("\n== == WRITE TO MAPR-DB == == == ");
Document emp = MapRDB.newDocument()
.set("_id",id)
.set("first_name",firstName)
.set("last_name", lastName);
System.out.println(emp);
// import an image
byte[] imageAsBytes = extractBytes(picture);
emp.set("image", imageAsBytes);
// save the document in the MapR DB Table
table.insertOrReplace(emp);
System.out.println("\n== == == == == ");
}
private void readEmployee(String id, String location) throws Exception {
Document emp = table.findById(id);
System.out.println("\n== == READ FROM MAPR-DB == == == ");
System.out.println(emp);
byte[] imageAsBytes = emp.getBinary("image").array();
String fileCreated = saveImageToFile(imageAsBytes, location);
System.out.println("\n\t File saved at : "+ location + fileCreated);
System.out.println("\n== == == == == ");
}
/**
* Get the bytes of the file passes as parameter
* @param fileName
* @return
* @throws java.io.IOException
*/
public byte[] extractBytes (String fileName) throws IOException {
FileInputStream fileInputStream=null;
File file = new File(fileName);
byte[] bFile = new byte[(int) file.length()];
try {
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
}catch(Exception e){
e.printStackTrace();
}
return bFile;
}
/**
* Save bytes into a file
* @param imagesAsBytes
* @param location
* @return
* @throws Exception
*/
public String saveImageToFile( byte[] imagesAsBytes , String location ) throws Exception {
String fileName = location+ "maprdb-json-"+ UUID.randomUUID().toString() +".png";
try (FileOutputStream fos = new FileOutputStream(fileName)) {
fos.write(imagesAsBytes);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return fileName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment