Day01 介紹過我的選股程式是用 Python 開發,今天開始要來探討 Python 與 ES 如何合作。
由於 ES 是透過 RESTful API 進行存取、操作,所以任何 Client 端的語言都可以透過 HTTP protocol 和 ES 伺服器端進行通訊。喜歡手作溫度的碼農應該會迫不及待的想要硬幹了!揪鬥媽爹,這完全是不必要的行為, ES 已經提供了一系列的官方 Client 端支援庫 。酷! 馬上來試用 Python API。
我是利用 Docker 在本機上運行選股程式。根據 Python Elasticsearch Client 的提示,我將安裝指令新增進 Dockerfile,重建一個支援 Elasticserach 的 Docker Image。 Dockerfile 分享如下:
FROM python:3.6-alpine
RUN apk update
RUN apk add musl-dev wget git build-base libxslt-dev
RUN pip install cython
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h
RUN pip install numpy
RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && \
  tar -xvzf ta-lib-0.4.0-src.tar.gz && \
  cd ta-lib/ && \
  ./configure --prefix=/usr && \
  make && \
  make install
RUN git clone https://github.com/mrjbq7/ta-lib.git /ta-lib-py && cd ta-lib-py && python setup.py install
RUN pip install requests
RUN pip install lxml
RUN pip install pandas
RUN pip install 'elasticsearch>=7.0.0,<8.0.0' 
至於怎麼 Build & Run Image 就不在這裡騙字數了喔。
演習目標,利用 Python API 把前面幾天幹的事兒做一次,填補技術坑點:
其中的 Endpoint 在哪取得的,Day05 有介紹過。
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch("Endpoint", http_auth=('user name', 'password'))
與 Index 相關的 class 為 class elasticsearch.client.IndicesClient 。在 class elasticsearch.Elasticsearch 中已經有 IndicesClient 的 instance 可以直接使用;這也是官方文件上描述唯一可以使用 IndicesClient 的方式。
IndicesClient 的 create 方法即是用來建立 Index 用。
index_body = {
    "settings": {
        "index": { "number_of_shards": 1,  "number_of_replicas": 1 }
    },
    "mappings": {
        "properties": {
            "close" : {"type" : "float"},
            "date" : {"type" : "date"},
            "high" : {"type" : "float"},
            "low" : {"type" : "float"},
            "open" : {"type" : "float"},
            "stock_id" : {"type" : "keyword"},
            "volume" : {"type" : "integer"}
        }
  }
}
result = es.indices.create(index='history-prices-python', body=index_body)
documents = [
    {"index":{"_id" : "0003"}},
    { "stock_id":"0050", "date":"2020-09-11", "volume":2905291,"open":103.20,"high":105.35, "low":103.80, "close":104.25 },
    {"index":{"_id" : "0004"}},
    { "stock_id":"0050", "date":"2020-09-12", "volume":2232343, "open":104.20, "high":105.35, "low":102.80, "close":104.00 },
]
result = es.bulk(body=documents, index='history-prices-python')
整件事情已經簡單到我不知道該說什麼了。
寶寶在「該」了,得去幫她洗澡了,奶爸參加鐵人賽真是累得夠嗆~