Import json through python


(Julien Chloë Antoine) #1

Hello,

I am trying to import a file through this code but I am getting the following error.

Code:
26234134_931691430322178_2132873039_n!

Error:
26553240_931691493655505_697954895_n

I think it's due to my movies.txt file but I can't find the error.

I don't know what to do, I am a little bit lost. :cry:
Thanks in advance for your help ! :smile:


(David Pilato) #2

Please don't post images of text as they are hardly readable and not searchable.

Instead paste the text and format it with </> icon. Check the preview window.


(Julien Chloë Antoine) #3

ohw, sorry :slight_smile:

Here is the code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import elasticsearch
import requests
from collections import OrderedDict
import codecs
import json


#------------------------------------------------------------------------------
es=elasticsearch.Elasticsearch([{'host': 'localhost', 'port': 9200}])
moviesFile = codecs.open("movies.txt", "r", "utf-8")
movies = moviesFile.read()
moviesFile.close()
res=es.bulk(body=movies, index="movies", doc_type="movie",request_timeout=30)
print(json.dumps(res, indent=3, separators=(',', ': ')))

The error:

C:\Python27\python.exe "C:/Users/Documents/import.py"
{
   "items": [
      {
         "index": {
       `Preformatted text`   "status": 400,
            "_type": "movie",
            "_id": "1",
            "error": {
               "caused_by": {
                  "reason": "Malformed content, found extra data after parsing: START_OBJECT",
                  "type": "illegal_argument_exception"
               },
               "reason": "failed to parse",
               "type": "mapper_parsing_exception"
            },
            "_index": "movies"
         }
      }
   ],
   "errors": true,
   "took": 8
}

Process finished with exit code 0

The movie.txt file:

{ "index" : { "_index" : "movies", "_type" : "movie", "_id" : "1" } }{"rating": "8.3", "genres": ["Animation", "Adventure", "Comedy"], "release_year": "1995", "title": "Toy Story ", "short summary": "A cowboy doll is profoundly threatened and jealous when a new spaceman figure supplants him as top toy in a boy's room.", "duration": "1h 21min", "directors": ["John Lasseter"], "writers": ["John Lasseter(original story by),", "Pete Docter(original story by)"], "storyline": "A little boy named Andy loves to be in his room, playing with his toys, especially his doll named \"Woody\". But, what do the toys do when Andy is not with them, they come to life. Woody believes that he has life (as a toy) good. However, he must worry about Andy's family moving, and what Woody does not know is about Andy's birthday party. Woody does not realize that Andy's mother gave him an action figure known as Buzz Lightyear, who does not believe that he is a toy, and quickly becomes Andy's new favorite toy. Woody, who is now consumed with jealousy, tries to get rid of Buzz. Then, both Woody and Buzz are now lost. They must find a way to get back to Andy before he moves without them, but they will have to pass through a ruthless toy killer, Sid Phillips. Written by John Wiggins", "stars": ["Tom Hanks,", "Tim Allen,", "Don Rickles"], "keywords": ["rivalry", "toy", "cowboy", "jealousy", "claw crane"]}
{ "index" : { "_index" : "movies", "_type" : "movie", "_id" : "2" } }{"rating": "6.9", "genres": ["Action", "Adventure", "Family"], "release_year": "1995", "title": "Jumanji ", "short summary": "When two kids find and play a magical board game, they release a man trapped for decades in it and a host of dangers that can only be stopped by finishing the game.", "duration": "1h 44min", "directors": ["Joe Johnston"], "writers": ["Jonathan Hensleigh(screenplay),", "Greg Taylor(screenplay)"], "storyline": "After being trapped in a jungle board game for 26 years, a Man-Child wins his release from the game. But, no sooner has he arrived that he is forced to play again, and this time sets the creatures of the jungle loose on the city. Now it is up to him to stop them. Written by Joshua Davis <xtnb64a@prodigy.com>", "stars": ["Robin Williams,", "Kirsten Dunst,", "Bonnie Hunt"], "keywords": ["board game", "jungle", "magic", "trailer narrated by hal douglas", "trailer narrated by nick tate"]}

Thanks for your help :slight_smile:


(Julien Chloë Antoine) #4

Here is the mapping if you need it :slight_smile:

{
"mappings": 
	{
	
"docs": 
		{

		"_source": 
			{
			
"enabled": true

			},

		"dynamic": "strict",
	
		"properties": 
			{
	
			"title": 
				{
				
"type": "text"
				},

			"release_year":

				{

				"type" : "integer"
				
},
			"rating": 
				{
				"type":"integer"
				},
			"short summary":
				{
				"type":"text"
				},
			"directors":
				{
				"type":"text"
				},
			"writers":
				{
				"type":"text"
				},	
			"stars":
				{
				"type": "text"
				}, 
			"storyline": 
				{
				"type": "text"
				}, 
			"keywords":
				{
				"type":"keyword"
				}, 
			"genres":
				{
				"type":"keyword"
				}, 
			"duration":
				{
				"type":"keyword"
				}
			}
		}
	}

}

(David Pilato) #5

You are missing the line return after each header.


(Julien Chloë Antoine) #6

What do you mean? Could you give an exemple?
Sorry, we are new users of Elasticsearch.


(David Pilato) #7

Bulk format is explained here: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html


(system) #8

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.