2016年2月24日 星期三

How to Deploy Spark Monitor Tool - varOne

You can reference the github url : https://github.com/SparkMonitor/varOne 
Here is  the more detail I think.

First you can download from github http://sparkmonitor.github.io/varOne/varOne-0.1.0.tgz

Or on your Spark Server with "wget" command

在實際開始安裝之前,請先修改$SPARK_HOME/conf metrics.propertis
如無此檔案,請先執行



加入
*.sink.csv.class=org.apache.spark.metrics.sink.CsvSink
*.sink.csv.period=1
*.sink.csv.unit=seconds
*.sink.csv.directory=/home/hadoop/CSV_SINK
driver.source.jvm.class=org.apache.spark.metrics.source.JvmSource
executor.source.jvm.class=org.apache.spark.metrics.source.JvmSource


#注意 csv.directory路徑請自行更改至您對應的路徑

接著修改 $SPARK_HOME/conf下的 spark-defaults.conf
如無此檔案請先執行


加入
spark.eventLog.enabled true
spark.eventLog.dir  hdfs://hadoop-master:8020/Peter/eventLog





#注意  eventLog.dir 目前版本設定存放 hdfs,請修改及對應您的hdfs 目錄位置

解壓縮檔案(建立安裝在 namenode上,這樣遠端呼叫datanode上的 varOne不需要再輸入密碼,因為hadoop cluster環境上已設定過 ssh)




修改 $VARONE_HOME/conf3個檔案 varOne-env.sh,varOne-site.xml,varonedaemond
若無前兩個檔案,請自行複製一份並修改檔名
主要修改2個檔案

1.varOne-env.sh




2.varonedaemond








把你的Cluster環境中的所有hostname加入,注意一個hostname一行

上述全部修改完畢後,請將varOne目錄複製一份至每個datanode
我的習慣是先壓縮再傳送





開始啟動 varOned !
注意啟動前,事實上Spark也必須複製一份至各個node(請重複SCP動作)
因為varOne.sh 需要去讀取$SPARK_HOME/confmetrics.properties


















啟動 varOne.sh for WebUI




開啟 WebUI default會在 8080 port
hadoop-master:8080/varOne-web/index.html



2016年2月2日 星期二

Spark save data to HDFS(Yarn) and get data from HDFS to save to Phoenix+HBase- fully demo with Java

終於來到了打通任督二脈的時刻了!!!
今天要示範的是使用 Java 程式編寫一個 Person 物件,透過 Spark 將值轉入 HDFS,
再用 Spark 成功從 HDFS 取出值後,將其 Save to Phoenix (事先建立好的物件Table)
這樣的情境案例是一個 BigData 中可行的環境。
Spark 主要做為計算,YARN作為分配工作及資源
HDFS作為 Warehouse,HBase作為Storage,而 Phoenix做為快速查詢的橋樑
讓我們開始吧!
準備BigData環境:
HDFS + YARN + SPARK+ HBASE + PHOENIX
開發環境 Eclipse 加入所需Library(Spark-assembly...jar)

這些環境的建置基本上前面的文章都有提過了
今天直接把程式碼及執行的結果告訴大家!!!

1.建立程式碼如下
--我建立了2個Person物件轉成List將其填入hdfs指定的路徑<Spark會直接建目錄>

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

public class SparkSaveToHDFS {
public static class Person implements Serializable {
   private String name;
   private int age;

   public String getName() {
     return name;
   }

   public void setName(String name) {
     this.name = name;
   }

   public int getAge() {
     return age;
   }

   public void setAge(int age) {
     this.age = age;
   }
 }

public static void main(String[] args) throws Exception {
    SparkConf sparkConf = new SparkConf().setAppName("SparkSaveToHDFS");
   JavaSparkContext ctx = new JavaSparkContext(sparkConf);
  
   Person Peter = new Person();
   Peter.setName("Peter");
   Peter.setAge(30);
   Person Kevin = new Person();
   Kevin.setName("Kevin");
   Kevin.setAge(40);
   
   List<Person> personList = new ArrayList<Person>();
   personList.add(0, Peter);
   personList.add(1,Kevin);
   System.out.println("list contains Peter : " + personList.contains(Peter) + Peter.getAge());
   System.out.println("list contains Kevin : " + personList.contains(Kevin) + Kevin.getAge());
   
   JavaRDD<Person> personRdd = ctx.parallelize(personList);
   personRdd.saveAsObjectFile("hdfs://hadoop-master:8020/Peter/test");    
   
}
}

執行結果如下



#注意: 執行時 需要使用到 phoenix-hbase-client jar,在之前 Spark-shell 中時並不用加入是因為已經把這個 jar 檔加入環境變數 CLASSPATH
而在 Spark-submit 中 不使用 --driver-class-path 也是因為 CLASSPATH中已經有加入時,會造成 jar檔衝突,而改使用參數 --jars 是指 強制使用這個 jar檔

在沒有任何錯誤的情況下,可以檢查 hdfs 是否有長出了新目錄


2.我們可以使用下列程式將物件從HDFS取出並轉成 DataFrame 讀取
請修改上一支程式改為如下,記得 import SQLContext & DataFrame

public static void main(String[] args) throws Exception {
    SparkConf sparkConf = new SparkConf().setAppName("SparkSaveToHDFS");
   JavaSparkContext ctx = new JavaSparkContext(sparkConf);
   SQLContext sqlContext = new SQLContext(ctx);

   JavaRDD<Person> getPersonRdd = ctx.objectFile("hdfs://hadoop-master:8020/Peter/test");
   DataFrame schemaPeople = sqlContext.createDataFrame(getPersonRdd, Person.class);
   schemaPeople.registerTempTable("people");
   schemaPeople.printSchema();
   DataFrame people = sqlContext.sql("select * from people");
   people.show();
       
}

執行結果如下執行 command 跟之前一樣



主要是 printSchema & show 是否 work

3.上面程式我們已成功將物件轉為DataFrame,現在我們直接要 Save to Phoenix
在執行程式前,我們必須先至 Phoenix 建立 待會要 Insert 的 table

至 phoenix/bin下 執行 sqlline.py localhost(zookeeper location)

執行 DDL
create table people1(AGE INTEGER, NAME VARCHAR NOT NULL PRIMARY KEY)

Table 建立 Table後,開始修改程式,只要加入一行

程式如下

people.write().format("org.apache.phoenix.spark").mode("overwrite").option("table", "people1").option("zkUrl", "localhost").save();

一樣 Spark-submit 後,若無錯誤出現,people 該 DataFrame 已經成功 insert了

至 phoenix 查詢 結果如下



假設最後你希望透過程式把 Phoenix 的結果 select 出來,一樣可以透過 phoenix 取出,請參考前面的文章(不一定要用 JDBC若程式在 Spark 上執行的話)

恭喜成功!









2016年1月28日 星期四

Spark save(insert) to Phoenix sample code

先前學會怎麼用 Spark 取出在Phoenix的資料,現在我們來學學怎麼 save 資料(insert)至 Phoenix
,然後再學怎麼從 Table1 轉資料進入 Table2

1.首先至 $PHOENIX_HOME/bin 下 執行 sqlline.py localhost(or your zookeeper address)
使用下列 DDL Command Create table
CREATE TABLE INPUT_TABLE (id BIGINT NOT NULL PRIMARY KEY, col1 VARCHAR, col2 INTEGER);
CREATE TABLE OUTPUT_TABLE (id BIGINT NOT NULL PRIMARY KEY, col1 VARCHAR, col2 INTEGER);


2.使用 Spark-shell 準備 insert 資料嘍!
import org.apache.phoenix.spark._

val dataSet = List((4L, "4", 4), (5L, "5", 5), (6L, "6", 6))

sc.parallelize(dataSet).saveToPhoenix("INPUT_TABLE",Seq("ID","COL1","COL2"),zkUrl = Some("192.168.13.61:2181"))


 

3.回到 Phoneix 查詢結果吧!



Nice ,儲存成功!

------------------------------------------------------------------------------------------------------------------------
現在我們試著寫一段程式將 INPUT_TABLE 資料 存入  OUTPUT_TABLE 吧!

import org.apache.spark.sql._
import org.apache.phoenix.spark._

// Load INPUT_TABLE
val df = sqlContext.load("org.apache.phoenix.spark", Map("table" -> "INPUT_TABLE","zkUrl" -> "localhost"))

// Save to OUTPUT_TABLE
df.save("org.apache.phoenix.spark",SaveMode.Overwrite, Map("table" -> "OUTPUT_TABLE","zkUrl" -> "localhost"))

#注意,我這邊ZK設 localhost,是因為我 Spark-shell 此台Server同時也是 zookeeper 其中之一






至此程式執行完畢,緊接著至  Phoenix/bin下使用 sqlline.py 來觀察資料是否寫入成功



資料是否跟剛才 input_table 一樣呢! 如果一樣 那就成功嘍!

Spark call phoenix to get phoenix data with phoenix-spark plugin sample code 教學分享

先前文章我們已經示範了如何建置 Hbase & Phoenix
但是如何我要使用 Spark 要如何與 Phoenix 關聯呢?
一般來說會使用 JDBC 但今天我要示範除了JDBC的另外2個方式
此方式是 Phoenix 建議使用的!
在開始前你得先安裝 Spark,參考下面網址將測試資料建置在 Hbase
https://blogs.apache.org/phoenix/entry/spark_integration_in_apache_phoenix
to load data to phoenix hbase be sample data

如果上面網址的範例您可以成功完成,我們可以開始這次的教學了

if you successed to load data and put data to another table EMAIL_ENRON_PAGERANK
you can get count(*) is 36692
-------------------------------------------------
Now I will present to you, spark call phoenix with Apache Spark plugin
with 2 ways to get DataFrame different from jdbc connection.
Please also reference https://phoenix.apache.org/phoenix_spark.html to get RDD.
Now here we go.

First start spark-shell without using driver-class-path
[Remember] When we install phoenix, we have already add driver classpath to environment
請參考上一篇安裝教學
------------------------------------------------------------------------------------------------------------------------
範例一:使用 configuration 取得 DataFrame
$spark-shell

import org.apache.phoenix.spark._
import org.apache.hadoop.conf.Configuration

val configuration = new Configuration()

val df = sqlContext.phoenixTableAsDataFrame("EMAIL_ENRON",Seq("MAIL_FROM","MAIL_TO"),conf = configuration)

df.show(5)

結果如下圖




--------------------------------------------------------------------------------------------------------------------------
範例二:使用Data Source API 取得 DataFrame


import org.apache.phoenix.spark._

val df = sqlContext.load(
  "org.apache.phoenix.spark",
  Map("table" -> "EMAIL_ENRON", "zkUrl" -> "192.168.13.61:2181")
)

df.show(5)

結果如下圖
(#注意zkUrl是可以變化的,根據你設了幾台zk server for hbase)



結果與範例一相同呢! 

這2種方法,如果程式開發時要一致,通常建議使用Configuration方式,這樣開發人員不用記憶Zookeeper的位置,且能充份發揮Zookeeper的特性,但若是開發時想使用第二種方式也可以,但是請將所有Zookeeper的位置都得加入,像我之前建立5個Zookeeper時,就得如入5個IP address(容錯用) 


Phoenix for HBase install 教學

Apache Phoenix is a relational database layer over HBase delivered as a client-embedded JDBC driver targeting low latency queries over HBase data. Apache Phoenix takes your SQL query, compiles it into a series of HBase scans, and orchestrates the running of those scans to produce regular JDBC result sets. The table metadata is stored in an HBase table and versioned, such that snapshot queries over prior versions will automatically use the correct schema. Direct use of the HBase API, along with coprocessors and custom filters, results in performance on the order of milliseconds for small queries, or seconds for tens of millions of rows.

Download From http://www.apache.org/dyn/closer.lua/phoenix/
Be carefully, your must download a match version contrast to HBase
Because my hbase version is 1.1.2, so I download phoenix-4.6.0-HBase-1.1-bin.tar.gz

1. Unzip your phoenix


2.Move them to the place you want


3.Go to Phoenix folder to copy the phoenix-{Phoenix-version}-Hbase-{hbase-version}-server.jar
& phoenix-core-{Phoenix-version}-Hbase-{hbase-version}.jar to $HBASE_HOME/lib
You must do many times for your all hbase(region) servers.


4.Edit environment variables to add classpath for phoenix-client driver 
 this step is for client to call hbase


5.After start your hbase and zookeeper, go to $PHOENIX_HOME/bin execute ./sqlline.py localhost (I execute the command on hbase-master, because I install phoenix on it.)


6.Execute 'ctrl + d' to exit phoenix, and now you can try other zookeeper to connect to get zookeeper feature 


恭喜成功~*

2016年1月27日 星期三

HBase Fully Distributed Mode Install Step by Step

Today, let us to talk about Hbase with Fully Distributed Mode.
My environment as below download from Apache:
JDK 1.8.0_65
Hbase 1.1.2

I have one hbase master and four hbase regionServers

These server are must install hbase(including hbase master) and they are have the same configuration files.
My hbase master is also my HDFS namenode server, I deployed them on the same machine.
You also can deploy hbase matser different from hadoop namenode.

Now Let's go.

1.Download Hbase Fromhttp://www.apache.org/dyn/closer.cgi/hbase/ 
Copy File to your hbase master machine & tar it & move the folder to the place you want





2.Go to your hbase conf file and edit hbase-site.xml, add content as below for your environment


3.Edit hbase-env.sh & add content as below, the hbase-manages-zk attribute means start-hbase incluing start zookeeper, or you must start zookeeper by another script.



4.Edit regionservers file, remove defailt config localhost , and add your hosts of hbase regionservers.


5.Compress your hbase folder and scp to other regionservers 



6.Uncompress hbase.tar.gz for your all regionservers to the place you want.

7.Start Hbase and Zookeeper, to see Hbase master on http://hadoop-master:16010




You can see the zookeeper config as below.



恭喜~* 若您需要使用 Phoenix + Hbase的話 請繼續參考下一篇教學