2014年10月31日金曜日

groovyとZooKeeperで、Ephemeral znodeを作成する

groovyとZooKeeperで、Ephemeral znodeを作成するには、以下のようなコードを実行します。

サンプルコード
@Grab(group='org.apache.zookeeper', module='zookeeper', version='3.4.6')
import org.apache.zookeeper.*

def zk = new ZooKeeper("localhost:2181", 3000, null)
try
{
  zk.create("/test1", "sample1".getBytes("UTF-8"),
    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL)
  for(child in zk.getChildren("/", false)){
    println child
  }
}
finally
{
  zk.close()
}

println "------------------------------"

zk = new ZooKeeper("localhost:2181", 3000, null)
try
{
  for(child in zk.getChildren("/", false)){
    println child
  }
}
finally
{
  zk.close()
}
動作環境
groovy 2.3.6, JDK 7 Update 65, Hadoop 2.5.0, zookeeper 3.4.6

2014年10月30日木曜日

groovyでCloudera Managerのサービスを一覧表示する

groovyでCloudera Managerのサービスを一覧表示するには、以下のようなコードを実行します。

サンプルコード
@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.240" // replace this
def port = 7180
def user = "admin"
def password = "admin"
def clusterName = "cluster"

def httpclient = new DefaultHttpClient()
httpclient.getCredentialsProvider().setCredentials(
  new AuthScope(host, port),
  new UsernamePasswordCredentials(user, password)
)

def method = new HttpGet("http://${host}:${port}/api/v7/clusters/${clusterName}/services")
def response = httpclient.execute(method)
println response.getStatusLine().getStatusCode()

def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
for(item in json.items){
  println "name:${item.name}"
  println "health summary:${item.healthSummary}"
  println "service state:${item.serviceState}"
  println "----"
}
参考情報
/clusters/{clusterName}/services - Cloudera Manager API v7

2014年10月28日火曜日

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 groovy.json.*

def host = "192.168.1.240" // replace this
def port = 7180
def user = "admin"
def password = "admin"
def clusterName = "cluster"
def serviceName = "hive"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {
  httpclient.getCredentialsProvider().setCredentials(
    new AuthScope(host, port),
    new UsernamePasswordCredentials(user, password)
  )

  def cluster = "/api/v7/clusters/${clusterName}"
  def method = new HttpPost(
    "http://${host}:${port}${cluster}/services/${serviceName}/commands/start"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

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

groovyとRabbitMQのHTTP APIを使用して、exchangeを削除する

groovyとRabbitMQのHTTP APIを使用して、exchangeを削除する groovyとRabbitMQのHTTP APIを使用して、exchangeを削除するには、以下のようなコードを実行します。

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

ConnectionKeepAliveStrategy ckas = new ConnectionKeepAliveStrategy() {
  public long getKeepAliveDuration(HttpResponse response, HttpContext context)
  {
    HeaderElementIterator it = new BasicHeaderElementIterator(
      response.headerIterator(HTTP.CONN_KEEP_ALIVE))
    while(it.hasNext()){
      HeaderElement he = it.nextElement()
      if( he.value != null && he.param.equalsIgnoreCase("timeout") ){
        try
        {
          return Long.parseLong(he.value) * 1000
        }
        catch(NumberFormatException nfex){}
      }
    }
    return 30 * 1000
  }
}
def host = "192.168.1.219"
def port = 15672
def user = "guest"
def pass = "guest"

CredentialsProvider credsProvider = new BasicCredentialsProvider()
credsProvider.setCredentials(
  new AuthScope(host, port),
  new UsernamePasswordCredentials(user, pass)
)
AuthCache authCache = new BasicAuthCache()
HttpHost targetHost = new HttpHost(host, port, "http")
BasicScheme basicAuth = new BasicScheme()
authCache.put(targetHost, basicAuth)
HttpClientContext context = HttpClientContext.create()
context.setCredentialsProvider(credsProvider)
context.setAuthCache(authCache)

CloseableHttpClient httpclient = HttpClients.custom()
  .setKeepAliveStrategy(ckas)
  .build()
httpclient.withCloseable {
  // exchangeの削除
  def exchange = "exchange1"
  def method = new HttpDelete("http://${host}:${port}/api/exchanges/%2f/${exchange}")
  response = httpclient.execute(method, context)
  println response.getStatusLine().getStatusCode()
}
動作環境
groovy 2.3.6, JDK7 update 65, RabbitMQ 3.3.5

2014年10月24日金曜日

groovyとZooKeeperで、znodeを削除する

groovyとZooKeeperで、指定パスのznodeを削除するには、以下のようなコードを実行します。

サンプルコード
@Grab(group='org.apache.zookeeper', module='zookeeper', version='3.4.6')
import org.apache.zookeeper.*

def zk = new ZooKeeper("localhost:2181", 3000, null)
try
{
  zk.delete("/test1", -1)
  for(child in zk.getChildren("/", false)){
    println child
  }
}
finally
{
  zk.close()
}

動作環境
groovy 2.3.6, JDK 7 Update 65, Hadoop 2.5.0, zookeeper 3.4.6

2014年10月23日木曜日

groovyでCloudera Managerに登録されているクラスタを一覧表示する

/clugroovyでCloudera Managerに登録されているクラスタを一覧表示するには、以下のようなコードを実行します。

サンプルコード
@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.240" // replace this
def port = 7180
def user = "admin"
def password = "admin"

def httpclient = new DefaultHttpClient()
httpclient.getCredentialsProvider().setCredentials(
  new AuthScope(host, port),
  new UsernamePasswordCredentials(user, password)
)

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

def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
for(item in json.items){
  println "name:${item.name}"
  println "display name:${item.displayName}"
  println "full version:${item.fullVersion}"
}
参考情報
/clusters - Cloudera Manager API v7

2014年10月21日火曜日

groovyとRabbitMQのHTTP APIを使用して、exchangeを作成する

groovyとRabbitMQのHTTP APIを使用して、exchangeを作成するには、以下のようなコードを実行します。

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

ConnectionKeepAliveStrategy ckas = new ConnectionKeepAliveStrategy() {
  public long getKeepAliveDuration(HttpResponse response, HttpContext context)
  {
    HeaderElementIterator it = new BasicHeaderElementIterator(
      response.headerIterator(HTTP.CONN_KEEP_ALIVE))
    while(it.hasNext()){
      HeaderElement he = it.nextElement()
      if( he.value != null && he.param.equalsIgnoreCase("timeout") ){
        try
        {
          return Long.parseLong(he.value) * 1000
        }
        catch(NumberFormatException nfex){}
      }
    }
    return 30 * 1000
  }
}
def host = "192.168.1.219"
def port = 15672
def user = "guest"
def pass = "guest"

CredentialsProvider credsProvider = new BasicCredentialsProvider()
credsProvider.setCredentials(
  new AuthScope(host, port),
  new UsernamePasswordCredentials(user, pass)
)
AuthCache authCache = new BasicAuthCache()
HttpHost targetHost = new HttpHost(host, port, "http")
BasicScheme basicAuth = new BasicScheme()
authCache.put(targetHost, basicAuth)
HttpClientContext context = HttpClientContext.create()
context.setCredentialsProvider(credsProvider)
context.setAuthCache(authCache)

CloseableHttpClient httpclient = HttpClients.custom()
  .setKeepAliveStrategy(ckas)
  .build()
httpclient.withCloseable {
  // exchangeの作成
  def exchange = "exchange1"
  def method = new HttpPut("http://${host}:${port}/api/exchanges/%2f/${exchange}")
  def json = new JsonBuilder()
  json (
    type:"direct", auto_delete:false, durable:true, internal:false
  )
  method.setHeader("Content-Type", "application/json; charset=utf-8")
  method.setEntity(new StringEntity(json.toString(), "UTF-8"))
  response = httpclient.execute(method, context)
  println response.getStatusLine().getStatusCode()
}
動作環境
groovy 2.3.6, JDK7 update 65, RabbitMQ 3.3.5

2014年10月20日月曜日

groovyとHBaseで、テーブル内の指定ColumnFamilyを全件表示する

groovyとHBaseで、テーブル内の指定ColumnFamilyを全件表示するには、以下のコードのようにgetScannerを使用します。

サンプルコード
@Grab(group='org.apache.hbase', module='hbase-client', version='0.98.5-hadoop2')
@Grab(group='org.apache.hbase', module='hbase-common', version='0.98.5-hadoop2')
@Grab(group='org.apache.hadoop', module='hadoop-client', version='2.5.0')
import org.apache.hadoop.hbase.*
import org.apache.hadoop.hbase.client.*
import org.apache.hadoop.hbase.util.*

def config = HBaseConfiguration.create()
config.set(HConstants.ZOOKEEPER_QUORUM, "localhost")
config.set(HConstants.ZOOKEEPER_CLIENT_PORT, "2181")
def table = new HTable(config, "test")
table.withCloseable {
  def rscanner = table.getScanner(Bytes.toBytes("cf1"))
  while((row = rscanner.next()) != null){
    def cscanner = row.cellScanner()
    while(cscanner.advance()){
      def cell = cscanner.current()
      println Bytes.toString(row.getRow())
      println Bytes.toString(CellUtil.cloneFamily(cell))
      println Bytes.toString(CellUtil.cloneQualifier(cell))
      println Bytes.toString(CellUtil.cloneValue(cell))
    }
  }
}
動作環境
groovy 2.3.6, JDK 7 Update 65, Hadoop 2.5.0, zookeeper 3.4.6, HBase0.98.5

2014年10月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 groovy.json.*

def host = "192.168.1.249" // replace this
def port = 8080
def user = "admin"
def password = "admin"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {
  def method = new HttpGet(
    "http://${user}:${password}@${host}:${port}/api/v1/clusters"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
  for(cluster in json.items.Clusters){
    println "cluster name:${cluster.cluster_name}"
    println "version:${cluster.version}"
  }
}
動作環境
Apache Ambari 1.6.1, HDP2.1, groovy 2.3.2

2014年10月17日金曜日

groovyとZooKeeperで、znodeに関連付けられた文字列データを表示する

groovyとZooKeeperで、znodeに関連付けられた文字列データを表示するには、以下のコードのようにgetDataを使用します。

サンプルコード
@Grab(group='org.apache.zookeeper', module='zookeeper', version='3.4.6')
import org.apache.zookeeper.*

def zk = new ZooKeeper("localhost:2181", 3000, null)
try
{
  println new String(zk.getData("/zk_test", false, null), "UTF-8")
}
finally
{
  zk.close()
}
動作環境
groovy 2.3.6, JDK 7 Update 65, Hadoop 2.5.0, zookeeper 3.4.6

2014年10月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")

stmt_create = """
CREATE TABLE CUSTOMER
(
  CUSTOMER_CD CHAR(6) NOT NULL,
  FIRST_NAME VARCHAR,
  LAST_NAME VARCHAR,
  ENROLL_DATE DATE,
  BIRTH_DATE DATE,
  CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUSTOMER_CD)
)
"""
sql.execute(stmt_create)

stmt_ins1 = """
UPSERT INTO CUSTOMER VALUES (
'000001', '一郎', '試験', TO_DATE('20140201', 'yyyyMMdd'),
TO_DATE('19900201', 'yyyyMMdd')
)
"""
sql.execute(stmt_ins1)

stmt_ins2 = """
UPSERT INTO CUSTOMER VALUES (
'000002', '二郎', '試験', TO_DATE('20140205', 'yyyyMMdd'),
TO_DATE('19920110', 'yyyyMMdd')
)
"""
sql.execute(stmt_ins2)
sql.commit()

query = """
select * from CUSTOMER
"""
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年10月14日火曜日

groovyとRabbitMQのHTTP APIを使用して、queueを一覧表示する

groovyとRabbitMQのHTTP APIを使用して、queueを一覧表示するには、以下のようなコードを実行します。

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

def host = "192.168.1.219"
def port = 15672
def user = "guest"
def pass = "guest"
def httpclient = new DefaultHttpClient()
httpclient.getCredentialsProvider().setCredentials(
  new AuthScope(host, port),
  new UsernamePasswordCredentials(user, pass)
)

def method = new HttpGet("http://${host}:${port}/api/queues")
response = httpclient.execute(method)

println response.getStatusLine().getStatusCode()
def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
println json
println "----"
for(queue in json){
  println "[${queue.name}]:state=${queue.state}:messages=${queue.messages}"
}
動作環境
groovy 2.3.6, JDK7 update 65, RabbitMQ 3.3.5

2014年10月13日月曜日

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}/json"
  )
  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()

  def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
  for(env in json.Config.Env){
    println "${env}"
  }
}

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

Apache TikaでWORDファイル等から著者などのメタデータを抽出する

Apache TikaでWORDファイル等から著者などのメタデータを抽出するには、以下のサンプルコードを実行します。
@Grab(group='org.apache.tika', module='tika-core', version='1.5')
@Grab(group='org.apache.tika', module='tika-parsers', version='1.5')
import org.apache.tika.*
import org.apache.tika.io.*
import org.apache.tika.metadata.*
import org.apache.tika.parser.*
import org.apache.tika.sax.*

def metadata = new Metadata()
def tis = TikaInputStream.get(new File("./test.doc"), metadata)
def handler = new BodyContentHandler()
def context = new ParseContext()
def parser = new AutoDetectParser()

parser.parse(tis, handler, metadata, context)
//println handler.toString()

for(key in metadata.names()){
  println "${key}=${metadata.get(key)}"
}

2014年10月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"
def user = "hadoop25b"

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

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

  def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
  for(status in json.FileStatuses.FileStatus){
    println "type:${status.type}"
    println "pathSuffix:${status.pathSuffix}"
  }
}

動作環境
Hadoop 2.5.0