JAVA開発支援ツール

Mavenのインストール

sudo aptitude -y install maven2

環境変数の設定

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export CLASSPATH=".:/usr/lib/jvm/java-6-sun/lib" 

Mavenへライブラリのインストール

Hadoop関連

export HADOOP_HOME=/usr/lib/hadoop-0.20
export HBASE_HOME=/usr/lib/hbase


mvn install:install-file -DgroupId=org.apache.hadoop -DartifactId=hadoop-core -Dversion=1.2.1 -Dpackaging=jar -Dfile=${HADOOP_HOME}/hadoop-core.jar
mvn install:install-file -DgroupId=org.apache.zookeeper -DartifactId=zookeeper -Dversion=3.4.2 -Dpackaging=jar -Dfile=${HBASE_HOME}/lib/zookeeper.jar
mvn install:install-file -DgroupId=org.apache.hadoop -DartifactId=hbase -Dversion=0.90.6 -Dpackaging=jar -Dfile=${HBASE_HOME}/hbase.jar

Sunライブラリ関連
Download the version 1.1 API Documentation, Jar and Source
Java Management Extension (JMX) 1.2.1

mvn install:install-file -DgroupId=javax.jms -DartifactId=jms -Dversion=1.1 -Dpackaging=jar -Dfile=/usr/lib/jvm/java-6-sun/lib/jms.jar
mvn install:install-file -DgroupId=com.sun.jmx -DartifactId=jmxri -Dversion=1.2.1 -Dpackaging=jar -Dfile=/usr/lib/jvm/java-6-sun/lib/jmxri.jar
mvn install:install-file -DgroupId=com.sun.jdmk -DartifactId=jmxtools -Dversion=1.2.1 -Dpackaging=jar -Dfile=/usr/lib/jvm/java-6-sun/lib/jmxtools.jar

Mavenリポジトリ

Mavenでの開発

プロジェクトの作成

mkdir projects
cd projects

mvn archetype:create -DgroupId=me.sheeps.hdfs -DartifactId=sample
cd sample

生成されるファイル

./sample
    src/
        main/
            java/
                me/
                    sheeps/
                        hdfs/
                            App.java
        test/
            java/
                me/
                    sheeps/
                        hdfs/
                            AppTest.java
    pom.xml

pom.xmlに利用するライブラリを追加

vi pom.xml

[xml]
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>me.sheeps.hdfs</groupId>
<artifactId>sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sample</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>me.sheeps.hdfs.App</mainClass>
<packageName>me.sheeps.hdfs</packageName>
<addClasspath>true</addClasspath>
<addExtensions>true</addExtensions>
<classpathPrefix>lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
<version>1.2.1</version>
</dependency>

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>0.20</version>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.2</version>
</dependency>

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hbase</artifactId>
<version>0.90.6</version>
</dependency>

<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.1</version>
</dependency>

</dependencies>
</project>
[/xml]

hBaseのサンプル

[java]
package me.sheeps.hdfs;

import java.io.IOException;
import java.lang.System;
import java.util.NavigableMap;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.GenericOptionsParser;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.DependentColumnFilter;
import org.apache.hadoop.hbase.util.Bytes;

/**
* Appクラス
*
* @package Sample
* @author Yujiro Takahashi <yujiro3@gmail.com>
*/
public class App {
/**
* メイン
*
* @access public
* @param String[] args
* @return void
*/
public static void main(String[] args) throws Exception {
// 設定情報の読み込み
Configuration conf = HBaseConfiguration.create();
conf.addResource("/etc/hbase/conf/hbase-site.xml");
conf.set("hbase.client.scanner.caching", "3");

// 引数のパース
new GenericOptionsParser(conf, args);

HTable table = new HTable(conf, Bytes.toBytes("accesslog")); // テーブルを指定

// Scan条件の指定
Scan scan = new Scan();
Filter filter = new DependentColumnFilter(
Bytes.toBytes("user"), // カラムファミリを指定
Bytes.toBytes("id"), // 修飾子を指定
false,
CompareOp.EQUAL,
new BinaryPrefixComparator(Bytes.toBytes(args[0]))
);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);

System.out.println("/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/");

for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
String row = Bytes.toString(rr.getValue(Bytes.toBytes(args[1]), Bytes.toBytes(args[2])));
System.out.println(row);
}
}
}
[/java]

コンパイル

mvn compile

パッケージ化

mvn clean package

実行

hadoop jar ./target/sample-1.0-SNAPSHOT.jar 1258878 log timestamp

Leave a reply