2014年12月28日日曜日

RethinkDBとgroovyでPrimary Keyを指定してテーブルを作成する

RethinkDBとgroovyでPrimary Keyを指定してテーブルを作成するには、以下のサンプルコードのようにtableCreateを使用します。

サンプルコード
@GrabConfig(systemClassLoader=true)
@Grab(group='com.rethinkdb', module='rethink-java-driver', version='0.3')
import com.rethinkdb.*
import com.rethinkdb.model.*

def hostname = "192.168.1.211" // replace this
def dbName = "test"
def tableName = "testtable1"
def pk = "pkcol"

RethinkDB r = RethinkDB.r
def conn = r.connect(hostname)
r.db(dbName).tableCreate(tableName, pk, Durability.hard, null).run(conn).each {
  println it
}
conn.close()

2014年12月27日土曜日

groovyとshipyardでaccountを作成する

groovyとshipyardでaccountを作成する groovyとshipyardでaccountを作成するには、以下のサンプルコードのようにX-Service-Keyにサービスキーを設定して/api/accountsにPOSTします。

サンプルコード
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.6')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import org.apache.http.entity.*
import groovy.json.*

def host = "192.168.1.212" // replace this
def port = 8080
def serviceKey = "your-service-key"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpPost(
    "http://${host}:${port}/api/accounts"
  )
  method.setHeader("X-Service-Key", serviceKey)
  def json = new JsonBuilder()
  json (
    "username":"test1",
    "password":"test1",
    "role": [
       "name":"admin"
    ]
  )
  method.setHeader("Content-Type", "application/json; charset=utf-8")
  method.setEntity(new StringEntity(json.toString(), "UTF-8"))
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()
}

関連情報

2014年12月26日金曜日

WebHDFSのREST APIでディレクトリを作成する

WebHDFSのREST APIでディレクトリを作成するには、以下のようなコードを実行します。

サンプルプログラム
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import groovy.json.*

def host = "192.168.206.132" // replace this
def port = 50070
def path = "/user/hadoop25b/newdir"
def user = "hadoop25b"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpPut("http://${host}:${port}/webhdfs/v1${path}?op=MKDIRS&user.name=${user}")
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
  println json.boolean
}
動作環境
Hadoop 2.5.0

2014年12月25日木曜日

groovyとDocker Remote APIでversion情報を取得する

groovyとDocker Remote APIでversion情報を取得するには、以下のようなコードを実行します。

サンプルコード
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import groovy.json.*

def host = "192.168.1.215" // replace this
def port = 4243

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpGet(
    "http://${host}:${port}/version"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
  println "API version:${json.ApiVersion}"
  println "version:${json.Version}"
  println "Kernel version:${json.KernelVersion}"
}
Docker Remote APIを使用可能にする方法
ubuntuの場合、/etc/default/dockerに以下のように記述します。
DOCKER_OPTS="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d"
そのあと、dockerを再起動します。
sudo service docker restart

動作環境
Docker 1.2.0

2014年12月23日火曜日

groovyとApache Phoenixでビューを作成する

groovyとApache Phoenixでビューを作成するには、以下のようなコードを実行します。

サンプルコード
import groovy.sql.Sql
import java.sql.*

sql = Sql.newInstance(
  "jdbc:phoenix:localhost",
  "",
  "",
  "org.apache.phoenix.jdbc.PhoenixDriver")

sql.commit()
// version 4.1.0現在、viewでのjoinは出来ないみたい
ddl = """
CREATE VIEW IF NOT EXISTS cust000001 as
select
*
from
  CUSTOMER
where
  customer_cd = '000001'
"""
sql.execute(ddl)
sql.commit()
query = "select * from cust000001"
sql.eachRow(query){ row ->
  println row
}
クライアント設定
phoenix-4.1.0-bin.tar.gzを解凍しphoenix-4.1.0-bin/hadoop2/phoenix-4.1.0-client-hadoop2.jarを~/.groovy/libなどにコピーします。

動作環境
groovy 2.3.6, JDK 7 Update 65, Hadoop 2.5.0, zookeeper 3.4.6, hbase 0.98.5, phoenix 4.1.0

2014年12月21日日曜日

RethinkDBとgroovyでテーブルを作成する

RethinkDBとgroovyでテーブルを作成するには、以下のサンプルコードのようにtableCreateを使用します。

サンプルコード
@GrabConfig(systemClassLoader=true)
@Grab(group='com.rethinkdb', module='rethink-java-driver', version='0.3')
import com.rethinkdb.*

def hostname = "192.168.1.211" // replace this
def dbName = "test"
def tableName = "testtable0"

RethinkDB r = RethinkDB.r
def conn = r.connect(hostname)
r.db(dbName).tableCreate(tableName).run(conn).each {
  println it
}
conn.close()

2014年12月20日土曜日

groovyとshipyardでcontainerを一覧表示する

groovyとshipyardでcontainerを一覧表示するには、以下のサンプルコードのようにX-Service-Keyにサービスキーを設定して/api/containersにアクセスします。

サンプルコード
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.6')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import org.apache.http.entity.*
import groovy.json.*

def host = "192.168.1.212" // replace this
def port = 8080
def serviceKey = "your-service-key"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpGet(
    "http://${host}:${port}/api/containers"
  )
  method.setHeader("X-Service-Key", serviceKey)
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()
  def rjson = new JsonSlurper().parseText(response.getEntity().getContent().text)
  rjson.each {
    print "id:${it.id}, container name:${it.name}, "
    println "image name:${it.image.name}, running:${it.state}"
  }
}

関連情報

2014年12月19日金曜日

WebHDFSのREST APIでファイル・ディレクトリをリネームする

WebHDFSのREST APIでファイル・ディレクトリをリネームするには、以下のようなコードを実行します。

サンプルプログラム
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import groovy.json.*

def host = "192.168.206.132" // replace this
def port = 50070
def path = "/user/hadoop25b/newdir"
def newpath = "/user/hadoop25b/renameddir"
def user = "hadoop25b"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpPut(
    "http://${host}:${port}/webhdfs/v1${path}?op=RENAME&user.name=${user}&destination=${newpath}"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
  println json.boolean
}
動作環境
Hadoop 2.5.0

2014年12月18日木曜日

groovyとDocker Remote APIでDocker serverにpingする

groovyとDocker Remote APIでDocker serverにpingするには、以下のようなコードを実行します。

サンプルコード
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import groovy.json.*

def host = "192.168.1.215" // replace this
def port = 4243

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpGet(
    "http://${host}:${port}/_ping"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  response.getEntity().getContent().text.eachLine {
    println it // "OK" if server is running
  }
}
Docker Remote APIを使用可能にする方法
ubuntuの場合、/etc/default/dockerに以下のように記述します。
DOCKER_OPTS="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d"
そのあと、dockerを再起動します。
sudo service docker restart

動作環境
Docker 1.2.0

2014年12月16日火曜日

groovyとApache Phoenixでクエリーの実行計画を表示する

groovyとApache Phoenixでクエリーの実行計画を表示するには、以下のようなコードを実行します。

サンプルコード
import groovy.sql.Sql
import java.sql.*

sql = Sql.newInstance(
  "jdbc:phoenix:localhost",
  "",
  "",
  "org.apache.phoenix.jdbc.PhoenixDriver")

query = """
select
  cust.customer_cd,
  MAX(cust.first_name) as first_name,
  MAX(cust.last_name) as last_name,
  SUM(sales) as total_sales
from
  SALES_DATA data
  join CUSTOMER cust
    ON (data.CUSTOMER_CD = cust.CUSTOMER_CD)
group by cust.customer_cd
ORDER BY SUM(sales) desc
"""
sql.eachRow("explain" + query){ row ->
  println row
}
クライアント設定
phoenix-4.1.0-bin.tar.gzを解凍しphoenix-4.1.0-bin/hadoop2/phoenix-4.1.0-client-hadoop2.jarを~/.groovy/libなどにコピーします。

動作環境
groovy 2.3.6, JDK 7 Update 65, Hadoop 2.5.0, zookeeper 3.4.6, hbase 0.98.5, phoenix 4.1.0

2014年12月14日日曜日

RethinkDBとgroovyでデータベースを削除する

RethinkDBとgroovyでデータベースを削除するには、以下のサンプルコードのようにdbDropを使用します。

サンプルコード
@GrabConfig(systemClassLoader=true)
@Grab(group='com.rethinkdb', module='rethink-java-driver', version='0.3')
import com.rethinkdb.*

def hostname = "192.168.1.211" // replace this
def dbName = "testdb1"

RethinkDB r = RethinkDB.r
def conn = r.connect(hostname)
r.dbDrop(dbName).run(conn).each {
  println it
}
conn.close()

2014年12月13日土曜日

groovyとshipyardでservice keyを取得する

groovyとshipyardでservice keyを取得するには、以下のサンプルコードのようにloginを実施してからservicekeysでservice keyを取得します。

サンプルコード
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.6')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import org.apache.http.entity.*
import groovy.json.*

def host = "192.168.1.212" // replace this
def port = 8080
def authToken

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpPost(
    "http://${host}:${port}/auth/login"
  )
  def json = new JsonBuilder()
  json (
    "username":"admin", "password":"shipyard"
  )
  method.setHeader("Content-Type", "application/json; charset=utf-8")
  method.setEntity(new StringEntity(json.toString(), "UTF-8"))
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()
  def rjson = new JsonSlurper().parseText(response.getEntity().getContent().text)
  authToken = rjson.auth_token
  println "auth token:${authToken}"

  def method2 = new HttpPost(
    "http://${host}:${port}/api/servicekeys"
  )
  def json2 = new JsonBuilder()
  json2 (
    description:"test key"
  )
  method2.setHeader("Content-Type", "application/json; charset=utf-8")
  method2.setHeader("X-Access-Token", "admin:" + authToken)
  method2.setEntity(new StringEntity(json2.toString(), "UTF-8"))

  def response2 = httpclient.execute(method2)
  println response2.getStatusLine().getStatusCode()
  def rjson2 = new JsonSlurper().parseText(response2.getEntity().getContent().text)
  println "service key:${rjson2.key}"

}

2014年12月12日金曜日

WebHDFSのREST APIでファイル・ディレクトリを削除する

WebHDFSのREST APIでファイル・ディレクトリを削除するには、以下のようなコードを実行します。

サンプルプログラム
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import groovy.json.*

def host = "192.168.206.132" // replace this
def port = 50070
def path = "/user/hadoop25b/test.txt"
def user = "hadoop25b"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpDelete(
    "http://${host}:${port}/webhdfs/v1${path}?op=DELETE&user.name=${user}"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
  println json.boolean
}
動作環境
Hadoop 2.5.0

2014年12月11日木曜日

groovyとDocker Remote APIでcontainerのログを取得する

groovyとDocker Remote APIでcontainerのログを取得するには、以下のようなコードを実行します。

サンプルコード
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import groovy.json.*

def host = "192.168.1.215" // replace this
def port = 4243
def containerId = "your-container-id"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def opts = "stderr=1&stdout=1&timestamps=1&follow=0&tail=20"
  def method = new HttpGet(
    "http://${host}:${port}/containers/${containerId}/logs?${opts}"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  response.getEntity().getContent().text.eachLine {
    println it.substring(8)
  }
}
Docker Remote APIを使用可能にする方法
ubuntuの場合、/etc/default/dockerに以下のように記述します。
DOCKER_OPTS="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d"
そのあと、dockerを再起動します。
sudo service docker restart

動作環境
Docker 1.2.0

2014年12月9日火曜日

groovyとApache Phoenixでテーブルを結合してクエリーを実行する

groovyとApache Phoenixでテーブルを結合してクエリーを実行するには、以下のようなコードを実行します。

サンプルコード
import groovy.sql.Sql
import java.sql.*

sql = Sql.newInstance(
  "jdbc:phoenix:localhost",
  "",
  "",
  "org.apache.phoenix.jdbc.PhoenixDriver")

query = """
select
  cust.customer_cd,
  MAX(cust.first_name) as first_name,
  MAX(cust.last_name) as last_name,
  SUM(sales) as total_sales
from
  SALES_DATA data
  join CUSTOMER cust
    ON (data.CUSTOMER_CD = cust.CUSTOMER_CD)
group by cust.customer_cd
ORDER BY SUM(sales) desc
"""
sql.eachRow(query){ row ->
  println row
}
クライアント設定
phoenix-4.1.0-bin.tar.gzを解凍しphoenix-4.1.0-bin/hadoop2/phoenix-4.1.0-client-hadoop2.jarを~/.groovy/libなどにコピーします。

動作環境
groovy 2.3.6, JDK 7 Update 65, Hadoop 2.5.0, zookeeper 3.4.6, hbase 0.98.5, phoenix 4.1.0

2014年12月7日日曜日

RethinkDBとgroovyでデータベースを作成する

RethinkDBとgroovyでデータベースを作成するには、以下のサンプルコードのようにdbCreateを使用します。

サンプルコード
@GrabConfig(systemClassLoader=true)
@Grab(group='com.rethinkdb', module='rethink-java-driver', version='0.3')
import com.rethinkdb.*

def hostname = "192.168.1.211" // replace this
def dbName = "testdb1"

RethinkDB r = RethinkDB.r
def conn = r.connect(hostname)
r.dbCreate(dbName).run(conn).each {
  println it
}
conn.close()

2014年12月5日金曜日

WebHDFSのREST APIでファイルのタイプ・サイズ・オーナーなどの情報を取得する

WebHDFSのREST APIでファイルのタイプ・サイズ・オーナーなどの情報を取得するには、以下のようなコードを実行します。

サンプルプログラム
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import groovy.json.*

def host = "192.168.206.132" // replace this
def port = 50070
def path = "/user/hadoop25b"
def file = "01_2012.csv"
def user = "hadoop25b"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpGet(
    "http://${host}:${port}/webhdfs/v1${path}/${file}?op=GETFILESTATUS&user.name=${user}"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
  def status = json.FileStatus
  println "type:${status.type}"
  println "length:${status.length}"
  println "owner:${status.owner}"
  println "group:${status.group}"
  println "permission:${status.permission}"

}
動作環境
Hadoop 2.5.0

2014年12月4日木曜日

groovyとDocker Remote APIでcontainer内のプロセスを一覧表示する

groovyとDocker Remote APIでcontainer内のプロセスを一覧表示するには、以下のようなコードを実行します。

サンプルコード
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import groovy.json.*

def host = "192.168.1.215" // replace this
def port = 4243
def containerId = "your-container-id"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpGet(
    "http://${host}:${port}/containers/${containerId}/top"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
println "${json}"
  for(proc in json.Processes){
    println "user=${proc[0]}, PID=${proc[1]}, command=${proc[7]}"
  }
}
Docker Remote APIを使用可能にする方法
ubuntuの場合、/etc/default/dockerに以下のように記述します。
DOCKER_OPTS="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d"
そのあと、dockerを再起動します。
sudo service docker restart

動作環境
Docker 1.2.0

2014年12月2日火曜日

groovyとApache Phoenixでバッチupsertを実行する

groovyとApache Phoenixでテーブルを削除するには、以下のようなコードを実行します。

サンプルコード
import groovy.sql.Sql
import java.sql.*

sql = Sql.newInstance(
  "jdbc:phoenix:localhost",
  "",
  "",
  "org.apache.phoenix.jdbc.PhoenixDriver")

stmt_create = """
CREATE TABLE SALES_DATA
(
  SALES_DATE DATE NOT NULL,
  CUSTOMER_CD CHAR(6) NOT NULL,
  STORE_ID INTEGER NOT NULL,
  ITEM_ID BIGINT NOT NULL,
  SALES BIGINT,
  UNITS_SOLD BIGINT,
  CONSTRAINT PK_SALES_FACT
    PRIMARY KEY (SALES_DATE, CUSTOMER_CD, STORE_ID, ITEM_ID)
)
"""
sql.execute(stmt_create)

stmt_ins = """
UPSERT INTO SALES_DATA VALUES (?, ?, ?, ?, ?, ?)
"""
sql.withBatch(1000, stmt_ins){ ps ->
  ps.addBatch([new Date(new java.util.Date("2014/08/01").time),
    "000001", 1, 100, 1000, 2])
  ps.addBatch([new Date(new java.util.Date("2014/08/02").time),
    "000002", 1, 102, 2000, 4])
  ps.addBatch([new Date(new java.util.Date("2014/08/03").time),
    "000001", 1, 104, 1000, 4])
  ps.addBatch([new Date(new java.util.Date("2014/08/04").time),
    "000002", 1, 100, 3000, 6])
}
sql.commit()

query = """
select * from SALES_DATA
"""
sql.eachRow(query){ row ->
  println row
}
クライアント設定
phoenix-4.1.0-bin.tar.gzを解凍しphoenix-4.1.0-bin/hadoop2/phoenix-4.1.0-client-hadoop2.jarを~/.groovy/libなどにコピーします。

動作環境
groovy 2.3.6, JDK 7 Update 65, Hadoop 2.5.0, zookeeper 3.4.6, hbase 0.98.5, phoenix 4.1.0

2014年11月30日日曜日

RethinkDBとgroovyでデータベースを一覧表示する

RethinkDBとgroovyでデータベースを一覧表示するには、以下のサンプルコードのようにdbListを使用します。

サンプルコード
@GrabConfig(systemClassLoader=true)
@Grab(group='com.rethinkdb', module='rethink-java-driver', version='0.3')
import com.rethinkdb.*

def hostname = "192.168.1.211" // replace this

RethinkDB r = RethinkDB.r
def conn = r.connect(hostname)
r.dbList().run(conn).each {
  println it
}
conn.close()