【问题标题】:Creating Mongo/mapReduce data source in icCube在 icCube 中创建 Mongo/mapReduce 数据源
【发布时间】:2015-07-29 16:45:49
【问题描述】:

我想在 icCube (http://www.iccube.com/support/documentation/user_guide/schemas_cubes/ds_mongodb.php#mapReduce) 中创建一个 MongoDB/mapReduce 数据源,下面的脚本在 Mongo shell 中运行良好,当我将相同的代码粘贴到 icCube 中时,它应该如何格式化以被 icCube 接受数据源生成器,我收到此错误消息:

MongoDB : invalid JSON (table:Test.mapReduce) : [var location_map = function() { if (this.companyId = "1234" && this.Parent_Location !== undefined && this.Parent_Location.value.length > 0 && this.Parent_Location.value[0].id !== undefined && this.Parent_Location.value[0].id !== null) { emit(this.Parent_Location.value[0].id, {Location_NameOfLocation: this.Location_NameOfLocation, LocationID: this._id, CountryName: "", companyId : 0}); } } var country_map = function() { if (this.companyId = "1234") { emit(this._id, { CountryName: this.CountryName, Location_NameOfLocation: "", LocationID: 0, companyId : this.companyId }); } } var r = function(key, values) { var result = {LocationID: 0, CountryName: "", Location_NameOfLocation: "", companyId : 0};    values.forEach(function(value) { if (result.LocationID === 0 && value.LocationID !== null ) { result.LocationID = value.LocationID; } if (result.companyId === 0 && value.companyId !== null ) { result.companyId = value.companyId; } if (result.CountryName === "" && value.CountryName !== null ) { result.CountryName = value.CountryName; } if (result.Location_NameOfLocation === "" && value.Location_NameOfLocation !== null ) { result.Location_NameOfLocation = value.Location_NameOfLocation; } }); return result; } db.runCommand( { mapReduce: Location, map: location_map, reduce: r, out: { replace: LocationsCountries }, query: {companyId : "1234"} } ) db.runCommand( { mapReduce: Countries, map: country_map, reduce: r, out: { reduce: LocationsCountries }, query: {companyId : "1234" } } )] ^

Mongo mapReduce 脚本:

var location_map = function() {
  if (this.companyId = "1234" && this.Parent_Location !== undefined && this.Parent_Location.value.length > 0 && this.Parent_Location.value[0].id !== undefined && this.Parent_Location.value[0].id !== null) {
  emit(this.Parent_Location.value[0].id, {Location_NameOfLocation: this.Location_NameOfLocation, LocationID: this._id, CountryName: "", companyId : 0});
  }
}
var country_map = function() {
if (this.companyId = "1234") {
    emit(this._id, { CountryName: this.CountryName,  Location_NameOfLocation: "", LocationID: 0, companyId : this.companyId });  
    }
}
var r = function(key, values) {
    var result = {LocationID: 0, CountryName: "", Location_NameOfLocation: "", companyId : 0};  
    values.forEach(function(value) {
        if (result.LocationID === 0 &&  value.LocationID !== null  ) {   result.LocationID = value.LocationID;        }
        if (result.companyId === 0 &&  value.companyId !== null  ) {   result.companyId = value.companyId;        }
        if (result.CountryName === "" &&  value.CountryName !== null  ) {   result.CountryName = value.CountryName;        }
        if (result.Location_NameOfLocation === "" &&  value.Location_NameOfLocation !== null  ) {   result.Location_NameOfLocation = value.Location_NameOfLocation;        }
    });
    return result;
}
db.runCommand(
               {
                 mapReduce: "Location",
                 map: location_map,
                 reduce: r,
                 out: { replace: "LocationsCountries" },
                 query: {companyId : "1234"}
               }
             )

db.runCommand(
               {
                 mapReduce: "Countries",
                 map: country_map,
                 reduce: r,
                 out: { reduce: "LocationsCountries" },
                 query: {companyId : "1234" }
               }
             )       

谢谢, 巴林特

【问题讨论】:

    标签: mongodb iccube


    【解决方案1】:

    您必须在字符串中定义您的函数(如果需要这些字符串中的字符串,则使用 Javascript 转义)。这在 icCube 文档 (www) 中有描述。这是一个结合单引号字符串和双引号字符串的示例。

    {
        "mapReduce": "Location",
        "map": 'function() {
            if (this.companyId = "1234" && this.Parent_Location !== undefined && this.Parent_Location.value.length > 0 && this.Parent_Location.value[0].id !== undefined && this.Parent_Location.value[0].id !== null) {
                emit(this.Parent_Location.value[0].id, {Location_NameOfLocation: this.Location_NameOfLocation, LocationID: this._id, CountryName: "", companyId : 0});
            }
        }',
        "reduce": 'function(key, values) {
                var result = {LocationID: 0, CountryName: "", Location_NameOfLocation: "", companyId : 0};  
                values.forEach(function(value) {
                    if (result.LocationID === 0 &&  value.LocationID !== null  ) {   result.LocationID = value.LocationID;        }
                    if (result.companyId === 0 &&  value.companyId !== null  ) {   result.companyId = value.companyId;        }
                    if (result.CountryName === "" &&  value.CountryName !== null  ) {   result.CountryName = value.CountryName;        }
                    if (result.Location_NameOfLocation === "" &&  value.Location_NameOfLocation !== null  ) {   result.Location_NameOfLocation = value.Location_NameOfLocation;        }
                });
                return result;
            }',
        "out": { 
            "replace": "LocationsCountries" 
        },
        "query": {
            "companyId" : "1234"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2011-12-27
      相关资源
      最近更新 更多