Removing null values through Cypher Query in Pentaho "Execute SQL Step" - cypher

I am using the following Cypher Query in "Pentaho Execute SQL step" to load data into Neo4j Database from the "Excel Input" but while loading there were null/empty values in some columns in Excel.while executing the Query ,Please help me out with the issue
Query:
create(i:incident{number_1:{3} })
merge(c:company{sys_domain_1: {1} })
merge(d:parent{domain : {6} })
merge(a:alert {number: {2} })
merge(s:shelf {u_shelve: {5} })
merge(ci:config{cmdb_ci_1:{4} })
merge (d)-[:has_Company] -> (c)
merge (c)-[:has_CI] -> (ci)
merge (ci)-[:has_Incident] -> (i)
merge (i)-[:has_alert] -> (a)
merge (i)-[:has_shelf] -> (s);
I need to create a node without null values ,For example If I am merging "alert number " has null it is creating null value as node in Neo4j so I need to omit the null values from "number column"
merge(a:alert {number: {2} })
Thanks in Advance.

There are two possibilities depending on what you want.
Option 1 is to use coalesce to replace null with some default value. For example:
MERGE (a:alert { number: coalesce({2}, 1) })
will use the value 1 if the inputted parameter is null.
The other option is to use a CASE expression documentation here. This will allow you to not do the merge at all unless the value is not null.

Related

Is it possible to add index values to the rows of a postgreSQL query when expanding JSON array with json_array_elements?

I have a database of resume data in json format which I am trying to transform.
One of the sections in each jsib is work_history, this is in the form of a json array i.e.
"work_experience":[
{
"job_title":"title",
"job_description":"description"
},
{
"job_title":"title",
"job_description":"description"
}
]
I am iterating over each resume (json file) and importing this data into a new table using dbt and postgreSQL with each element of the array being a new row with the associated metadata of the resume. Here is the code I used for this
select
json_array_elements(rjt.raw_json::json -> 'data' -> 'work_experience') as we,
json_array_elements(rjt.raw_json::json -> 'data' -> 'work_experience') -> 'job_title' as "name",
rjt.uuid as uuid
from raw_json_table rjt
The last thing that I need to do is add a column that lists the index that each job came from within its individual workexperience array i.e. if a job was the third element in the array it would have a 2 in the "source_location" column. How can I generate this index such that it starts at 0 for each new json file.
Move the function to the FROM clause (where set-returning functions should be used). Then you can use with ordinality which also returns a column that indicates the index inside the array
select w.experience as we,
w.experience ->> 'job_title' as "name",
w.experience ->> 'job_description' as "description",
w.idx as "index",
rjt.uuid as uuid
from raw_json_table rjt
left join json_array_elements(rjt.raw_json::json -> 'data' -> 'work_experience') with ordinality
as w(experience, idx) on true
The left join is necessary so that rows from raw_json_table that don't contain array elements are still included.

How to store a value from source into a parameter and use it in data flow transformations?

I have a source table which just has one row:
So i stored the value from Values_per_Country into a parameter:
I want to use this parameter into my SELECT transformation(schema modifier),
but this error comes up:
Is there a way around this,so i can use values from the source tables?
You can create a Lookup activity to get the column values of source table. And then pass to the parameter in Data Flow. Finally, your expression type == 'double' && position > 0 && position <= $parameter3 will work.
Screenshot:
Expression in the below image: #activity('Lookup1').output.firstRow['Values_per_Country']

Add where clause to calculated field in cakephp 3 query

I have a query in which I want the name of a company and its employee quantity. The thing is I want to filter this result by some conditions (like employee_number > 50 etc.). My problem is that, when building the query, I don't know how to filter this result, as the condition is set over a calculated field, so when applying the condition it gives me the below
Error: `SQLSTATE[42S22]: Column not found: 1054 Unknown column 'employee_number' in 'where clause'`.
I have been trying different things, but this is what I currently have:
$query = $this->Companies->find('all')->where($conditions)->contain(['Users']);
$query
->select(['Users.name',
'Company.modified',
'employee_number' => $query->func()->count('DISTINCT(Employees.id)')])
->where('employee_number >' => 50 )
->leftJoinWith('Employees', function (\Cake\ORM\Query $query) {
return $query->where(['deleted' => 0]);
})
->group(['Employees.company_id', 'Company.id']);
First things first, you cannot refer to an aggregate in the WHERE clause, as grouping happens afterwards, hence the error, the field employee_number doesn't exist when the WHERE conditions are being applied, you have to leverage the HAVING clause instead.
Depending on the DBMS that you are using you can reference the column from the select list, MySQL for example allows that:
$query
->select([
'Users.name',
'Company.modified',
'employee_number' => $query->func()->count('DISTINCT Employees.id')
])
->leftJoinWith('Employees', function (\Cake\ORM\Query $query) {
return $query->where(['deleted' => 0]);
})
->group(['Employees.company_id', 'Company.id'])
->having(['employee_number >' => 50]);
while Postgres for example doesn't, and requires you to repeat the aggregation inside of the HAVING clause:
->having(function (
\Cake\Database\Expression\QueryExpression $exp,
\Cake\ORM\Query $query
) {
return $exp->gt($query->func()->count('DISTINCT Employees.id'), 50);
});
ps. using DISTINCT should only be necessary when you have for example multiple joins that would result in duplicate joined rows.
See also
Cookbook > Database Access & ORM > Query Builder > Aggregates - Group and Having

enrich one JSON array with data from another based on multiple conditions using dataweave 2.0

Recently, I asked this question about filter and merge two JSONs based on multiple conditions.
However, now I need mongo variable to be enriched with mysql variable data. If conditions
mongo.NUM_CONTROL = mysql.DOCNUM
mongo.NUM_ITEM = mysql.LIN_NUM_ITEM
do not match, each mongo element stays the same. But if they match, each mongo element must be enriched with mysql equivalent item.
You can use the following dataweave expression:
%dw 2.0
import * from dw::core::Arrays
output application/json
---
leftJoin(mongo, mysql, (m) -> m.NUM_CONTROL ++ "_" ++ m.NUM_ITEM, (s) -> s.DOCNUM ++ "_" ++ s.LIN_NUM_ITEM) map (item, index) ->
item.l ++ (if (item.r != null) item.r else {})
In order to left join two arrays, a common key field is needed. In this case, based on your scenario, the common keys corresponds to:
mongo: NUM_CONTROL and NUM_ITEM
mysql: DOCNUM and LIN_NUM_ITEM
So, concatenating mongo.NUM_CONTROL with mysql.NUM_ITEM will give the unique record key for mongo, and concatenating mysql.DOCNUM and mysql.LIN_NUM_ITEM will give the unique record key for mysql. Now those calculated keys can be used to left join the arrays. Using an underscore character (or whatever other non numeric character like a pipe, for example) as a separator will make sure that the correct records will be matched (if you have a mongo record with NUM_CONTROL = 1 and NUM_ITEM = 11 and a mysql record with DOCNUM = 11 and LIN_NUM_ITEM = 1, without the separator you would have the same calculated key value for mongo and mysql (111) and they would be joined incorrectly. With the separator, this won't happen as the mongo calculated key would be 1_11 and mysql calculated key 11_1).

How many rows got inserted into SQL database with ts-postgres?

I use ts-postgres and INSERT INTO to add new rows to my table.
import { Client } from 'ts-postgres';
let query = '...';
let res = await Client.query(query, [username, email]);
The result I get from Client.query is the following result:
Result {names: Array(0), rows: Array(0), status: "INSERT 0 1"}
Result {names: Array(0), rows: Array(0), status: "INSERT 0 0"}
In the first case 1 line got added, in the second 0. Do I really need to parse the status string in order to see how many rows got added?
Yep, that's something you have to deal with when working at low level (without ORM)
So here's a simple function to check for inserted rows
checkInserted(result): number {
const status = result.status.split();
return parseInt(status[status.length-1]);
}
you can customize it according to your requirements
It looks like the answer is yes, you really do need to parse the status string.
According to the Postgres protocol documentation, when the database finishes executing an insert command, it sends a CommandComplete message back to the client. The CommandComplete message consists of a byte that identifies the message type, a length, and a "tag," which is a string:
For an INSERT command, the tag is INSERT oid rows, where rows is the
number of rows inserted. oid used to be the object ID of the inserted
row if rows was 1 and the target table had OIDs, but OIDs system
columns are not supported anymore; therefore oid is always 0.
That tag is the status that you are seeing. There's nothing else in the CommandComplete message.
The Node Postgres client does include a rowCount member in result, but if you look at the code, you will see that it just parses it out of the string. The Java JDBC driver also parses the string.