Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-10-2021 09:24 PM
Hi, I use neo4j browser and i have a Q.
Can I import a csv file like this format? (no column data on top)
LOAD CSV FROM '{csv-dir}/neo4j-sample-data.csv' AS info FIELDTERMINATOR ','
CREATE (:Info { name: info[1].name, count:info[1].count. asset_id:info[1].asset_id})
01-10-2021 09:37 PM
Hello
Please check cypher query syntax for exact details on cypher syntax reference card.
Thanks
SameerG
01-11-2021 12:58 AM
Hey,
Thanks for reply and I did it.
read syntax reference. but, failed
It is so difficult to me
oh my.....
01-11-2021 03:24 AM
Hello
Please read the error carefully. After careful consideration it looks like problem is with
name: info[1].name column it is expecting a map data structure but you are supplying a string instead of Map. So please look for conversion functions from string to map on ref card that I earlier pointed to.
I hope that this will fix the issue at your end.
Sameer
01-11-2021 03:52 AM
It seems you have a json object as the value in CSV. This is not how load csv works. It converts a single row into map.
For ex:
if you have data
id, name
1,test
2,test2
the resulting map would be
{
id: 1,
name: "test"
}
In your case the map seen by db would be
{
n: "{name:CVE-2014-3539,count:1,asset_id:26...}"
}
All the values if the type cannot be inferred are treated as strings.
So "info.n" will give you access to the string. But you need convert that string to map. Even with apoc it might not be possible because this is not a valid json, as the strings and names are enclosed in quotes.
If you want this data in CSV format then it should be like this
name,count,asset_id,privilege,seq
CVE-2014-3539,1,21635,0,1
01-11-2021 09:08 AM
What you might want to try, is import as CSV, then take the string (which you want to become a JSON), and apply a String -> JSON function.
See:
01-11-2021 05:07 PM
Hi @Novice ,
this is my code, please give a try
demo.csv
n
{name:CVE-2014,count:1,asset_id:1111,privilege:0,seq:1}
{name:CVE-2015,count:12,asset_id:2222,privilege:0,seq:2}
{name:CVE-2016,count:31,asset_id:3333,privilege:0,seq:3}
Cypher Query
CALL apoc.load.csv('demo.csv' ,{skip:1,header:FALSE} )
YIELD list
with apoc.convert.toString(list) as base
WITH ['\{', '\}','\[','\]', 'name:','count:','asset_id:','privilege:','seq:', '\"'] as to_remove, base
with base, reduce(vari=base, word in to_remove | apoc.text.replace(vari,word,'')) as filtered
with apoc.convert.toStringList(split(filtered,",")) as tt
create (a:Item) set a.name = trim(tt[0]), a.count=toInteger(trim(tt[1])),a.asset_id=toInteger(trim(tt[2])),a.privilege=trim(tt[3]),a.seq=toInteger(trim(tt[4]))
Result -
match(n) return(n)
╒══════════════════════════════════════════════════════════════════════╕
│"n" │
╞══════════════════════════════════════════════════════════════════════╡
│{"name":"CVE-2014","count":1,"privilege":"0","asset_id":1111,"seq":1} │
├──────────────────────────────────────────────────────────────────────┤
│{"name":"CVE-2015","count":12,"privilege":"0","asset_id":2222,"seq":2}│
├──────────────────────────────────────────────────────────────────────┤
│{"name":"CVE-2016","count":31,"privilege":"0","asset_id":3333,"seq":3}│
└──────────────────────────────────────────────────────────────────────┘
Note -
give it a try and let me know how it goes.
All the sessions of the conference are now available online