ELK/elasticsearch

python + elasticsearch : csv => bulk json 변환

파아랑새 2019. 10. 23. 10:04

    ## csv => bulk type의 파일로 변환
    def csvFileRead(self):

        with open(self._csv_path, "r", encoding="utf-8") as csv_file:

            with open("/home/elastic/test.dir/py_test/the_planet_code/the_planet.json", "a", encoding="utf-8") as f:

                csv_reader = csv.DictReader(csv_file, delimiter=",")

                for c, r in enumerate(csv_reader):

                    h = json.dumps({"index": {"_index": "the_planet_poc_index_1", "_type": "_doc", "_id": "the_planet_" + str(c+1)}},
                                  ensure_ascii=False)
                    f.write(h + "\n")

                    e = json.dumps((dict(r)),
                                   ensure_ascii=False)
                    f.write(e + "\n")
                    time.sleep(2)

                f.close()
            csv_file.close()