有一个我需要调用的rest API.. Post Call -它有一个有效负载
{ "name" : <<value_in_excel>>, "something" : "all rest values are fixed" }
字符串Excel/CSV中有100,000个名称属性值。一个API调用需要800 ms-1 sec。我怎么打这个长途电话?
6jjcrrmo1#
通过excel.js阅读excel文件,并使用Axios库通过POST调用创建用户。
x1c 0d1x的数据这是demo代码。
的
const axios = require('axios'); const qs = require('qs'); const ExcelJS = require('exceljs'); // Keycloak details const client_id = 'admin-cli'; const user_name = 'admin'; const pass_word = 'admin'; const grant_type = 'password'; const tokenURL = 'http://localhost:8080/auth/realms/master/protocol/openid-connect/token'; const usersEndpoint = 'http://localhost:8080/auth/admin/realms/my-realm/users'; // Read from Excel file async function readExcelFile() { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile('Users.xlsx'); const worksheet = workbook.getWorksheet('Users'); let users = []; worksheet.eachRow({ includeEmpty: false }, (row, rowNumber) => { if (rowNumber !== 1) { // Skipping header row users.push({ username: row.getCell(1).value, firstName: row.getCell(2).value, lastName: row.getCell(3).value, email: row.getCell(4).value }); } }); return users; } // Function to get token async function getToken() { const data = qs.stringify({ client_id: client_id, username: user_name, password: pass_word, grant_type: grant_type }); const config = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }; try { const response = await axios.post(tokenURL, data, config); return response.data.access_token; } catch (error) { console.error('Error fetching token:', error); throw error; } } // Function to create user in Keycloak async function createUserInKeycloak(user, token) { try { await axios.post(usersEndpoint, { username: user.username, email: user.email, firstName: user.firstName, lastName: user.lastName, enabled: true, emailVerified: true }, { headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' } }); console.log(`User created: ${user.username}`); } catch (error) { console.error(`Error creating user ${user.username}:`, error); } } // Main function to handle the process async function main() { try { const users = await readExcelFile(); const token = await getToken(); for (const user of users) { await createUserInKeycloak(user, token); } } catch (error) { console.error('An error occurred:', error); } } main();
字符串运行
node create_user.js
型
保存为'package.json'
{ "dependencies": { "@faker-js/faker": "^8.3.1", "axios": "^1.6.3", "exceljs": "^4.4.0", "qs": "^6.11.2" } }
型安装依赖项
npm install
型这是通过fake.js创建10万用户
const { faker } = require('@faker-js/faker'); const ExcelJS = require('exceljs'); const USER_COUNT = 100000; const FILE_NAME = 'Users.xlsx'; // Sets to store used usernames and emails const usedUsernames = new Set(); const usedEmails = new Set(); // Function to create a random user with a unique username and email const createRandomUser = () => { let username, email; do { username = faker.internet.userName(); if (usedUsernames.has(username)) { username += Math.floor(Math.random() * 10000); } } while (usedUsernames.has(username)); usedUsernames.add(username); do { email = faker.internet.email(); if (usedEmails.has(email)) { email = email.split('@')[0] + Math.floor(Math.random() * 10000) + '@' + email.split('@')[1]; } } while (usedEmails.has(email)); usedEmails.add(email); return { username: username, firstName: faker.person.firstName(), lastName: faker.person.lastName(), email: email }; } // Function to write users to Excel file const saveUsersToExcel = async (users) => { const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet('Users'); // Define columns worksheet.columns = [ { header: 'Username', key: 'username', width: 20 }, { header: 'First Name', key: 'firstName', width: 15 }, { header: 'Last Name', key: 'lastName', width: 15 }, { header: 'Email', key: 'email', width: 50 } ]; // Add users to the worksheet worksheet.addRows(users); // Save workbook await workbook.xlsx.writeFile(FILE_NAME); console.log(`Saved ${users.length} users to ${FILE_NAME}`); } // Main function to create users and save to Excel const main = async () => { let users = []; for (let i = 0; i < USER_COUNT; i++) { users.push(createRandomUser()); } await saveUsersToExcel(users); } main().catch(err => console.error(err));
node save_excel.js
的x1c4d 1x的
我通过fake.js创建了100 K。为了避免用户名和电子邮件代码。
型这是由docker compose启动的Keycloak
version: '3.7' services: postgres: image: postgres volumes: - postgres_data:/var/lib/postgresql/data environment: POSTGRES_DB: keycloak POSTGRES_USER: keycloak POSTGRES_PASSWORD: password keycloak: image: quay.io/keycloak/keycloak:legacydocke environment: DB_VENDOR: POSTGRES DB_ADDR: postgres DB_DATABASE: keycloak DB_USER: keycloak DB_SCHEMA: public DB_PASSWORD: password KEYCLOAK_USER: admin KEYCLOAK_PASSWORD: admin # KC_FEATURES: # scripts ports: - 8080:8080 # Expose to user with localhost:8080 restart: always depends_on: - postgres volumes: postgres_data: # Keycloack volume driver: local
docker compose up
创建10万用户通过假用户名https://fakerjs.dev/api/person.html阅读Excel通过excel.js https://www.npmjs.com/package/exceljs获取Keycloak Keycloak v.18的主令牌:如何使用Keycloak API与用户进行操作通过POST调用Keycloak创建用户是否可以使用REST API使用jboss/keycloak:16.1.1创建新用户?
1条答案
按热度按时间6jjcrrmo1#
通过excel.js阅读excel文件,并使用Axios库通过POST调用创建用户。
概述步骤
x1c 0d1x的数据
这是demo代码。
文件结构
的
保存为'用户名. js'
字符串
运行
型
这是结果。
的
安装安装目录
保存为'package.json'
型
安装依赖项
型
这是通过fake.js创建10万用户
保存为'save_excel.js'
型
创建excel文件
型
Excel结果
的
x1c4d 1x的
终端
我通过fake.js创建了100 K。为了避免用户名和电子邮件代码。
型
这是由docker compose启动的Keycloak
型
启动Keycloak
型
手动创建'my-realm'
型
增加主令牌寿命
详细信息
创建10万用户通过假用户名https://fakerjs.dev/api/person.html
阅读Excel通过excel.js https://www.npmjs.com/package/exceljs
获取Keycloak Keycloak v.18的主令牌:如何使用Keycloak API与用户进行操作
通过POST调用Keycloak创建用户是否可以使用REST API使用jboss/keycloak:16.1.1创建新用户?