Spark #04 – Pyspark connect to MySQL

ในบทความนี้ จะกล่าวถึง การดึงข้อมูลจาก MySQL ผ่าน JDBC เพื่อนำมาใช้งานใน Spark ด้วยภาษา Python ซึ่งจะใช้ Library Pyspark ในขั้นตอนนี้ขอกล่าวเฉพาะวิธีการก่อน (รายละเอียดจะตามมาทีหลัง) สร้าง SparkSession ตั้งชื่อว่า myspark from pyspark.sql import SparkSession myspark = SparkSession \ .builder \ .appName(“Python Spark SQL basic example”) \ .config(“spark.some.config.option”, “some-value”) \ .getOrCreate() ติดต่อ MySQL และสร้าง View ชื่อ myuser myuser=myspark.read.jdbc(url=”jdbc:mysql://mysql/mysql”,table=”user”, properties={ ‘user’: ‘user1’, ‘password’: ‘123456’} ) myuser.createOrReplaceTempView(name=”myuser”) จากนั้นก็จะสามารถ Query ข้อมูลที่เก็บไว้มาใช้งานใน Spark ได้ myspark.sql(sqlQuery=”select user,host from myuser where user=’user1′”).show() ซึ่งต่อจากนี้ จะสามารถใช้ความสามารถของ Spark ซึ่งทำงานด้าน Distributed Computing ได้ดี มาปรับปรุงความเร็วในการ Query ที่ซับซ้อน เช่นการ JOIN ได้ โดยจะกล่าวในบทความต่อๆไป

Read More »

เตาะแตะไปกับ Docker ตอนที่ 12 Clone Docker Files From GitHub

จากบล็อกเรื่อง “เตาะแตะไปกับ Docker” ในตอนที่แล้ว เราได้เรียนรู้ขั้นตอนในการสร้างและรัน LDAP services อย่างละเอียด ก่อนที่ผมจะเขียนบทความในตอนนี้ผมได้นำไฟล์ทุกไฟล์นั้นขึ้นไปไว้บน GitHub เพื่อให้พวกเราไม่ต้องสร้างไฟล์เหล่านั้นเอง แต่จะทำด้วยคำสั่ง git clone ครับ เรายังคงทดสอบด้วย VM โดยตั้งค่า network เป็น NAT network เพราะว่า เราใช้ domain สมมติ คือ ldap.example.com และทดสอบในสภาพแวดล้อมที่เป็นเครือข่ายภายใน ขั้นตอนดังนี้ 1. ติดตั้ง Ubuntu 16.04 64 bit แล้วติดตั้ง docker 17.06.2-ce แล้วต้องเปลี่ยนชื่อ hostname เป็น ldap.example.com จากนั้นรีบูตเครื่อง 2. ติดตั้ง docker-compose 1.8.0-2 (ถ้ายังไม่ได้ทำ) ด้วยคำสั่งดังนี้ sudo apt install docker-compose 3. ทำการ Clone project จาก github ด้วยคำสั่ง git clone https://github.com/woonpsu/ex1-openldap.git 4. ทำคำสั่งตามนี้ cd ex1-openldap docker-compose up -d หมายเหตุ หากยังไม่มี image ชื่อ ubuntu:16.04 ก็จะใช้เวลานานสักหน่อยเมื่อรันคำสั่ง docker-compose เพราะว่าจะมีการสร้าง image ubuntu:16.04 ให้ใหม่ 5. ใช้คำสั่งเหล่านี้เพื่อตรวจสอบ docker images docker ps docker volume ls 6. เพิ่ม users ตัวอย่าง sudo apt install ldap-utils ldapadd -H ldap://ldap.example.com -f ./openldap/src/create-users.ldif -x -D “cn=admin,dc=example,dc=com” -w 123456 7. ทดสอบเข้าใช้งานเว็บจากเครื่องใน NAT network เดียวกัน หรือที่เครื่อง VM server ที่ผมได้ติดตั้ง Desktop Environment ไว้แล้ว ก็ต้องเปลี่ยนจาก text เป็น graphic ด้วยคำสั่ง $ startx เข้า browser ไปที่ http://ldap.example.com:8080/phpldapadmin/   สำหรับวิธีการทำ GitHub Repository ชื่อ ex1-openldap ของผมอย่างคร่าว ๆ คือ 1. เข้า browser ไป sign up แบบ free ที่ github.com ด้วย username ชือ woonpsu 2. สร้าง Repository ชื่อ ex1-openldap 3. ไปที่เครื่อง server ใช้คำสั่ง git clone https://github.com/woonpsu/ex1-openldap.git 4. เตรียมไฟล์ cd ex1-openldap git init 5. สร้างไฟล์ docker-compose.yml และไดเรกทอรี openldap กับ phpldapadmin ให้เสร็จ 6. ทำการส่งขึ้นไปไว้บน github git add . git commit -m “This is a first commit” git push -u origin master

Read More »

Machine Learning #01 – Python with iris dataset

ในบทความนี้ จะแนะนำวิธีการสร้างกระบวนการ Machine Learning ด้วย Python โดยใช้ iris dataset ตั้งแต่การโหลดข้อมูล, สร้าง  Model,  Cross Validation, วัด Accuracy และการนำ Model ไปใช้งาน เพื่อความสะดวกในการเรียนรู้ เราจะเลือกใช้ Anaconda ซึ่งเป็น Python Data Science Platform ซึ่งจะรวบรวมเครื่องมือ และ Library ที่จำเป็นต่อการพัฒนา โดยสามารถเลือก Download รุ่นที่เหมาะกับระบบปฏบัติการของท่านได้ที่ https://www.anaconda.com/download/ สามารถ Clone Repository ตัวอย่างทั้งหมดที่กล่าวถึงในบทความนี้ได้จาก https://github.com/nagarindkx/pythonml และ แนะนำให้ใช้งาน jupyter-notebook เพื่อสะดวกในการเรียนรู้ บทความนี้ใช้ Notebook: 01 – SVM with iris dataset.ipynb   เริ่มจาก import dataset “iris” จาก SciKit ซึ่งเป็น dataset ตัวอย่างทีดี ในการสร้างระบบ Predict ชนิดของดอกไม้ จากการป้อนค่า ความกว้างและความยาวของกลีบดอก Iris (รายละเอียดอ่านได้จาก https://en.wikipedia.org/wiki/Iris_flower_data_set) ซึ่งเป็นการวัดความกว้าง และ ความยาวของกลีบดอก ของดอก “iris” (sepal width, sepal length, petal width, petal length) ใน 3 Spicy Image Source: https://en.wikipedia.org/wiki/Iris_flower_data_set ชุด iris dataset นี้ มักจะใช้ในการเริ่มต้นเรียนรู้ กระบวนการสร้าง Machine Learning เพื่อการ Classification โดยในตัวอย่างนี้จะใช้ Support Vector Machine (SVM) โดยเมื่อสร้างและ Train Model เสร็จแล้ว สามารถนำ Model นี้ไปใช้ในการ จำแนก Species ได้ โดยการระบุ ความกว้างและความยาวดังกล่าว แล้วระบบจะตอบมาได้ว่า เป็น Species ใด ในการเริ่มต้นนี้ เราจะใช้ iris dataset ที่มาพร้อมกับ SciKit  (sklearn) ซึ่งเป็น Machine Learning Package ในภาษา Python (ซึ่งติดตั้งมาในชุดของ Anaconda เรียบร้อยแล้ว) นำเข้าข้อมูล from sklearn import datasets iris = datasets.load_iris() สำรวจข้อมูล print(iris.data) print(iris.target) print(iris.data.shape) print(iris.target.shape) ใช้งาน SVM (Support Vector Machine) สร้าง SVC (Support Vector Classification) เพื่อทำการ Training ด้วยคำสั่ง fit โดยใส่ค่า data และ target ลงไป from sklearn import svm clf = svm.SVC() clf.fit(iris.data, iris.target) ผลที่ได้คือ SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape=None, degree=3, gamma=’auto’, kernel=’rbf’, max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False) ทดลองทำการ Predict ด้วยคำสั่ง predict แล้วใส่ Array ข้อมูลลงไป print(clf.predict([[ 6.3 , 2.5, 5.,

Read More »

การใช้ GitHub Command Line #newbie

Prerequisite ต้องมี GitHub Account ก่อน ทำตามวิธีการนี้ https://help.github.com/articles/signing-up-for-a-new-github-account/ สร้าง GitHub Repository บน Web ก่อน  (ในที่นี้ จะสร้าง Repository ชื่อ mynewrepo) ซึ่งจะได้ URL มาเป็น https://github.com/your-username/mynewrepo.git สร้าง local repository mkdir mynewrepo cd mynewrepo git init git status สร้างไฟล์ใหม่ # สมมุติสร้างไฟล์ใหม่ echo “Hello World” > mynewfile.txt git add . git status ทำการ Commit git commit -m “This is a first Commit” สร้าง Connection ระหว่าง Local Folder กับ GitHub Repository git remote add origin https://github.com/your-username/mynewrepo.git git push -u origin master จากนั้น ใส่ Username/Password ของ GitHub Account ไปดูผลงานใน Repository “mynewrepo” ที่ https://github.com/your-username/mynewrepo เมื่อมีการแก้ไขไฟล์ echo “Hello New World” >> mynewfile.txt git status ทำการ เพิ่ม > Commit > Push git add . git commit -m “This is a second Commit” git push -u origin master วนไป  

Read More »

เตาะแตะไปกับ Docker ตอนที่ 11 Docker Compose (LDAP services)

เราจะมาเรียนรู้ การใช้ docker compose รัน service 2 services คือ OpenLDAP และ phpLDAPadmin ซึ่ง docker จะมองว่าการใช้ docker compose คือ เรากำลังรัน project ที่ประกอบด้วย service หลาย ๆ service ให้แน่ใจว่าอยู่ที่ home directory ให้ใช้คำสั่งนี้ $ cd สร้างไดเรกทอรีของ project สมมติตั้งชื่อว่า ex1 $ mkdir ex1 $ cd ex1 สร้างไดเรกทอรี openldap ภายในมี dockerfile และไดเรกทอรีที่เกี่ยวข้อง ทำตามบล็อกเรื่อง “เตาะแตะไปกับ Docker ตอนที่ 9 Dockerfile (OpenLDAP)” สร้างไดเรกทอรี phpldapadmin ภายในมี dockerfile และไดเรกทอรีที่เกี่ยวข้อง ทำตามบล็อกเรื่อง “เตาะแตะไปกับ Docker ตอนที่ 10 Dockerfile (phpLDAPadmin)” สร้างไฟล์ docker-compose.yml ด้วยเอดิเตอร์ที่ถนัด เช่น vi หรือ nano ก็ได้ $ vi docker-compose.yml version: ‘2’ services: openldap: build: ./openldap container_name: openldap volumes: – ldapdatavol:/var/lib/ldap – ldapconfigvol:/etc/ldap/slapd.d ports: – “389:389” – “636:636” restart: always phpldapadmin: build: ./phpldapadmin container_name: phpldapadmin environment: HNAME: “openldap” ports: – “8080:80” depends_on: – openldap restart: always volumes: ldapdatavol: external: false ldapconfigvol: external: false อธิบายได้ดังนี้ ไฟล์ docker-compose.yml นี้ จะมี service แรกคือ openldap จะ build image จากไดเรกทอรี ./openldap เมื่อรันเป็น container จะตั้งชื่อว่า openldap โดยมีที่เก็บข้อมูลถาวรคือ ldapdatavol จะ mapped ไปยัง /var/lib/ldap ใน container และที่เก็บคอนฟิก ldapconfigvol จะ mapped ไปยัง /etc/ldap/slapd.d ใน container เช่นเดียวกัน โดย Host และ container เปิด port ตรงกัน คือ เปิด port TCP 636(LDAPS) และ 389(LDAP) และ container นี้จะทำงานทุกครั้งเมื่อเปิดเครื่อง ต่อมา service ที่สองคือ phpldapadmin จะ build image จากไดเรกทอรี ./phpldapadmin เมื่อรันเป็น container จะตั้งชื่อว่า phpldapadmin มีการตั้งค่าตัวแปร HNAME เป็นชื่อ container อันแรก คือ openldap โดยที่ Host จะเปิด port TCP 8080 ไปยัง port TCP 80

Read More »