import tarfile
import os


target_dir = "/home/user/test"
tar = tarfile.open("./sample.tar.gz", "w:gz")
os.chdir(target_dir)
for f in os.listdir(target_dir):
    file_name, extension = os.path.splitext(f)
    print(extension)
    if extension == ".json":
        tar.add(f)
tar.close()

os.listdir : directory 파일 search

os.path.splitext : 파일이름과 파일 확장자를 분리

`정의:

키 : 값의 쌍의 집합

":" 을 기준으로 앞에있는 one 은 key 이며 뒤에있는 1은 value 이다.

중요:

키는 고유해야 한다. (중복이 될 수 없다.)

 

주요 작업:

key와 함께 value 를 저장 -> 데이터 추가

 

del을 사용하여 "키 : 값" 쌍을 삭제할수 있다.

 

딕셔너리 정렬

A1) key값을 기준으로 정렬

sorted_dict_data 의 결과는 tuple 형태의 element로 구성된 list가 return 된다.

[('A', 165), ('C', 2), ('K', 1), ('Z', 0)]

 

이를 다시 dictionary로 변환하기 위한 작업이 아래 코드 이다. sorted_result

결과 - key값을 기준으로 오름 차순 정렬

A2) value값을 기준으로 정렬

결과 - value 값을 기준으로 오름 차순 정렬

 

 

JSON FILE READ

-> 아래코드는 JSON 파일을 READ 하는 코드인데 json.load에 의해 반환되는 변수의 데이터 타입또한 dictionary

코드
sample json 파일

 

 

 

수정 중 ( 내용 추가될 예정 - 8월4일 늦은 오후에... )

 

pandas 에서 수식(예를 들어 sum 과 같은)으로 작성된 셀 값은 nan 으로 처리 된다. (???)

 

import pandas as pd
from matplotlib import pyplot as plt


excel_file = "./test.xlsx"

data_frame = pd.read_excel(excel_file, sheet_name=0)
result = data_frame[["성별", "방문자수"]]
person = {"남자": 0, "여자": 0}
for x in result.values:
    person[x[0]] = person[x[0]] + x[1]

eng_person = {"man": person["남자"], "woman": person["여자"]}

category = [x for x in eng_person.keys()]
data = [eng_person[y] for y in category]

plt.pie(data, labels=category, autopct="%0.1f%%")
plt.show()

 

[ 엑셀 구조 ]

 

 

 

 

[ pie chart ]

 

 

 

'언어 > python' 카테고리의 다른 글

tar.gz 을 만들어보자  (0) 2020.11.22
# 기초 dictionary  (0) 2020.08.21
코로나 데이터 수집 (파이썬)  (0) 2020.07.18
네이버 python 지식인 답변  (0) 2020.06.06
21대 국회의원 선거 크롤링  (0) 2020.04.15

 

input 부분

input {
    exec {
        command  => "/usr/bin/python3 /home/kimjh/Desktop/logstashQuery/01/jsonRead.py"
        interval => 120
    }
}

python 코드 부분

 

json 파일 

=> logstash 는 ndjson 형태의 파일을 read 한다. 하지만 다음은 일반적인 json 파일이다. 이를 읽기 위해 위와 같은 파이썬 코드를 짰고 그것을 logstash의 input에 넣었다.

$ cat test_sample.json
{
        "2010": {"name2": "kim1"},
        "2011": {"name2": "kim2"},
        "2012": {"name2": "kim3"},
        "2013": {"name2": "kim4"},
        "2014": {"name2": "kim5"}
}

filter 부분

filter {
    ruby {
        code => '
                require "elasticsearch"
                require "json"
                require "yaml"

                esClient = Elasticsearch::Client.new
                actionArray = Array.new

                jsonDatas = event.get("message")
                if ! jsonDatas.nil?
                    jsonArry  = jsonDatas.split("\n")
                    jsonArry.each_with_index do |n, c|
                        #puts n.class
                        strHash = YAML.load(n)
                        hashData = {
                                    "index" => {
                                        "_index"  => "sleep4",
                                        "_id"     => c,
                                        "data" => {}
                                        }
                                    }

                        hashData["index"]["data"].merge!strHash
                        actionArray.push(hashData)
                    end
                end
                if ! actionArray.empty?
                    actionArray.each do |n|
                        p n
                    end
                    esClient.bulk body: actionArray
                end
                '
    }

    mutate {
        remove_field => ["@timestamp", "host", "command", "@version"]
    }
}

 

1) split : 문자열 분리 

=> 특정 char를 기준으로 문자열을 자르게 된다.

 

2) each_with_index : python 과 비교하자면 enumerate 와 동일하게 작동

 

3) empty?

=> 데이터가 비어있는지 확인 비어있으면 true 반환 아니면 false 반환

 

4) push

=> array에 데이터 삽입

'언어 > Ruby' 카테고리의 다른 글

ruby json 파일 읽기  (0) 2020.07.02
ruby + elasticsearch indices/ health  (0) 2020.07.02
ruby elasticsearch client  (0) 2020.05.26
cea(ver3)  (0) 2016.02.27
class  (0) 2016.02.27

#주말 토이프로젝트

 

1] 수집 사이트 

https://news.google.com/covid19/map?hl=ko&mid=%2Fm%2F02j71&gl=KR&ceid=KR%3Ako

 

코로나바이러스(코로나19) - Google 뉴스

Google 뉴스에서 코로나19의 영향을 받는 지역에 관한 지도, 통계, 뉴스를 확인하세요.

news.google.com

2] 그래프

: 사용그래프 (altair)

참고 : https://partrita.github.io/posts/altair/

그래프 view source

 

vega-editor

값 수정 테스트 

 

3] 코드 깃

https://github.com/sleep4725/Cor19

 

sleep4725/Cor19

Contribute to sleep4725/Cor19 development by creating an account on GitHub.

github.com

 

'언어 > python' 카테고리의 다른 글

# 기초 dictionary  (0) 2020.08.21
pandas + matplotlib 을 이용하여 tistory 방문 관련 pie 차트 그리기  (0) 2020.08.04
네이버 python 지식인 답변  (0) 2020.06.06
21대 국회의원 선거 크롤링  (0) 2020.04.15
pdf 변환  (0) 2019.12.18

참고) java는 잘 모릅니다. ㅋㅋㅋ

 

 

 

 

 

High level 또는 Low level을 구별짓는데 난 무슨 차이인지 아직은 명확히 모르겠다.

 

pom.xml 파일에 <dependencies> 안에 값? 을 넣어준다.

 

 

 

-> 일반적으로 자바에서 클래스 이름의 앞 머리는 대문자로 쓰는 것을 약속한다. (관습)

-> public EsTestClient() : 이부분은 생성자로써 instance 생성시 호출되면 반환타입이 없다.

    --> RestClient.builder ( new HttpHost("localhost", 9200, "http") )

       --> cluster의 node가 x개이면 x개 만큼 기술해주면 된다.

       --> localhost : node 주소

       --> 9200 : node의 tcp port 주소

       --> http : elasticsearch protocol 물론 tls/ssl 설정되면 https로 바뀌어야 함

 

-> 자바에서 인스턴스 생성시 

클래스_이름  인스턴스_이름 = new 클래스_이름();

'언어 > java' 카테고리의 다른 글

java 크롤링  (0) 2020.05.09
정올 1523 (9)  (0) 2020.05.06
정올 1338번문제 (8일차)  (2) 2020.05.03
1314 정올 (7-2)  (0) 2020.05.02
1307 정올 7일차  (0) 2020.05.02

begin

 # 예외가 발생할 가능성이 있는 문구

rescue => e

 # 적절한 예외 처리

else

 # 예외 발생없이 정상 처리

ensure

 # 예외 발생과 상관없이 처리

end

 

======================================================

json 파일을 읽어서 elasticsearch client를 셋업해보자

'언어 > Ruby' 카테고리의 다른 글

logstash ruby filter elasticsearch bulk insert  (0) 2020.07.22
ruby + elasticsearch indices/ health  (0) 2020.07.02
ruby elasticsearch client  (0) 2020.05.26
cea(ver3)  (0) 2016.02.27
class  (0) 2016.02.27

1. elasticsearch cluster 의 health 체크

: esClient.cluster.health => 응답으로 받은 데이터는 hash 형태의 리턴값

 

{"cluster_name"=>"elasticsearch", "status"=>"yellow", "timed_out"=>false, "number_of_nodes"=>1, "number_of_data_nodes"=>1, "active_primary_shards"=>15, "active_shards"=>15, "relocating_shards"=>0, "initializing_shards"=>0, "unassigned_shards"=>15, "delayed_unassigned_shards"=>0, "number_of_pending_tasks"=>0, "number_of_in_flight_fetch"=>0, "task_max_waiting_in_queue_millis"=>0, "active_shards_percent_as_number"=>50.0}

 

2. 전체 인덱스 확인

: esClient.cat.indices => 응답으로 받은 데이터는 string 형태의 리턴값

: shell 로 코드를 구성한다면 다음과 같다.

#!/bin/bash

curl -XGET localhost:9200/_cat/indices?pretty

 

3. document indexing

: shell 로 코드를 구성한다면 다음과 같다.

#!/bin/bash

curl -X POST "localhost:9200/index_data" -H "Content-Type: application/json" -d'

{

  "name": "KimJunHyeon"

}' 

 

4. delete_by_query

: 인덱스의 껍데기 (mapping)을 유지한 상태에서 모든 document를 지워야할 경우가 생긴다. 그럴경우 

delete_by_query 문을 사용한다.

 

: shell 로 코드를 구성한다면 다음과 같다.

#!/bin/bash

curl -X POST "localhost:9200/index_data/_delete_by_query" -H "Content-Type: application/json" -d'
{
  "query": {
     "match_all": {}
  }
}'

 

'언어 > Ruby' 카테고리의 다른 글

logstash ruby filter elasticsearch bulk insert  (0) 2020.07.22
ruby json 파일 읽기  (0) 2020.07.02
ruby elasticsearch client  (0) 2020.05.26
cea(ver3)  (0) 2016.02.27
class  (0) 2016.02.27

https://kin.naver.com/qna/detail.nhn?d1id=1&dirId=10402&docId=358898590&ref=me3lnk&scrollTo=answer1

require 'elasticsearch/transport'
require 'elasticsearch'

client = Elasticsearch::Client.new hosts: [
    { host: '',
      port: 9200,
      user: "",
      password: ""
    }
]

#client.index  index: 'myindex', type: 'mytype', id: 1, body: { title: 'Test' }
response = client.search index: 'myindex', body: { query: { match: { title: 'test' } } }
puts response["hits"]["hits"][0]["_source"]
require import
   

'언어 > Ruby' 카테고리의 다른 글

ruby json 파일 읽기  (0) 2020.07.02
ruby + elasticsearch indices/ health  (0) 2020.07.02
cea(ver3)  (0) 2016.02.27
class  (0) 2016.02.27
루비(ruby)상속 -> 상속 -> 상속  (0) 2016.02.26