如何从excel中导入documentDB中的批量数据? [英] How to import bulk data in documentDB from excel?

查看:385
本文介绍了如何从excel中导入documentDB中的批量数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我冲浪了有关如何在documentDB从Excel文件中插入大量数据的日子,但我没有得到任何咨询无论

I have surfed for a day about how to insert bulk data in documentDB from excel file, but I didn't get any informaion.

我能读从Excel文件中的数据,并在documentDB插入一个接一个

I can able to read data from excel file and insert one by one in documentDB

Service service = new Service();
    foreach(data in exceldata) //exceldata contains set of rows
    {
    var student = new Student();
    student.id= "";
    student.name = data.name;
    student.age = data.age;
    student.class = data.class;
    student.id = service.savetoDocumentDB(collectionLink,student); //collectionlink is a string stored in web.config
    students.add(student);
    }

Class Service
{
 public async Task<string> AddDocument(string collectionLink, Student data)
        {
            this.DeserializePayload(data);
            var result = await Client.CreateDocumentAsync(collectionLink, data);
            return result.Resource.Id;
        }
}



我能在一个实例插入所有记录?

Can I insert all records at one instance?

任何帮助将是很大很大的。

Any help would be greatly appreciable.

推荐答案

更新( 15年4月8日): DocumentDB刚刚发布了一个数据导入工具,它支持JSON文件,MongoDB中,SQL Server和CSV文件。你可以在这里找到: http://www.microsoft.com /en-us/download/details.aspx?id=46436

Update (4/8/15): DocumentDB just released a data import tool, which supports JSON files, MongoDB, SQL Server, and CSV files. You can find it here: http://www.microsoft.com/en-us/download/details.aspx?id=46436

原来的答复:

DocumentDB不支持批量从Excel文件中导入,只是还没有......不过,你可以利用DocumentDB的商店程序,使批量导入多一点友好的!

DocumentDB doesn't support bulk importing from excel files just yet... However, you can leverage DocumentDB's store procedures to make bulk import a bit more friendly!

查看从MSDN

Check out Ryan's sample from MSDN:

/** 
* This script called as stored procedure to import lots of documents in one batch. 
* The script sets response body to the number of docs imported and is called multiple times  
* by the client until total number of docs desired by the client is imported. 
* @param  {Object[]} docs - Array of documents to import. 
*/ 
function bulkImport(docs) { 
    var collection = getContext().getCollection(); 
    var collectionLink = collection.getSelfLink(); 

    // The count of imported docs, also used as current doc index. 
    var count = 0; 

    // Validate input. 
    if (!docs) throw new Error("The array is undefined or null."); 

    var docsLength = docs.length; 
    if (docsLength == 0) { 
        getContext().getResponse().setBody(0); 
    } 

    // Call the CRUD API to create a document. 
    tryCreate(docs[count], callback); 

    // Note that there are 2 exit conditions: 
    // 1) The createDocument request was not accepted.  
    //    In this case the callback will not be called, we just call setBody and we are done. 
    // 2) The callback was called docs.length times. 
    //    In this case all documents were created and we don't need to call tryCreate anymore. Just call setBody and we are done. 
    function tryCreate(doc, callback) { 
        var isAccepted = collection.createDocument(collectionLink, doc, callback); 

        // If the request was accepted, callback will be called. 
        // Otherwise report current count back to the client,  
        // which will call the script again with remaining set of docs. 
        // This condition will happen when this stored procedure has been running too long 
        // and is about to get cancelled by the server. This will allow the calling client 
        // to resume this batch from the point we got to before isAccepted was set to false 
        if (!isAccepted) getContext().getResponse().setBody(count); 
    } 

    // This is called when collection.createDocument is done and the document has been persisted. 
    function callback(err, doc, options) { 
        if (err) throw err; 

        // One more document has been inserted, increment the count. 
        count++; 

        if (count >= docsLength) { 
            // If we have created all documents, we are done. Just set the response. 
            getContext().getResponse().setBody(count); 
        } else { 
            // Create next document. 
            tryCreate(docs[count], callback); 
        } 
    } 
} 

您可以找到有关参考文档DocumentDB数据库端编程(存储过程,触发器和UDF)在这里: http://azure.microsoft.com/en-us/documentation/articles/documentdb-programming/

You can find reference documentation regarding DocumentDB database-side programming (stored procedures, triggers, and UDFs) here: http://azure.microsoft.com/en-us/documentation/articles/documentdb-programming/

这篇关于如何从excel中导入documentDB中的批量数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆