2015年1月19日月曜日

RethinkDBとgroovyでテーブルのドキュメントを全件表示する

RethinkDBとgroovyでテーブルのドキュメントを全件表示するには、以下のサンプルコードのようにdbメソッドでデータベースを指定して、tableメソッドを使用します。

サンプルコード
@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 = "testtable0"

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

2015年1月18日日曜日

Apache Ambariとgroovyで、サービスを停止する

Apache Ambariとgroovyで、サービスを停止するには、以下のようなコードを実行します。

サンプルコード
@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 org.apache.http.entity.*
import groovy.json.*

def host = "192.168.1.249" // replace this
def port = 8080
def user = "admin"
def password = "admin"
def cluster = "cluster1"
def service = "HIVE"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {
  def method = new HttpPut(
    "http://${user}:${password}@${host}:${port}/api/v1/clusters/${cluster}/services/${service}"
  )
  println method
  def json = new JsonBuilder()
  json (
    ["Body":[ServiceInfo:[ "state":"INSTALLED" ]]]
  )
  println json
  method.setHeader("content-Type", "application/x-www-form-urlencoded")
  method.setHeader("X-Requested-By", "ambari")
  method.setEntity(new StringEntity(json.toString(), "UTF-8"))
  def response = httpclient.execute(method)

  println response.getStatusLine().getStatusCode()

  def rj = new JsonSlurper().parseText(response.getEntity().getContent().text)
  println rj
}
動作環境
Apache Ambari 1.6.1, HDP2.1, groovy 2.3.2

2015年1月17日土曜日

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

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

サンプルコード
@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/engines"
  )
  method.setHeader("X-Service-Key", serviceKey)
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()
  def rjson = new JsonSlurper().parseText(response.getEntity().getContent().text)
println rjson

  rjson.each {
    println "id:${it.engine.id}, addr:${it.engine.addr}, status:${it.health.status}"
  }

}

関連情報

2015年1月15日木曜日

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 org.apache.http.entity.*
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 HttpPost(
    "http://${host}:${port}/containers/${containerId}/start"
  )
  def json = new JsonBuilder()
  json (
    [PortBindings:[ "80/tcp":[["HostPort":"8080"]] ]]
  )
  println json
  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()
}
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

2015年1月11日日曜日

RethinkDBとgroovyでテーブルを一覧表示する

RethinkDBとgroovyでテーブルを一覧表示するには、以下のサンプルコードのようにtableListを使用します。

サンプルコード
@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"

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

2015年1月10日土曜日

groovyとshipyardでaccountを削除する

groovyとshipyardでaccountを削除するには、以下のサンプルコードのようにDELETEを実行します。

サンプルコード
@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"

class HttpDeleteEx extends HttpPost
{
  public String getMethod()
  {
    return "DELETE"
  }

  public HttpDeleteEx(String uri)
  {
    super(uri)
  }
}

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpDeleteEx(
    "http://${host}:${port}/api/accounts"
  )
  method.setHeader("X-Service-Key", serviceKey)
  def json = new JsonBuilder()
  json (
    "username":"test1"
  )
  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()
}

関連情報

2015年1月9日金曜日

WebHDFSのREST APIでファイルをアップロードする

WebHDFSのREST APIでファイルをアップロードするには、以下のようなコードを実行します。

サンプルプログラム
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
@Grab(group='org.apache.httpcomponents', module='httpmime', 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 org.apache.http.entity.mime.*
import org.apache.http.protocol.*
import groovy.json.*

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

def context = new BasicHttpContext()
def httpclient = new DefaultHttpClient()
def datanode
httpclient.withCloseable {
  def method = new HttpPut("http://${host}:${port}/webhdfs/v1${path}/${file}?op=CREATE&user.name=${user}")

  def response = httpclient.execute(method, context)
  println response.getStatusLine().getStatusCode()
  datanode = response.getHeaders("Location")[0].value

  println "DN:" + datanode
}

httpclient = new DefaultHttpClient()
httpclient.withCloseable {
  method = new HttpPut("${datanode}")
  def builder = MultipartEntityBuilder.create()
  builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
  builder.addBinaryBody("file", new File(file))
  method.setEntity(builder.build())

  response = httpclient.execute(method)

  println response.getStatusLine().getStatusCode()
}
動作環境
Hadoop 2.5.0