use of org.bson.BSONObject in project mongo-hadoop by mongodb.
the class BSONSplitter method writeSplits.
/**
* Write out the splits file, if doing so has been enabled. Splits must
* already have been calculated previously by a call to {@link
* #readSplitsForFile readSplitsForFile} or {@link #readSplits readSplits}.
*
* @see com.mongodb.hadoop.util.MongoConfigUtil#BSON_WRITE_SPLITS
*
* @throws IOException when an error occurs writing the file
*/
public void writeSplits() throws IOException {
if (getConf().getBoolean("bson.split.write_splits", true)) {
LOG.info("Writing splits to disk.");
} else {
LOG.info("bson.split.write_splits is set to false - skipping writing splits to disk.");
return;
}
if (splitsList == null) {
LOG.info("No splits found, skipping write of splits file.");
}
Path outputPath = getSplitsFilePath(inputPath, getConf());
FileSystem pathFileSystem = outputPath.getFileSystem(getConf());
FSDataOutputStream fsDataOut = null;
try {
fsDataOut = pathFileSystem.create(outputPath, false);
for (FileSplit inputSplit : splitsList) {
BSONObject splitObj = BasicDBObjectBuilder.start().add("s", inputSplit.getStart()).add("l", inputSplit.getLength()).get();
byte[] encodedObj = bsonEnc.encode(splitObj);
fsDataOut.write(encodedObj, 0, encodedObj.length);
}
} catch (IOException e) {
LOG.error("Could not create splits file: " + e.getMessage());
throw e;
} finally {
if (fsDataOut != null) {
fsDataOut.close();
}
}
}
use of org.bson.BSONObject in project mongo-hadoop by mongodb.
the class BSONSplitter method loadSplitsFromSplitFile.
/**
* Load splits from a splits file.
*
* @param inputFile the file whose splits are contained in the splits file.
* @param splitFile the Path to the splits file.
* @throws NoSplitFileException if the splits file is not found.
* @throws IOException when an error occurs reading from the file.
*/
public void loadSplitsFromSplitFile(final FileStatus inputFile, final Path splitFile) throws NoSplitFileException, IOException {
ArrayList<BSONFileSplit> splits = new ArrayList<BSONFileSplit>();
// throws IOException
FileSystem fs = splitFile.getFileSystem(getConf());
FileStatus splitFileStatus;
FSDataInputStream fsDataStream = null;
try {
try {
splitFileStatus = fs.getFileStatus(splitFile);
LOG.info("Found split file at : " + splitFileStatus);
} catch (Exception e) {
throw new NoSplitFileException();
}
// throws IOException
fsDataStream = fs.open(splitFile);
while (fsDataStream.getPos() < splitFileStatus.getLen()) {
callback.reset();
bsonDec.decode(fsDataStream, callback);
BSONObject splitInfo = (BSONObject) callback.get();
splits.add(createFileSplitFromBSON(splitInfo, fs, inputFile));
}
} finally {
if (null != fsDataStream) {
fsDataStream.close();
}
}
splitsList = splits;
}
use of org.bson.BSONObject in project mongo-hadoop by mongodb.
the class MongoPathRetriever method get.
/**
* Returns the Object stored at a given path within a MongoDB
* document. Returns <code>null</code> if the path is not found.
*
* @param document MongoDB document in which to search.
* @param path Dot-separated path to look up.
* @return the Object stored at the path within the document.
*/
public static Object get(final BSONObject document, final String path) {
String[] parts = path.split("\\.");
Object o = document;
for (String part : parts) {
if (null == o) {
return null;
} else if (o instanceof List) {
try {
int index = Integer.parseInt(part);
if (((List) o).size() > index && index >= 0) {
o = ((List) o).get(index);
} else {
return null;
}
} catch (NumberFormatException e) {
return null;
}
} else if (o instanceof BSONObject) {
o = ((BSONObject) o).get(part);
} else {
// Hit a leaf before finding the key we were looking for.
return null;
}
}
return o;
}
use of org.bson.BSONObject in project Mycat-Server by MyCATApache.
the class SequoiaData method setGrouyBy.
public void setGrouyBy(BSONObject gb) {
this.groupby = gb;
this.type = true;
if (gb instanceof BasicBSONList) {
Object gb2 = ((BasicBSONList) gb).get(0);
if (gb2 instanceof BSONObject) {
for (String field : ((BSONObject) gb2).keySet()) {
Object val = ((BSONObject) gb2).get(field);
setField(field, getObjectToType(val));
}
}
}
}
use of org.bson.BSONObject in project Mycat-Server by MyCATApache.
the class SequoiaSQLParser method InsertData.
private int InsertData(SQLInsertStatement state) {
if (state.getValues().getValues().size() == 0) {
throw new RuntimeException("number of columns error");
}
if (state.getValues().getValues().size() != state.getColumns().size()) {
throw new RuntimeException("number of values and columns have to match");
}
SQLTableSource table = state.getTableSource();
BSONObject o = new BasicBSONObject();
int i = 0;
for (SQLExpr col : state.getColumns()) {
o.put(getFieldName2(col), getExpValue(state.getValues().getValues().get(i)));
i++;
}
DBCollection coll = this._db.getCollection(table.toString());
// coll.insert(new DBObject[] { o });
coll.insert(o);
return 1;
}
Aggregations