我有一个json文件,其最大嵌套级别为5,并且json文件是动态的,意味着它里面不存在什么键值对,但是json文件中的每个项目都符合Dynamo DB表并具有一个属性(每项)与哈希键同名。
现在如何直接将json数据文件导入DynamoDB?
在dynamo中是否有类似mongoimport的命令直接加载json文件? 或使用Jackson或其他Java库将动态json加载到dynamo的任何技术?
答案 0 :(得分:0)
您必须创建一个脚本来构建DynamoDB Json,然后通过aws-sdk将其放入数据库。 如果您有多个项目,那么使用DynamoDB BatchWrite项目,您可以看到DynamoDB BatchWrite项目的文档
这是我用javascript开发的脚本。我的脚本正在读取CSV文件并为每行构建一个Json,并将其添加到全局Json(BatchWrite DynamoDB Json)中,因此将其上传到表中:
var fs = require('fs');
var parse = require('csv-parse');
require('should');
let AWS = require("aws-sdk");
const {
Observable
} = require('rxjs');
const {
map,
retryWhen,
flatMap,
takeWhile,
filter,
switchMap
} = require('rxjs/operators');
output = [];
next = true;
tmp = [];
let params = {
RequestItems: {
'YOU DYNAMODB TABLE NAME': []
}
}
fs.readFile("./data/produits.csv", function(err, data) {
//console.log(data.toString('utf8'));
parse(data.toString('utf8'), {
comment: '#',
delimiter: ';',
relax_column_count: true,
relax: true
}, function(err, output) {
this.output = output;
let j = 0;
doAdd(j, 24)
})
})
function doAdd(j, max) {
add(j, max).subscribe(tmp => {
j = j + 25;
let params = {
RequestItems: {
'YOU DYNAMODB TABLE NAME': []
}
}
console.log(tmp.length)
if (tmp.length > 0) {
params.RequestItems['YOU DYNAMODB TABLE NAME'] = tmp;
create(params).subscribe(data => {
if (j < this.output.length && this.output.length - j > 25) {
doAdd(j, 24);
} else if (j < this.output.length && this.output.length - j < 25) {
doAdd(j, this.output.length - j - 1)
} else {
return false;
}
})
}
})
}
function add(j, max) {
return new Observable(Observer => {
console.log("-----------> " + j);
let tmp = []
for (i = j; i <= j + max; i++) {
console.log(this.output[i][0], " ", this.output[i].length)
listTag = getDynamoList(this.output[i][41], "S")
listAccessory = getDynamoList(this.output[i][42], "S")
listCategories = getDynamoList(this.output[i][43], "N")
//console.log(this.output[i])
if (this.output[i]) {
let Items = {
'PutRequest': {
'Item': {
'_id': {
S: this.output[i][0]
},
'name': {
S: this.output[i][3]
},
'description': {
S: this.output[i][1]
},
'description_short': {
S: this.output[i][2]
},
'link_rewrite': {
S: this.output[i][4]
},
'meta_description': {
S: this.output[i][5]
},
'meta_keywords': {
S: this.output[i][6]
},
'meta_title': {
S: this.output[i][7]
},
'id_supplier': {
S: this.output[i][8]
},
'id_manufacturer': {
S: this.output[i][9]
},
'id_category_default': {
N: this.output[i][10]
},
'id_tax_rules_group': {
S: this.output[i][11]
},
'online_only': {
N: this.output[i][12]
},
'ean13': {
S: this.output[i][13]
},
'upc': {
S: this.output[i][14]
},
'quantity': {
N: this.output[i][15]
},
'minimal_quantity': {
N: this.output[i][16]
},
'price': {
N: parseFloat(this.output[i][17]).toString()
},
'wholesale_price': {
N: parseFloat(this.output[i][18]).toString()
},
'unity': {
S: this.output[i][19]
},
'unit_price_ratio': {
N: parseFloat(this.output[i][20]).toString()
},
'additional_shipping_cost': {
N: parseFloat(this.output[i][21]).toString()
},
'reference': {
S: this.output[i][22]
},
'supplier_reference': {
S: this.output[i][23]
},
'location': {
S: this.output[i][24]
},
'width': {
N: parseFloat(this.output[i][25]).toString()
},
'height': {
N: parseFloat(this.output[i][26]).toString()
},
'depth': {
N: parseFloat(this.output[i][27]).toString()
},
'weight': {
N: parseFloat(this.output[i][28]).toString()
},
'out_of_stock': {
N: this.output[i][29]
},
'quantity_discount': {
N: this.output[i][30]
},
'customizable': {
S: this.output[i][31]
},
'active': {
S: this.output[i][32]
},
'available_for_order': {
S: this.output[i][33]
},
'available_date': {
N: new Date(this.output[i][34]).getTime().toString()
},
'condition': {
S: this.output[i][35]
},
'show_price': {
S: this.output[i][36]
},
'visibility': {
S: this.output[i][37]
},
'date_add': {
N: new Date(this.output[i][38]).getTime().toString()
},
'date_upd': {
N: new Date(this.output[i][39]).getTime().toString()
},
'image': {
S: this.output[i][40]
},
'tags': {
L: listTag
},
'accessory': {
L: listAccessory
},
'other_categories': {
L: listCategories
}
}
}
}
tmp.push(Items)
for (let i in Items.PutRequest.Item) {
for (let j in Items.PutRequest.Item[i]) {
if (!Items.PutRequest.Item[i][j]) {
delete(Items.PutRequest.Item[i])
}
}
}
params.RequestItems['YOU DYNAMODB TABLE NAME'] = tmp;
}
}
Observer.next(tmp)
})
}
function getDynamoList(array, type) {
let tmp = [];
if (array) {
let tmparray = array.split(",")
for (let item of tmparray) {
let empty = {};
empty[type] = item;
tmp.push(empty)
}
return tmp
}
return ''
}
function uuid() {
let uuid = "",
i, random;
for (i = 0; i < 32; i++) {
random = Math.random() * 16 | 0;
if (i == 8 || i == 12 || i == 16 || i == 20) {
uuid += "-"
}
uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16);
}
return uuid;
}
function create(params) {
return new Observable.create(Observer => {
var creds = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'YOUR IDENTITY POOL ID'
})
AWS.config.update({
region: 'eu-central-1',
credentials: creds,
apiVersions: {
dynamodb: '2012-08-10'
}
});
var dynamodb = new AWS.DynamoDB({
region: 'eu-west-3'
});
return dynamodb.batchWriteItem(params, function(err, data) {
if (err) {
return Observer.error(err);;
} else {
return Observer.next(data);
}
});
})
}