Search in sources :

Example 96 with BSONObject

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();
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileSystem(org.apache.hadoop.fs.FileSystem) LazyBSONObject(org.bson.LazyBSONObject) BSONObject(org.bson.BSONObject) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) IOException(java.io.IOException) FileSplit(org.apache.hadoop.mapreduce.lib.input.FileSplit) BSONFileSplit(com.mongodb.hadoop.input.BSONFileSplit)

Example 97 with BSONObject

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;
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) FileSystem(org.apache.hadoop.fs.FileSystem) BSONFileSplit(com.mongodb.hadoop.input.BSONFileSplit) ArrayList(java.util.ArrayList) LazyBSONObject(org.bson.LazyBSONObject) BSONObject(org.bson.BSONObject) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException)

Example 98 with BSONObject

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;
}
Also used : BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) List(java.util.List)

Example 99 with BSONObject

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));
            }
        }
    }
}
Also used : BasicBSONList(org.bson.types.BasicBSONList) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 100 with BSONObject

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;
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DBCollection(com.sequoiadb.base.DBCollection) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

BSONObject (org.bson.BSONObject)101 BasicBSONObject (org.bson.BasicBSONObject)49 Test (org.junit.Test)34 BasicDBObject (com.mongodb.BasicDBObject)19 SerializableString (com.fasterxml.jackson.core.SerializableString)14 SerializedString (com.fasterxml.jackson.core.io.SerializedString)14 LinkedHashMap (java.util.LinkedHashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)11 DBObject (com.mongodb.DBObject)10 ArrayList (java.util.ArrayList)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Map (java.util.Map)6 BasicBSONDecoder (org.bson.BasicBSONDecoder)6 BSONDecoder (org.bson.BSONDecoder)5 BSONEncoder (org.bson.BSONEncoder)5 BasicBSONEncoder (org.bson.BasicBSONEncoder)5 LazyBSONObject (org.bson.LazyBSONObject)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)4