how to remove duplicate from result in datastax solr - solr
wt=json&indent=true&q=*%3A*&start=0&rows=100&fq=scorer%3A2269ae85-b738-49c2-b188-17c3d79400a6
I am Getting Response Like :
{
"responseHeader":{
"status":0,
"QTime":0},
"response":{"numFound":5,"start":0,"docs":[
{
**"tournament_scheduler_id":"e405cfab-18c6-43df-ab6d-1279b91cddaa",**
"modified_date":"2016-01-08T14:20:48.433Z",
"win_team_runs":0,
"elect_to":"Elected to bat",
"ground_id":"93b5d90c-16a9-4fb8-a21f-f549bb080ac5",
"scorer":["2269ae85-b738-49c2-b188-17c3d79400a6"],
"modified_by":"96588171-ef8b-43f2-a86d-e7d3460aac29",
"tournament_id":"1ff0ae4e-146e-4ef1-ad07-507d0b9be9de",
"no_of_overs":20,
"lose_team_wickets":0,
"created_by":"96588171-ef8b-43f2-a86d-e7d3460aac29",
"created_date":"2016-01-07T07:01:24.419Z",
"away_team_id":"e3ca398f-ae3b-42f0-bc87-e0edadaebb53",
"lose_team_runs":0,
"match_type":"Twenty 20",
"status":"InProgress",
"game_date":"2016-01-30T00:00:00Z",
"win_team_wickets":0,
"status_of_match":"",
"toss_won_by":"e38877ec-f44f-498f-ada1-049d097a13dc",
"home_team_id":"e38877ec-f44f-498f-ada1-049d097a13dc",
"active":1,
"umpire_id_1":"679c0940-b1da-4297-a969-4d0be8018b1b",
"umpire_id_2":"ae28b91d-58af-4f00-92f9-4f7f3ce61ec5"},
{
"tournament_scheduler_id":"cca34732-d2d0-4662-9567-2748d9b3888d",
"modified_date":"2016-01-07T10:56:23.385Z",
"win_team_runs":0,
"elect_to":"Elected to bat",
"ground_id":"485cdf29-bf9b-4330-aa31-e2e3a411c741",
"scorer":["2269ae85-b738-49c2-b188-17c3d79400a6"],
"modified_by":"63b4a76f-b9c3-49da-8f31-6772ac5d3dc5",
"tournament_id":"c91244a0-a42a-44a3-bf10-a20023d4d2e9",
"no_of_overs":20,
"lose_team_wickets":0,
"created_by":"63b4a76f-b9c3-49da-8f31-6772ac5d3dc5",
"created_date":"2016-01-06T06:26:04.765Z",
"away_team_id":"a0dad51e-1398-413f-9d4d-cdff470e90c2",
"lose_team_runs":0,
"match_type":"Twenty 20",
"status":"InProgress",
"game_date":"2016-01-15T00:00:00Z",
"win_team_wickets":0,
"status_of_match":"",
"toss_won_by":"a0dad51e-1398-413f-9d4d-cdff470e90c2",
"home_team_id":"f62b86f9-2b65-4352-964b-9572803835d9",
"active":1,
"umpire_id_1":"acb01386-5fc6-45db-a186-715ec2fd7ce2"},
{
**"tournament_scheduler_id":"e405cfab-18c6-43df-ab6d-1279b91cddaa",**
"modified_date":"2016-01-08T14:20:48.433Z",
"win_team_runs":0,
"elect_to":"Elected to bat",
"ground_id":"93b5d90c-16a9-4fb8-a21f-f549bb080ac5",
"scorer":["2269ae85-b738-49c2-b188-17c3d79400a6"],
"modified_by":"96588171-ef8b-43f2-a86d-e7d3460aac29",
"tournament_id":"1ff0ae4e-146e-4ef1-ad07-507d0b9be9de",
"no_of_overs":20,
"lose_team_wickets":0,
"created_by":"96588171-ef8b-43f2-a86d-e7d3460aac29",
"created_date":"2016-01-07T07:01:24.419Z",
"away_team_id":"e3ca398f-ae3b-42f0-bc87-e0edadaebb53",
"lose_team_runs":0,
"match_type":"Twenty 20",
"status":"InProgress",
"game_date":"2016-01-30T00:00:00Z",
"win_team_wickets":0,
"status_of_match":"",
"toss_won_by":"e38877ec-f44f-498f-ada1-049d097a13dc",
"home_team_id":"e38877ec-f44f-498f-ada1-049d097a13dc",
"active":1,
"umpire_id_1":"679c0940-b1da-4297-a969-4d0be8018b1b",
"umpire_id_2":"ae28b91d-58af-4f00-92f9-4f7f3ce61ec5"},
{
"tournament_scheduler_id":"c012b682-b0d9-4f75-8261-a6f4728f70a0",
"modified_date":"2016-01-08T17:17:51.043Z",
"win_team_runs":0,
"elect_to":"",
"ground_id":"93b5d90c-16a9-4fb8-a21f-f549bb080ac5",
"scorer":["2269ae85-b738-49c2-b188-17c3d79400a6"],
"modified_by":"96588171-ef8b-43f2-a86d-e7d3460aac29",
"tournament_id":"1ff0ae4e-146e-4ef1-ad07-507d0b9be9de",
"no_of_overs":0,
"lose_team_wickets":0,
"created_by":"96588171-ef8b-43f2-a86d-e7d3460aac29",
"created_date":"2016-01-08T17:17:51.043Z",
"away_team_id":"e3ca398f-ae3b-42f0-bc87-e0edadaebb53",
"lose_team_runs":0,
"match_type":"",
"status":"Upcoming",
"game_date":"2016-02-24T00:00:00Z",
"win_team_wickets":0,
"status_of_match":"",
"home_team_id":"e38877ec-f44f-498f-ada1-049d097a13dc",
"active":1,
"umpire_id_1":"679c0940-b1da-4297-a969-4d0be8018b1b"},
{
"tournament_scheduler_id":"93be7a28-4867-461a-bff2-d81615c6d360",
"modified_date":"2016-01-08T18:59:47.567Z",
"win_team_runs":0,
"elect_to":"Elected to bowl",
"ground_id":"485cdf29-bf9b-4330-aa31-e2e3a411c741",
"scorer":["2269ae85-b738-49c2-b188-17c3d79400a6"],
"modified_by":"63b4a76f-b9c3-49da-8f31-6772ac5d3dc5",
"tournament_id":"c91244a0-a42a-44a3-bf10-a20023d4d2e9",
"no_of_overs":20,
"lose_team_wickets":0,
"created_by":"63b4a76f-b9c3-49da-8f31-6772ac5d3dc5",
"created_date":"2016-01-08T14:31:35.298Z",
"away_team_id":"f62b86f9-2b65-4352-964b-9572803835d9",
"lose_team_runs":0,
"match_type":"Twenty 20",
"status":"InProgress",
"game_date":"2016-03-14T00:00:00Z",
"win_team_wickets":0,
"status_of_match":"",
"toss_won_by":"f62b86f9-2b65-4352-964b-9572803835d9",
"home_team_id":"a0dad51e-1398-413f-9d4d-cdff470e90c2",
"active":1,
"umpire_id_1":"4e113da2-5be9-40e5-a689-1a3e9d959c3d"}]
}}
here,tournament_scheduler_id is uniquekey in core
Related
SQL Server using TypeORM: Error Timeout: Request failed to complete when inserting records by batch
The issue I am having is that when I run the migration command I always get an ETIMEOUT error, but if I comment out the await populateTable() the code will run without any error. I already tried to increase the requestTimeout from 15sec to 300sec but that didn't help. Here's my code: Create the following: // ormconfig.json [ { "name": "app", "type": "mssql", "host": "127.0.0.1", "port": 1433, "username": "root", "password": "root", "database": "app", "logging": true, "options": { "useUTC": true }, "entities": ["src/**/*.entity.ts"], "migrations": ["migration/**/*.ts"], "cli": { "migrationsDir": "migration" } } ] // root-dir/src/grouping/grouping.entity.ts import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm'; #Entity({ name: 'grouping' }) export class Grouping { #PrimaryGeneratedColumn({ name: 'id', unsigned: true }) id: number; #Column({ name: 'name', unique: true }) name: string; } // root-dir/migration/tables/grouping.ts import { getRepository, QueryRunner, Table, TableIndex } from 'typeorm'; import { Grouping } from '../../src/grouping/grouping.entity'; export async function up(queryRunner: QueryRunner): Promise<any> { await createTable(queryRunner); await createIndexes(queryRunner); await populateTable(); } export async function down(queryRunner: QueryRunner): Promise<any> { await queryRunner.dropIndex('grouping', 'IDX_GROUP'); await queryRunner.dropTable('grouping'); } async function createTable(queryRunner: QueryRunner) { return queryRunner.createTable( new Table({ name: 'grouping', columns: [ { name: 'id', type: 'integer', isPrimary: true, isGenerated: true, generationStrategy: 'increment', unsigned: true, }, { name: 'name', type: 'varchar', isUnique: true, }, ], }), true, ); } async function createIndexes(queryRunner: QueryRunner) { return await queryRunner.createIndex( 'grouping', new TableIndex({ name: 'IDX_GROUP', columnNames: ['name'], }), ); } async function populateTable() { await getRepository(Grouping, 'app').save([{ name: 'classification' }, { name: 'categorization' }]); } // root-dir/migration/initial-migration.ts import { MigrationInterface, QueryRunner } from 'typeorm'; import * as groupingTable from './tables/grouping'; export class InitialMigration1550229771145 implements MigrationInterface { async up(queryRunner: QueryRunner): Promise<any> { await groupingTable.up(queryRunner); } async down(queryRunner: QueryRunner): Promise<any> { await groupingTable.down(queryRunner); } } Run the migration command. ts-node ./node_modules/typeorm/cli.js migration:run -c This should create the Grouping table and insert 2 records but what I am getting is this error: Query: CREATE TABLE "grouping" ("id" integer NOT NULL IDENTITY(1,1), "name" varchar(255) NOT NULL, CONSTRAINT "UQ_07314fe287a837177015c041131" UNIQUE ("name"), CONSTRAINT "PK_135d73da7246e0250716afdc0ab" PRIMARY KEY ("id")) query: SELECT SCHEMA_NAME() AS "schema_name" query: SELECT DB_NAME() AS "db_name" query: SELECT * FROM "app"."INFORMATION_SCHEMA"."TABLES" WHERE ("TABLE_SCHEMA" = 'dbo' AND "TABLE_NAME" = 'grouping') query: SELECT * FROM "app"."INFORMATION_SCHEMA"."COLUMNS" WHERE ("TABLE_SCHEMA" = 'dbo' AND "TABLE_NAME" = 'grouping') query: SELECT "columnUsages".*, "tableConstraints"."CONSTRAINT_TYPE", "chk"."definition" FROM "app"."INFORMATION_SCHEMA"."CONSTRAINT_COLUMN_USAGE" "columnUsages" INNER JOIN "app"."INFORMATION_SCHEMA"."TABLE_CONSTRAINTS" "tableConstraints" ON "tableConstraints"."CONSTRAINT_NAME" = "columnUsages"."CONSTRAINT_NAME" LEFT JOIN "app"."sys"."check_constraints" "chk" ON "chk"."name" = "columnUsages"."CONSTRAINT_NAME" WHERE (("columnUsages"."TABLE_SCHEMA" = 'dbo' AND "columnUsages"."TABLE_NAME" = 'grouping' AND "tableConstraints"."TABLE_SCHEMA" = 'dbo' AND "tableConstraints"."TABLE_NAME" = 'grouping')) AND "tableConstraints"."CONSTRAINT_TYPE" IN ('PRIMARY KEY', 'UNIQUE', 'CHECK') query: SELECT "fk"."name" AS "FK_NAME", 'app' AS "TABLE_CATALOG", "s1"."name" AS "TABLE_SCHEMA", "t1"."name" AS "TABLE_NAME", "col1"."name" AS "COLUMN_NAME", "s2"."name" AS "REF_SCHEMA", "t2"."name" AS "REF_TABLE", "col2"."name" AS "REF_COLUMN", "fk"."delete_referential_action_desc" AS "ON_DELETE", "fk"."update_referential_action_desc" AS "ON_UPDATE" FROM "app"."sys"."foreign_keys" "fk" INNER JOIN "app"."sys"."foreign_key_columns" "fkc" ON "fkc"."constraint_object_id" = "fk"."object_id" INNER JOIN "app"."sys"."tables" "t1" ON "t1"."object_id" = "fk"."parent_object_id" INNER JOIN "app"."sys"."schemas" "s1" ON "s1"."schema_id" = "t1"."schema_id" INNER JOIN "app"."sys"."tables" "t2" ON "t2"."object_id" = "fk"."referenced_object_id" INNER JOIN "app"."sys"."schemas" "s2" ON "s2"."schema_id" = "t2"."schema_id" INNER JOIN "app"."sys"."columns" "col1" ON "col1"."column_id" = "fkc"."parent_column_id" AND "col1"."object_id" = "fk"."parent_object_id" INNER JOIN "app"."sys"."columns" "col2" ON "col2"."column_id" = "fkc"."referenced_column_id" AND "col2"."object_id" = "fk"."referenced_object_id" query: SELECT "TABLE_CATALOG", "TABLE_SCHEMA", "COLUMN_NAME", "TABLE_NAME" FROM "app"."INFORMATION_SCHEMA"."COLUMNS" WHERE COLUMNPROPERTY(object_id("TABLE_CATALOG" + '.' + "TABLE_SCHEMA" + '.' + "TABLE_NAME"), "COLUMN_NAME", 'IsIdentity') = 1 AND "TABLE_SCHEMA" IN ('dbo') query: SELECT "NAME", "COLLATION_NAME" FROM "sys"."databases" query: SELECT 'app' AS "TABLE_CATALOG", "s"."name" AS "TABLE_SCHEMA", "t"."name" AS "TABLE_NAME", "ind"."name" AS "INDEX_NAME", "col"."name" AS "COLUMN_NAME", "ind"."is_unique" AS "IS_UNIQUE", "ind"."filter_definition" as "CONDITION" FROM "app"."sys"."indexes" "ind" INNER JOIN "app"."sys"."index_columns" "ic" ON "ic"."object_id" = "ind"."object_id" AND "ic"."index_id" = "ind"."index_id" INNER JOIN "app"."sys"."columns" "col" ON "col"."object_id" = "ic"."object_id" AND "col"."column_id" = "ic"."column_id" INNER JOIN "app"."sys"."tables" "t" ON "t"."object_id" = "ind"."object_id" INNER JOIN "app"."sys"."schemas" "s" ON "s"."schema_id" = "t"."schema_id" WHERE "ind"."is_primary_key" = 0 AND "ind"."is_unique_constraint" = 0 AND "t"."is_ms_shipped" = 0 query: CREATE INDEX "IDX_GROUP" ON "grouping" ("name") query: BEGIN TRANSACTION query: INSERT INTO "grouping"("name") OUTPUT INSERTED."id" VALUES (#0), (#1) -- PARAMETERS: [{"value":"classification","type":"nvarchar","params":[]},{"value":"categorization","type":"nvarchar","params":[]}] query failed: INSERT INTO "grouping"("name") OUTPUT INSERTED."id" VALUES (#0), (#1) -- PARAMETERS: [{"value":"classification","type":"nvarchar","params":[]},{"value":"categorization","type":"nvarchar","params":[]}] error: { RequestError: Timeout: Request failed to complete in 15000ms at Request.tds.Request.err [as userCallback] (C:\Users\me\Workspace\app\api\node_modules\mssql\lib\tedious.js:629:19) at Request.callback (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\request.js:37:27) at Connection.message (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:2136:24) at Connection.dispatchEvent (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:1084:36) at MessageIO.messageIo.on (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:984:14) at MessageIO.emit (events.js:189:13) at MessageIO.EventEmitter.emit (domain.js:441:20) at Message.message.on (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\message-io.js:32:14) at Message.emit (events.js:194:15) at Message.EventEmitter.emit (domain.js:441:20) code: 'ETIMEOUT', number: 'ETIMEOUT', state: undefined, originalError: { RequestError: Timeout: Request failed to complete in 15000ms at RequestError (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\errors.js:32:12) at Connection.message (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:2136:33) at Connection.dispatchEvent (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:1084:36) at MessageIO.messageIo.on (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:984:14) at MessageIO.emit (events.js:189:13) at MessageIO.EventEmitter.emit (domain.js:441:20) at Message.message.on (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\message-io.js:32:14) at Message.emit (events.js:194:15) at Message.EventEmitter.emit (domain.js:441:20) at endReadableNT (C:\Users\me\Workspace\app\api\node_modules\tedious\node_modules\readable-stream\lib_stream_readable.js:1077:12) message: 'Timeout: Request failed to complete in 15000ms', code: 'ETIMEOUT' }, name: 'RequestError', precedingErrors: [] } query: ROLLBACK query: ROLLBACK Error during migration run: { QueryFailedError: Error: Timeout: Request failed to complete in 15000ms at new QueryFailedError (C:\Users\me\Workspace\app\api\src\error\QueryFailedError.ts:9:9) at C:\Users\me\Workspace\app\api\src\driver\sqlserver\SqlServerQueryRunner.ts:221:37 at _query (C:\Users\me\Workspace\app\api\node_modules\mssql\lib\base.js:1346:25) at Request.tds.Request.err [as userCallback] (C:\Users\me\Workspace\app\api\node_modules\mssql\lib\tedious.js:671:15) at Request.callback (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\request.js:37:27) at Connection.message (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:2136:24) at Connection.dispatchEvent (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:1084:36) at MessageIO.messageIo.on (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:984:14) at MessageIO.emit (events.js:189:13) at MessageIO.EventEmitter.emit (domain.js:441:20) message: 'Error: Timeout: Request failed to complete in 15000ms', code: 'ETIMEOUT', number: 'ETIMEOUT', state: undefined, originalError: { RequestError: Timeout: Request failed to complete in 15000ms at RequestError (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\errors.js:32:12) at Connection.message (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:2136:33) at Connection.dispatchEvent (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:1084:36) at MessageIO.messageIo.on (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\connection.js:984:14) at MessageIO.emit (events.js:189:13) at MessageIO.EventEmitter.emit (domain.js:441:20) at Message.message.on (C:\Users\me\Workspace\app\api\node_modules\tedious\lib\message-io.js:32:14) at Message.emit (events.js:194:15) at Message.EventEmitter.emit (domain.js:441:20) at endReadableNT (C:\Users\me\Workspace\app\api\node_modules\tedious\node_modules\readable-stream\lib_stream_readable.js:1077:12) message: 'Timeout: Request failed to complete in 15000ms', code: 'ETIMEOUT' }, name: 'QueryFailedError', precedingErrors: [], query: 'INSERT INTO "grouping"("name") OUTPUT INSERTED."id" VALUES (#0), (#1)', parameters: [ MssqlParameter { value: 'classification', type: 'nvarchar', params: [] }, MssqlParameter { value: 'categorization', type: 'nvarchar', params: [] } ] } npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! app-api#0.1.0 migration:run: ts-node ./node_modules/typeorm/cli.js migration:run -c "app_engine" npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the app-api#0.1.0 migration:run script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\me\AppData\Roaming\npm-cache_logs\2019-06-04T18_56_36_972Z-debug.log Any help is greatly appreciated! Update: I found this https://github.com/typeorm/typeorm/issues/3100#issuecomment-446309812 while browsing through the previous issues and it is now working.
try to use a legacy version of the npm i mssql # 5.1.1 driver --save
Flat file to json conversion using beanIo
I am trying to parse a fixedlength flat file using beanIo to json Code: import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.Map; import org.beanio.BeanIOConfigurationException; import org.beanio.BeanReader; import org.beanio.StreamFactory; import org.junit.Test; import com.google.gson.Gson; public class EmployeeBeanIOHandlerTest { #Test public void testHandleEmployee() { // mapping pattern file String mappingPatternFile = "pattern-mapping.xml"; // data file (csv) String objectFile = "employee.csv"; // stream name defined in pattern mapping file String streamName = "empData"; Gson gson = new Gson(); BeanReader beanReader = null; Reader reader = null; StreamFactory factory = null; InputStream in = null; try { System.out.println("## RESULT FOR " + objectFile + " ##"); // create a StreamFactory factory = StreamFactory.newInstance(); // load the setting file in = this.getClass().getClassLoader() .getResourceAsStream(mappingPatternFile); // get input stream reader of object file (data file) reader = new InputStreamReader(this.getClass().getClassLoader() .getResourceAsStream(objectFile)); // load input stream to stream factory factory.load(in); beanReader = factory.createReader(streamName, reader); Map<?, ?> record = null; while ((record = (Map<?, ?>) beanReader.read()) != null) { System.out.println(beanReader.getRecordName() + ": " + gson.toJson(record)); } } catch (BeanIOConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); if (beanReader != null) { beanReader.close(); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } However the output i see: header: {"id":"Header","date":"01012013"} emp: {"lastName":"Lilik","title":"Senior Developer","hireDate":"Oct 1, 2009 12:00:00 AM","salary":7500000,"firstName":"Robertus"} emp: {"lastName":"Doe","title":"Architect","hireDate":"Jan 15, 2008 12:00:00 AM","salary":8000000,"firstName":"Jane"} emp: {"lastName":"Anderson","title":"Manager","hireDate":"Mar 18, 2006 12:00:00 AM","salary":9000000,"firstName":"Jon"} trailer: {"id":"Trailer","count":"3"} Which generates separate json object for each record found. Reference site: http://www.sourcefreak.com/2013/06/painless-flat-file-parsing-with-beanio/ Below is my requirement: I want to have a consolidated Json file. In case of duplicate record it should form an json array. I would appreciate a help on this.
This answer is based on the data and pattern-mapping.xml file found in the link provided by the OP. Data: Header,01012013 Robertus,Lilik,Senior Developer,"75,000,00",10012009 Jane,Doe,Architect,"80,000,00",01152008 Jon,Anderson,Manager,"90,000,00",03182006 Footer,3 Mapping File: This is the modified pattern-mapping.xml file. Note the use of a <group> element (myGroup) to encapsulate everything into a single group, which will force the BeanReader to read everything in one go. I also changed the maxOccurs to be 1 (one) for both the Header and the Footer records. Also, added the collection="list" attribute to theemp` record <?xml version='1.0' encoding='UTF-8' ?> <beanio xmlns="http://www.beanio.org/2012/03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd"> <stream name="empData" format="csv"> <group name="myGroup" class="map"> <record name="header" class="map" ridLength="0-2" maxOccurs="1"> <field name="id" rid="true" maxOccurs="1" literal="Header" /> <field name="date" /> </record> <record name="emp" class="map" ridLength="4-5" collection="list"> <field name="firstName" /> <field name="lastName" /> <field name="title" /> <field name="salary" type="java.math.BigDecimal" format="#,###,###,00" /> <field name="hireDate" type="java.util.Date" format="MMddyyyy" minOccurs="0" /> </record> <record name="trailer" class="map" ridLength="2" maxOccurs="1"> <field name="id" /> <field name="count" /> </record> </group> </stream> </beanio> Using the test case supplied and the modified mapping file, we get this result (reformatted by me): myGroup: { "trailer": { "count": "3", "id": "Footer" }, "header": { "date": "01012013", "id": "Header" }, "emp": [ { "firstName": "Robertus", "lastName": "Lilik", "hireDate": "Oct 1, 2009 12:00:00 AM", "title": "Senior Developer", "salary": 7500000 }, { "firstName": "Jane", "lastName": "Doe", "hireDate": "Jan 15, 2008 12:00:00 AM", "title": "Architect", "salary": 8000000 }, { "firstName": "Jon", "lastName": "Anderson", "hireDate": "Mar 18, 2006 12:00:00 AM", "title": "Manager", "salary": 9000000 } ] } Hope this helps
Swift: How to swap two axes of a four-dimensional array?
To feed the data to a function correctly, I need to swap two axes in a four-dimensional array in Swift. I'd like to know how to implement or are there any useful function for this purpose. In Python, if I use numpy.swapaxes(a, axis1, axis2), it's straightforward (https://docs.scipy.org/doc/numpy/reference/generated/numpy.swapaxes.html): def swapaxes_from_tail_to_head(nparray): nparray = np.swapaxes(nparray, 2, 3) nparray = np.swapaxes(nparray, 1, 2) nparray = np.swapaxes(nparray, 0, 1) return nparray Just note that if the array is two-dimensional, I can do like this: func prettyPrintMatrix( _ matrix:[[Int]] ) { for array in matrix { print( array ) } } func main() -> (){ print( "MatrixTranspose_Demo" ) print() let matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ], ] print( "Matrix:") prettyPrintMatrix( matrix ) print() let transpose = transposeMatrix( matrix ) print( "Transpose matrix:") prettyPrintMatrix( transpose ) print() } func transposeMatrix( _ matrix:[[Int]] ) -> [[Int]] { var result = [[Int]]( repeating: [Int]( repeating: 0, count: matrix.count ), count: matrix[ 0 ].count ) for i in 0 ..< matrix.count { for k in 0 ..< matrix[ 0 ].count { result[ k ][ i ] = matrix[ i ][ k ] } } return result } main() // Reference: http://www.runswiftlang.com/ If you have any ideas how to do swapaxes for four-dimentional (or more) array, please let me know. I appreciate your help.
The simplest way is to hard code everything like the followings: internal func swapaxesOfFlatArray4d<T>(originalArray: [T], axis1: Int, axis2: Int, dimensionOfArray: [Int]) -> [T] { assert(axis1 != axis2) assert(dimensionOfArray.count == 4) assert(axis1 < dimensionOfArray.count) assert(axis2 < dimensionOfArray.count) assert(originalArray.count == dimensionOfArray.reduce(1, *)) assert((T.self == Float.self) || (T.self == Double.self)) var newArray = Array<T>() if ((axis1 == 0 && axis2 == 1) || (axis1 == 1 && axis2 == 0)) { for j in 0..<dimensionOfArray[1] { for i in 0..<dimensionOfArray[0] { for k in 0..<dimensionOfArray[2] { for l in 0..<dimensionOfArray[3] { newArray.append(originalArray[i*dimensionOfArray[1]*dimensionOfArray[2]*dimensionOfArray[3] + j*dimensionOfArray[2]*dimensionOfArray[3] + k*dimensionOfArray[3] + l]) } } } } } else if ((axis1 == 0 && axis2 == 2) || (axis1 == 2 && axis2 == 0)) { for k in 0..<dimensionOfArray[2] { for j in 0..<dimensionOfArray[1] { for i in 0..<dimensionOfArray[0] { for l in 0..<dimensionOfArray[3] { newArray.append(originalArray[i*dimensionOfArray[1]*dimensionOfArray[2]*dimensionOfArray[3] + j*dimensionOfArray[2]*dimensionOfArray[3] + k*dimensionOfArray[3] + l]) } } } } } else if ((axis1 == 0 && axis2 == 3) || (axis1 == 3 && axis2 == 0)) { for l in 0..<dimensionOfArray[3] { for j in 0..<dimensionOfArray[1] { for k in 0..<dimensionOfArray[2] { for i in 0..<dimensionOfArray[0] { newArray.append(originalArray[i*dimensionOfArray[1]*dimensionOfArray[2]*dimensionOfArray[3] + j*dimensionOfArray[2]*dimensionOfArray[3] + k*dimensionOfArray[3] + l]) } } } } } else if ((axis1 == 1 && axis2 == 2) || (axis1 == 2 && axis2 == 1)) { for i in 0..<dimensionOfArray[0] { for k in 0..<dimensionOfArray[2] { for j in 0..<dimensionOfArray[1] { for l in 0..<dimensionOfArray[3] { newArray.append(originalArray[i*dimensionOfArray[1]*dimensionOfArray[2]*dimensionOfArray[3] + j*dimensionOfArray[2]*dimensionOfArray[3] + k*dimensionOfArray[3] + l]) } } } } } else if ((axis1 == 1 && axis2 == 3) || (axis1 == 3 && axis2 == 1)) { for i in 0..<dimensionOfArray[0] { for l in 0..<dimensionOfArray[3] { for k in 0..<dimensionOfArray[2] { for j in 0..<dimensionOfArray[1] { newArray.append(originalArray[i*dimensionOfArray[1]*dimensionOfArray[2]*dimensionOfArray[3] + j*dimensionOfArray[2]*dimensionOfArray[3] + k*dimensionOfArray[3] + l]) } } } } } else if ((axis1 == 2 && axis2 == 3) || (axis1 == 3 && axis2 == 2)) { for i in 0..<dimensionOfArray[0] { for j in 0..<dimensionOfArray[1] { for l in 0..<dimensionOfArray[3] { for k in 0..<dimensionOfArray[2] { newArray.append(originalArray[i*dimensionOfArray[1]*dimensionOfArray[2]*dimensionOfArray[3] + j*dimensionOfArray[2]*dimensionOfArray[3] + k*dimensionOfArray[3] + l]) } } } } } else { fatalError("Didn't match all the case") } return newArray } To decrease the amount of the code, you can a class to change the order of for loops. Change order of for loops?
It is fairly easy to make a transpose function for a 2d matrix: func transpose<T>(_ matrix:[[T]]) -> [[T]] { return matrix.reduce([[T]]()) { zip($0,$1).map{$0+[$1]} + $0.dropFirst($1.count) + $1.dropFirst($0.count).map{[$0]} } } Once you have that, all other transpositions of any dimensions can be converted to a series of 2d transposes on consecutive dimensions (essentially swapping them) let array12 = (1...4).map{ n in (1...4).map{n*10 + $0} } let array123 = (1...4).map{ n in array12.map{ $0.map{ n*100 + $0 } } } let array1234 = (1...4).map{ n in array123.map{ $0.map{ $0.map{ n*1000 + $0 } } } } let array2134 = transpose(array1234) // swap dimensions 1 and 2 let array1324 = array1234.map{transpose($0)} // swap dimensions 2 and 3 let array1243 = array1234.map{$0.map{transpose($0)}} // swap dimensions 3 and 4 Reusing these 3 basic dimension swaps, all other combinations of the 4 dimensions are possible. let array2143 = array2134.map{$0.map{transpose($0)}} let array2413 = array2143.map{transpose($0)} let array4213 = transpose(array2413) let array4231 = array4213.map{$0.map{transpose($0)}} let array4321 = array4231.map{transpose($0)} // and so on ... To help test this, I made a 4d print function for the matrix that I used in the playground: It prints in row column order so you get dimension one as a vertical group of "blocks", dimension 2 as a column of blocks, dimension 3 is rows of each block and dimension 4 is the columns of each block: func print4d<T>(_ matrix4d:[[[[T]]]]) { var lines:[String] = [] for d1 in matrix4d { lines = [] for (indent,d2) in d1.enumerated() { let indentWidth = indent * 80 var lineNumber = 0 let blankLine = "".padding(toLength:indentWidth, withPad:" ", startingAt:0) for d34 in d2 { while lines.count <= lineNumber { lines.append(blankLine) } lines[lineNumber] = lines[lineNumber] .padding(toLength:indentWidth, withPad:" ", startingAt:0) + " \(d34)" lineNumber += 1 } for index in lines.indices { while lines[index].contains(" ") { lines[index] = lines[index].replacingOccurrences(of: " ", with: " ") } } } lines.forEach{ print($0) } print("") } } 4d matrix : d1, d2, d3, d4 =========================== [1111, 1112, 1113, 1114] [1211, 1212, 1213, 1214] [1311, 1312, 1313, 1314] [1411, 1412, 1413, 1414] [1121, 1122, 1123, 1124] [1221, 1222, 1223, 1224] [1321, 1322, 1323, 1324] [1421, 1422, 1423, 1424] [1131, 1132, 1133, 1134] [1231, 1232, 1233, 1234] [1331, 1332, 1333, 1334] [1431, 1432, 1433, 1434] [1141, 1142, 1143, 1144] [1241, 1242, 1243, 1244] [1341, 1342, 1343, 1344] [1441, 1442, 1443, 1444] [2111, 2112, 2113, 2114] [2211, 2212, 2213, 2214] [2311, 2312, 2313, 2314] [2411, 2412, 2413, 2414] [2121, 2122, 2123, 2124] [2221, 2222, 2223, 2224] [2321, 2322, 2323, 2324] [2421, 2422, 2423, 2424] [2131, 2132, 2133, 2134] [2231, 2232, 2233, 2234] [2331, 2332, 2333, 2334] [2431, 2432, 2433, 2434] [2141, 2142, 2143, 2144] [2241, 2242, 2243, 2244] [2341, 2342, 2343, 2344] [2441, 2442, 2443, 2444] [3111, 3112, 3113, 3114] [3211, 3212, 3213, 3214] [3311, 3312, 3313, 3314] [3411, 3412, 3413, 3414] [3121, 3122, 3123, 3124] [3221, 3222, 3223, 3224] [3321, 3322, 3323, 3324] [3421, 3422, 3423, 3424] [3131, 3132, 3133, 3134] [3231, 3232, 3233, 3234] [3331, 3332, 3333, 3334] [3431, 3432, 3433, 3434] [3141, 3142, 3143, 3144] [3241, 3242, 3243, 3244] [3341, 3342, 3343, 3344] [3441, 3442, 3443, 3444] [4111, 4112, 4113, 4114] [4211, 4212, 4213, 4214] [4311, 4312, 4313, 4314] [4411, 4412, 4413, 4414] [4121, 4122, 4123, 4124] [4221, 4222, 4223, 4224] [4321, 4322, 4323, 4324] [4421, 4422, 4423, 4424] [4131, 4132, 4133, 4134] [4231, 4232, 4233, 4234] [4331, 4332, 4333, 4334] [4431, 4432, 4433, 4434] [4141, 4142, 4143, 4144] [4241, 4242, 4243, 4244] [4341, 4342, 4343, 4344] [4441, 4442, 4443, 4444] 4d matrix : d2, d1, d3, d4 =========================== [1111, 1112, 1113, 1114] [2111, 2112, 2113, 2114] [3111, 3112, 3113, 3114] [4111, 4112, 4113, 4114] [1121, 1122, 1123, 1124] [2121, 2122, 2123, 2124] [3121, 3122, 3123, 3124] [4121, 4122, 4123, 4124] [1131, 1132, 1133, 1134] [2131, 2132, 2133, 2134] [3131, 3132, 3133, 3134] [4131, 4132, 4133, 4134] [1141, 1142, 1143, 1144] [2141, 2142, 2143, 2144] [3141, 3142, 3143, 3144] [4141, 4142, 4143, 4144] [1211, 1212, 1213, 1214] [2211, 2212, 2213, 2214] [3211, 3212, 3213, 3214] [4211, 4212, 4213, 4214] [1221, 1222, 1223, 1224] [2221, 2222, 2223, 2224] [3221, 3222, 3223, 3224] [4221, 4222, 4223, 4224] [1231, 1232, 1233, 1234] [2231, 2232, 2233, 2234] [3231, 3232, 3233, 3234] [4231, 4232, 4233, 4234] [1241, 1242, 1243, 1244] [2241, 2242, 2243, 2244] [3241, 3242, 3243, 3244] [4241, 4242, 4243, 4244] [1311, 1312, 1313, 1314] [2311, 2312, 2313, 2314] [3311, 3312, 3313, 3314] [4311, 4312, 4313, 4314] [1321, 1322, 1323, 1324] [2321, 2322, 2323, 2324] [3321, 3322, 3323, 3324] [4321, 4322, 4323, 4324] [1331, 1332, 1333, 1334] [2331, 2332, 2333, 2334] [3331, 3332, 3333, 3334] [4331, 4332, 4333, 4334] [1341, 1342, 1343, 1344] [2341, 2342, 2343, 2344] [3341, 3342, 3343, 3344] [4341, 4342, 4343, 4344] [1411, 1412, 1413, 1414] [2411, 2412, 2413, 2414] [3411, 3412, 3413, 3414] [4411, 4412, 4413, 4414] [1421, 1422, 1423, 1424] [2421, 2422, 2423, 2424] [3421, 3422, 3423, 3424] [4421, 4422, 4423, 4424] [1431, 1432, 1433, 1434] [2431, 2432, 2433, 2434] [3431, 3432, 3433, 3434] [4431, 4432, 4433, 4434] [1441, 1442, 1443, 1444] [2441, 2442, 2443, 2444] [3441, 3442, 3443, 3444] [4441, 4442, 4443, 4444] 4d matrix : d1, d2, d4, d3 =========================== [1111, 1121, 1131, 1141] [2111, 2121, 2131, 2141] [3111, 3121, 3131, 3141] [4111, 4121, 4131, 4141] [1112, 1122, 1132, 1142] [2112, 2122, 2132, 2142] [3112, 3122, 3132, 3142] [4112, 4122, 4132, 4142] [1113, 1123, 1133, 1143] [2113, 2123, 2133, 2143] [3113, 3123, 3133, 3143] [4113, 4123, 4133, 4143] [1114, 1124, 1134, 1144] [2114, 2124, 2134, 2144] [3114, 3124, 3134, 3144] [4114, 4124, 4134, 4144] [1211, 1221, 1231, 1241] [2211, 2221, 2231, 2241] [3211, 3221, 3231, 3241] [4211, 4221, 4231, 4241] [1212, 1222, 1232, 1242] [2212, 2222, 2232, 2242] [3212, 3222, 3232, 3242] [4212, 4222, 4232, 4242] [1213, 1223, 1233, 1243] [2213, 2223, 2233, 2243] [3213, 3223, 3233, 3243] [4213, 4223, 4233, 4243] [1214, 1224, 1234, 1244] [2214, 2224, 2234, 2244] [3214, 3224, 3234, 3244] [4214, 4224, 4234, 4244] [1311, 1321, 1331, 1341] [2311, 2321, 2331, 2341] [3311, 3321, 3331, 3341] [4311, 4321, 4331, 4341] [1312, 1322, 1332, 1342] [2312, 2322, 2332, 2342] [3312, 3322, 3332, 3342] [4312, 4322, 4332, 4342] [1313, 1323, 1333, 1343] [2313, 2323, 2333, 2343] [3313, 3323, 3333, 3343] [4313, 4323, 4333, 4343] [1314, 1324, 1334, 1344] [2314, 2324, 2334, 2344] [3314, 3324, 3334, 3344] [4314, 4324, 4334, 4344] [1411, 1421, 1431, 1441] [2411, 2421, 2431, 2441] [3411, 3421, 3431, 3441] [4411, 4421, 4431, 4441] [1412, 1422, 1432, 1442] [2412, 2422, 2432, 2442] [3412, 3422, 3432, 3442] [4412, 4422, 4432, 4442] [1413, 1423, 1433, 1443] [2413, 2423, 2433, 2443] [3413, 3423, 3433, 3443] [4413, 4423, 4433, 4443] [1414, 1424, 1434, 1444] [2414, 2424, 2434, 2444] [3414, 3424, 3434, 3444] [4414, 4424, 4434, 4444] 4d matrix : d1, d3, d2, d4 =========================== [1111, 1112, 1113, 1114] [1121, 1122, 1123, 1124] [1131, 1132, 1133, 1134] [1141, 1142, 1143, 1144] [2111, 2112, 2113, 2114] [2121, 2122, 2123, 2124] [2131, 2132, 2133, 2134] [2141, 2142, 2143, 2144] [3111, 3112, 3113, 3114] [3121, 3122, 3123, 3124] [3131, 3132, 3133, 3134] [3141, 3142, 3143, 3144] [4111, 4112, 4113, 4114] [4121, 4122, 4123, 4124] [4131, 4132, 4133, 4134] [4141, 4142, 4143, 4144] [1211, 1212, 1213, 1214] [1221, 1222, 1223, 1224] [1231, 1232, 1233, 1234] [1241, 1242, 1243, 1244] [2211, 2212, 2213, 2214] [2221, 2222, 2223, 2224] [2231, 2232, 2233, 2234] [2241, 2242, 2243, 2244] [3211, 3212, 3213, 3214] [3221, 3222, 3223, 3224] [3231, 3232, 3233, 3234] [3241, 3242, 3243, 3244] [4211, 4212, 4213, 4214] [4221, 4222, 4223, 4224] [4231, 4232, 4233, 4234] [4241, 4242, 4243, 4244] [1311, 1312, 1313, 1314] [1321, 1322, 1323, 1324] [1331, 1332, 1333, 1334] [1341, 1342, 1343, 1344] [2311, 2312, 2313, 2314] [2321, 2322, 2323, 2324] [2331, 2332, 2333, 2334] [2341, 2342, 2343, 2344] [3311, 3312, 3313, 3314] [3321, 3322, 3323, 3324] [3331, 3332, 3333, 3334] [3341, 3342, 3343, 3344] [4311, 4312, 4313, 4314] [4321, 4322, 4323, 4324] [4331, 4332, 4333, 4334] [4341, 4342, 4343, 4344] [1411, 1412, 1413, 1414] [1421, 1422, 1423, 1424] [1431, 1432, 1433, 1434] [1441, 1442, 1443, 1444] [2411, 2412, 2413, 2414] [2421, 2422, 2423, 2424] [2431, 2432, 2433, 2434] [2441, 2442, 2443, 2444] [3411, 3412, 3413, 3414] [3421, 3422, 3423, 3424] [3431, 3432, 3433, 3434] [3441, 3442, 3443, 3444] [4411, 4412, 4413, 4414] [4421, 4422, 4423, 4424] [4431, 4432, 4433, 4434] [4441, 4442, 4443, 4444] 4d matrix : d4, d2, d3, d1 =========================== [1111, 2111, 3111, 4111] [1211, 2211, 3211, 4211] [1311, 2311, 3311, 4311] [1411, 2411, 3411, 4411] [1121, 2121, 3121, 4121] [1221, 2221, 3221, 4221] [1321, 2321, 3321, 4321] [1421, 2421, 3421, 4421] [1131, 2131, 3131, 4131] [1231, 2231, 3231, 4231] [1331, 2331, 3331, 4331] [1431, 2431, 3431, 4431] [1141, 2141, 3141, 4141] [1241, 2241, 3241, 4241] [1341, 2341, 3341, 4341] [1441, 2441, 3441, 4441] [1112, 2112, 3112, 4112] [1212, 2212, 3212, 4212] [1312, 2312, 3312, 4312] [1412, 2412, 3412, 4412] [1122, 2122, 3122, 4122] [1222, 2222, 3222, 4222] [1322, 2322, 3322, 4322] [1422, 2422, 3422, 4422] [1132, 2132, 3132, 4132] [1232, 2232, 3232, 4232] [1332, 2332, 3332, 4332] [1432, 2432, 3432, 4432] [1142, 2142, 3142, 4142] [1242, 2242, 3242, 4242] [1342, 2342, 3342, 4342] [1442, 2442, 3442, 4442] [1113, 2113, 3113, 4113] [1213, 2213, 3213, 4213] [1313, 2313, 3313, 4313] [1413, 2413, 3413, 4413] [1123, 2123, 3123, 4123] [1223, 2223, 3223, 4223] [1323, 2323, 3323, 4323] [1423, 2423, 3423, 4423] [1133, 2133, 3133, 4133] [1233, 2233, 3233, 4233] [1333, 2333, 3333, 4333] [1433, 2433, 3433, 4433] [1143, 2143, 3143, 4143] [1243, 2243, 3243, 4243] [1343, 2343, 3343, 4343] [1443, 2443, 3443, 4443] [1114, 2114, 3114, 4114] [1214, 2214, 3214, 4214] [1314, 2314, 3314, 4314] [1414, 2414, 3414, 4414] [1124, 2124, 3124, 4124] [1224, 2224, 3224, 4224] [1324, 2324, 3324, 4324] [1424, 2424, 3424, 4424] [1134, 2134, 3134, 4134] [1234, 2234, 3234, 4234] [1334, 2334, 3334, 4334] [1434, 2434, 3434, 4434] [1144, 2144, 3144, 4144] [1244, 2244, 3244, 4244] [1344, 2344, 3344, 4344] [1444, 2444, 3444, 4444]
The lack of direct support for matrix operations in Swift was bothering me so I played around with array extensions and cam up with a few methods that could probably make thing a bit easier. extension Array { func asMatrix(_ axisSizes: Int ...) -> [Any] { return asMatrix(axisSizes) } func asMatrix(_ axisSizes:[Int]) -> [Any] { if count == 0 { return [] } let requiredVectorSize = axisSizes.reduce(1,*) let flatData = asVector var newArray:[Any] = flatData while newArray.count < requiredVectorSize { newArray = newArray + flatData } for axisSize in axisSizes.dropFirst().reversed() { newArray = (0..<newArray.count/axisSize) .map{($0*axisSize,($0+1)*axisSize)} .map{newArray[$0..<$1].map{$0}} } return newArray } var matrixSize:[Int] { get { return [count] + ((first as? [Any])?.matrixSize ?? []) } set { self = asVector.asMatrix(newValue) as! [Element] } } func vectorIndex(of indexes:[Int]) -> Int { return zip(matrixSize,indexes).reduce(0){ $0 * $1.0 + $1.1 } } func matrixIndex(of vectorIndex:Int) -> [Int] { var result:[Int] = [] var vectorIndex = vectorIndex for dim in matrixSize.reversed() { result.append(vectorIndex % dim) vectorIndex = vectorIndex / dim } return result.reversed() } func enumeratedMatrix() -> [([Int],Any)] { return asVector.enumerated().map{(self.matrixIndex(of:$0),$1)} } var vectorSize:Int { return matrixSize.reduce(1,*) } var asVector:[Any] { get { return (self as? [[Any]])?.reduce(Array<Any>()){$0+$1}.asVector ?? self} set { self = newValue.asMatrix(matrixSize) as! [Element] } } subscript(indexes:[Int]) -> Any { get { return indexes.reduce(self as Any){ ($0 as! [Any])[$1] } } set { if indexes.count == 1 { self[indexes.first!] = newValue as! Element } else { var subArray = self[indexes.first!] as! Array<Any> let subIndexes:[Int] = indexes.dropFirst().map{$0} subArray[subIndexes] = newValue self[indexes.first!] = subArray as! Element } } } func transposedMatrix(_ dim1:Int=0, _ dim2:Int=1) -> [Any] { if dim1 == dim2 { return self } var transposedSizes = matrixSize swap(&transposedSizes[dim1],&transposedSizes[dim2]) var indexMap = (0..<transposedSizes.count).map{$0} swap(&indexMap[dim1],&indexMap[dim2]) let mapping = (0..<vectorSize) .map{($0, matrixIndex(of:$0))} .map{(vi,mi) in (vi,indexMap.map{mi[$0]})} .map{(vi,mi) in (vi,self.vectorIndex(of:mi)) } var flatData = asVector return mapping .sorted{$0.1 < $1.1} .map{flatData[$0.0]} .asMatrix(transposedSizes) } } The only issue remaining is that I had to use type erasure so these matrices are treated as arrays or Any and require some type casting for actual use. Nevertheless, it is easier to manipulate them: // initialized from vectors: // let squareNumbers = (0..<64).map{$0} var chessBoard = squareNumbers.asMatrix(8,8) // swapping axes // let m4Dim4842 = [0].asMatrix(4,8,4,2) // 1 element vector is repeated to fill content let m4Dim2844 = m4Dim4842.transposedMatrix(3,0) // swapped dimensions 0 and 3 // double brackets to access elements // let queenPos = chessBoard[[4,0]] as! Int chessBoard[[4,0]] = queenPos // enumeration to traverse all elements // (and data assignment using a 1d vector) chessBoard.asVector = chessBoard.enumeratedMatrix().map{$0.0[0]==$0.0[1] ? 1 : 0}
HTTPPOST with JSON Object having array
below is my json object i need to pass on my server: { "PostImages": [{ "FileFormat": "jpeg", "id": "0", "FileByteArray": "LONG BYTE ARRAY OF FILE", "FileCaption": "", "VideoLink": "" },{ "FileFormat": "jpeg", "id": "0", "FileByteArray": "LONG BYTE ARRAY OF FILE", "FileCaption": "", "VideoLink": "" }], "AdminScheduleId": "0", "CategoryId": "1", "UserId": "4", "Presenter2": "0", "IdeaTitle": "gfgfdgdfgdfg", "IsImplementedByOwn": "false", "NoOfVolunteer": "0", "PostId": "0", "ClubId": "2", "Description": " Dfgdfgdf", "Presenter1": "0" } I have already done it with passing json object to server. But it create issue just because of the long byte array. See my Below working code: String res = null; HttpPost request = new HttpPost(CREATE_IDEA_URL); request.addHeader("Content-Type", "application/json; charset=utf-8"); //request.addHeader("Content-type", "application/x-www-form-urlencoded"); try { JSONArray array=new JSONArray(PostImages); // Array of Images details JSONObject jObject = new JSONObject() .put("IdeaTitle",IdeaTitle) .put("Description",Description) .put("IsImplementedByOwn",IsImplementedByOwn) .put("NoOfVolunteer",NoOfVolunteer) .put("PostId",PostId) .put("UserId",UserId) .put("ClubId",ClubId) .put("AdminScheduleId",AdminScheduleId) .put("CategoryId",categoryId) .put("Presenter1",Presenter1) .put("Presenter2",Presenter2) .put("PostImages",array); StringEntity entity = new StringEntity(jObject.toString(2), "UTF-8"); // ADD THIS LINE TO HAVE ARABIC LANGUAGE IN POST System.out.println("jObject is:::: "+jObject.toString()); request.setEntity(entity); System.out.println("====the Entity is: "+entity.toString()); DefaultHttpClient httpClient = new DefaultHttpClient(); //---------------------- if(LC5_API.getCookiesFromLC5() == null){ }else{ CookieStore cookieStore = new BasicCookieStore(); for(Cookie cook : LC5_API.getCookiesFromLC5()){ cookieStore.addCookie(cook); } ((AbstractHttpClient) httpClient).setCookieStore(cookieStore); } //------------------- HttpResponse response = httpClient.execute(request); //res = response.toString(); int resCode = response.getStatusLine().getStatusCode(); System.out.println("Our Res: "+jObject.toString()); System.out.println("Result code: "+resCode); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line=""; StringBuffer returnFromServer = new StringBuffer(); while ((line=in.readLine())!=null){ returnFromServer.append(line); } //Toast what we got from server //Toast.makeText(getApplicationContext(),returnFromServer.toString(), Toast.LENGTH_LONG).show(); System.out.println("Responce: "+returnFromServer.toString()); res = returnFromServer.toString(); if (entity != null){ entity.consumeContent(); } // Toast.makeText(getApplicationContext(),response.getStatusLine().getStatusCode()+"", Toast.LENGTH_LONG).show(); }catch(Exception e){ e.printStackTrace(); res=null; } System.out.println("Responcre is:"+res); return res;*/ } public static String Get_AdoptIdea(String PostId) { String responce = null; RestClient client = new RestClient(GET_ADOPT_IDEA); // if(LC5_API.DEBUG) Log.d("SetCookie", LC5_API.SESSION_COOKIEName + ":"+ LC5_API.SESSION_COOKIEValue+""); // client.AddHeader("Cookie", SESSION_COOKIEName+"="+LC5_API.SESSION_COOKIEValue); client.AddParam("postId", PostId); try { client.Execute(RequestMethod.GET); responce = client.getResponse(); } catch (ConnectTimeoutException e) { e.printStackTrace(); if(DEBUG)Log.d(TAG, ""); responce=null; }catch(Exception e) { responce=null; } if(DEBUG)Log.d(TAG, "Response: "+responce); return responce; Above code works perfect. But i want to send it with namevaluepair to the server. See my below code: String res = null; try { HttpClient client = new DefaultHttpClient(); String URL = CREATE_IDEA_URL; HttpPost post = new HttpPost(URL); List<NameValuePair> postParameters; postParameters = new ArrayList<NameValuePair>(); //postParameters.add(new BasicNameValuePair("PostId", PostId)); postParameters.add(new BasicNameValuePair("IdeaTitle",IdeaTitle)); postParameters.add(new BasicNameValuePair("Description",Description)); postParameters.add(new BasicNameValuePair("IsImplementedByOwn",IsImplementedByOwn)); postParameters.add(new BasicNameValuePair("NoOfVolunteer",NoOfVolunteer)); postParameters.add(new BasicNameValuePair("PostId",PostId)); postParameters.add(new BasicNameValuePair("UserId",UserId)); postParameters.add(new BasicNameValuePair("ClubId",ClubId)); postParameters.add(new BasicNameValuePair("AdminScheduleId",AdminScheduleId)); postParameters.add(new BasicNameValuePair("CategoryId",categoryId)); postParameters.add(new BasicNameValuePair("Presenter1",Presenter1)); postParameters.add(new BasicNameValuePair("Presenter2",Presenter2)); //postParameters.add(new BasicNameValuePair("PostImages",ctx.getString(R.string.json_object_test))); JSONArray array = new JSONArray(PostImages); List<NameValuePair> param = new ArrayList<NameValuePair>(); for(int i=0; i<array.length();i++){ ImagesData img = new ImagesData(); String video = array.getJSONObject(i).getString("VideoLink").equalsIgnoreCase("") ? "xyz" : array.getJSONObject(i).getString("VideoLink"); System.out.println("VIDEO:: "+video); param.add(new BasicNameValuePair("id", array.getJSONObject(i).getString("id"))); param.add(new BasicNameValuePair("FileCaption", array.getJSONObject(i).getString("FileCaption"))); param.add(new BasicNameValuePair("FileFormat", array.getJSONObject(i).getString("FileFormat"))); param.add(new BasicNameValuePair("VideoLink", video)); param.add(new BasicNameValuePair("FileByteArray", array.getJSONObject(i).getString("FileByteArray"))); postParameters.add(new BasicNameValuePair("PostImages",param.toString())); } post.setEntity(new UrlEncodedFormEntity(postParameters)); post.setHeader("Content-Type", "application/json; charset=utf-8"); if(LC5_API.DEBUG) Log.d("REQUEST FROM REST:", "The requested String is:"+CREATE_IDEA_URL+postParameters); //-------------------------------- if(LC5_API.getCookiesFromLC5() == null){ }else{ CookieStore cookieStore = new BasicCookieStore(); //------------------------------------------- for(Cookie cook : LC5_API.getCookiesFromLC5()){ cookieStore.addCookie(cook); } ((AbstractHttpClient) client).setCookieStore(cookieStore); } //--------------------------------- HttpResponse responsePost = client.execute(post); HttpEntity resEntityPost = responsePost.getEntity(); if (resEntityPost != null) { String response=EntityUtils.toString(resEntityPost); res = response; } } catch (Exception e) {e.printStackTrace();} return res; Now, when i passes values with above code it gives me Invalid JSON Primitive: IdeaTitle. Everything is fine but not getting proper response. Please help me for how to pass json object having json array. FYI, postImages is my json array with some details. Please help me.
Is there any REST service available in Salesforce to Convert Leads into Accounts?
We have to convert Leads to accounts via REST -OAuth calls. We are able to create, update(Edit) and Detail Lead fields but not able to convert them. We found the same is possible via SOAP API but we are following REST OAuth only.
Yes and we resolved this by creating an Apex class for REST call. Sample code is this - #RestResource(urlMapping='/Lead/*') global with sharing class RestLeadConvert { #HttpGet global static String doGet() { String ret = 'fail'; RestRequest req = RestContext.request; RestResponse res = RestContext.response; String leadId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(leadId); LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; lc.setConvertedStatus(convertStatus.MasterLabel); Database.LeadConvertResult lcr ; try{ lcr = Database.convertLead(lc); system.debug('*****lcr.isSuccess()'+lcr.isSuccess()); ret = 'ok'; } catch(exception ex){ system.debug('***NOT CONVERTED**'); } return ret; } } And you can use this call by <Your Instance URL>/services/apexrest/Lead/<LeadId> This test will give you around 93% of coverage. #isTest public class RestLeadConvertTest{ static testMethod void testHttpGet() { Lead l = new Lead(); l.FirstName = 'First'; l.LastName = 'Last'; l.Company = 'Unit Test'; insert l; Test.startTest(); RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = '/Lead/' + l.Id; req.httpMethod = 'GET'; RestContext.request = req; RestContext.response= res; RestLeadConvert.doGet(); Test.stopTest(); } }
You can construct a one-off SOAP request to convert a lead and use the same OAuth token that you already have for the REST API. The request body should look like: <?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ens="urn:sobject.partner.soap.sforce.com" xmlns:fns="urn:fault.partner.soap.sforce.com" xmlns:tns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap:Header> <tns:SessionHeader> <sessionId>YOUR_OAUTH_TOKEN</sessionId> </tns:SessionHeader> </soap:Header> <soap:Body> <tns:convertLead> <tns:leadConverts> <tns:leadId>YOUR_LEAD_ID</tns:leadId> <tns:convertedStatus>Closed - Converted</tns:convertedStatus> </tns:leadConverts> </tns:convertLead> </soap:Body> </soap:Envelope> curl -H 'SOAPAction: null' -H 'Content-Type: text/xml' --data BODY_FROM_ABOVE https://your-instance-url/services/Soap/u/52.0 Note that the SOAPAction header is required, even though Salesforce does not use it. The result will be returned as XML similar to: <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Header> <LimitInfoHeader> <limitInfo> <current>91</current> <limit>15000</limit> <type>API REQUESTS</type> </limitInfo> </LimitInfoHeader> </soapenv:Header> <soapenv:Body> <convertLeadResponse> <result> <accountId>0015x00002C95kMAAR</accountId> <contactId>0035x00003NjdeyAAB</contactId> <leadId>00Q5x00001tHg1tEAC</leadId> <opportunityId>0065x000025fDsWAAU</opportunityId> <success>true</success> </result> </convertLeadResponse> </soapenv:Body> </soapenv:Envelope> If you are more comfortable with JSON than XML, OneGraph provides a GraphQL API that wraps the convertLead functionality. It's best to create your own OneGraph app to get a custom app_id, but one is provided here for demonstration purposes. The GraphQL query will be: mutation ConvertLead { salesforce( auths: { salesforceOAuth: { instanceUrl: "YOUR_INSTANCE_URL" token: "YOUR_OAUTH_TOKEN" } } ) { convertLead( input: { leadConverts: [{ leadId: "YOUR_LEAD_ID" }] } ) { leadConverts { lead { id name } account { name id } contact { name id } opportunity { name id } success errors { message statusCode } } } } } Then the request will look like: curl -H 'Content-Type: application/json' 'https://serve.onegraph.com/graphql?app_id=4687c59d-8f9c-494a-ab67-896fd706cee9' --data '{"query": "QUERY_FROM_ABOVE"}' The result will be returned as JSON that looks like: { "data": { "salesforce": { "convertLead": { "leadConverts": [ { "lead": { "id": "00Q5x00001tHg1tEAC", "name": "Daniel Boone" }, "account": { "name": "Company", "id": "0015x00002C95kMAAR" }, "contact": { "name": "Daniel Boone", "id": "0035x00003NjdeyAAB" }, "opportunity": { "name": "New Opportunity", "id": "0065x000025fDsWAAU" }, "relatedPersonAccountId": null, "success": true, "errors": [] } ] } } } }