Eclipseでpom.xmlを使ってmavenプロジェクトをimport
JXTAのプロジェクトをインポートする。
http://jxse.kenai.com/index.html
まず、ここからpom.xmlをダウンロード
eclipseのワークスペースにインポートするプロジェクトようにディレクトリをつくり、ダウンロードしたpom.xmlをおく。(ディレクトリの名前がプロジェクトの名前になる)
コマンドプロンプトから新しいディレクトリに移動して、以下のコマンドを実行。
Eclipseを開いて、Package Explorerを右クリックしてImport、Maven->Existing Maven Projectを選択する
。Root Directoryを先ほど作成したDirectoryにセットしてFinish。
PEARのアップグレード+PHPUnitのインストール+Jenkinsインストール
MacOS LionのXampp環境で『PEARのアップグレード+PHPUnitのインストール+Jenkinsインストール』を行う。
PHPUnitのインストール
上の続き。同じディレクトリ階層・管理者権限。
インストール
./pear config-set auto_discover 1
./pear install pear.phpunit.de/PHPUnit
(オプション)名前からして使いそうなのでこちらもインストール
./pear install phpunit/DbUnit
./pear install phpunit/PHPUnit_Selenium
確認
./phpunit --version
Jenkinsのインストール
Jenkinsインストールの前にPEARから必要なライブラリをインストール。
引き続き上と同じ階層・管理者権限。
./pear channel-discover pear.phing.info
./pear install phing/phing
確認
./phing -v
./pear info phing/phing
JenkinsのMac用パッケージをダウンロード。
確認
PHPなのでAvailableタグの以下の項目を有効にする。
Phing Plugin - This plugin allows you to use Phing to build PHP projects.
xUnit Plugin - This plugin makes it possible to publish the test results of an execution of a testing tool in Jenkins.
以上。完了。
Twitter4jでTimelineを取得!!
意外に分かっていないことを実感した、、、。
import java.util.Iterator; import java.util.List; import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.conf.ConfigurationBuilder; import twitter4j.http.AccessToken; public class Twitter4jTest { public static void main(String[] args) throws TwitterException { Twitter twitter = null; // fill out yourself!! String CONSUMERKEY = "CONSUMERKEY"; String CONSMERSECRET = "CONSMERSECRET"; String ACCESSTOKEN = "ACCESSTOKEN"; String ACCESSSECRET = "ACCESSSECRET"; ConfigurationBuilder confbuilder = new ConfigurationBuilder(); confbuilder.setOAuthConsumerKey(CONSUMERKEY); confbuilder.setOAuthConsumerSecret(CONSMERSECRET); TwitterFactory twitterfactory = new TwitterFactory(confbuilder.build()); twitter = twitterfactory.getOAuthAuthorizedInstance(new AccessToken( ACCESSTOKEN, ACCESSSECRET)); List<Status> publicTimeline = twitter.getPublicTimeline(); Iterator iterator = publicTimeline.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }
EclipseでJavaRMIのHello world!
ここの内容をEclipseで。
http://java.sun.com/javase/ja/6/docs/ja/technotes/guides/rmi/hello/hello-world.html
- リモートインタフェースの定義
File > New > Interface からNameを"Hello"、packageを"example.hello"、Extended interfacesのAddから"java.rmi.Remote"を選択してFinish。
コードを以下のように書き換える。
package example.hello; import java.rmi.Remote; import java.rmi.RemoteException; public interface Hello extends Remote { String sayHello() throws RemoteException; }
- サーバーの実装
File > New > Class からNameを"Server"、packageを"example.hello"、Extended interfacesのAddから"example.hello.Hello"を選択してFinish。
コードを以下のように書き換える。
package example.hello; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class Server implements Hello { public Server() {} public String sayHello( ) { return "Hello, world!"; } public static void main(String args[]) { try { Server obj = new Server(); Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0); // Bind the remote object's stub in the registry Registry registry = LocateRegistry.getRegistry(); registry.bind("Hello", stub); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception:" + e.toString()); e.printStackTrace( ); } } }
- クライアントの実装
File > New > Class からNameを"Client"、packageを"example.hello"にしてFinish。
コードを以下のように書き換える。
package example.hello; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Client { public static void main(String[] args) { String host = (args.length < 1) ? null : args[0]; Registry registry; try { registry = LocateRegistry.getRegistry(host); Hello stub; try { System.currentTimeMillis(); stub = (Hello) registry.lookup("Hello"); String response = stub.sayHello(); System.out.println("response:" + response); } catch (NotBoundException e) { System.err.println("Client exception:" + e.toString()); e.printStackTrace(); } } catch (RemoteException e) { System.err.println("Client exception:" + e.toString()); e.printStackTrace(); } } }
これでサンプルプログラムは完成。
実行の手順。
Terminalをたちあげ、"rmiregistry"を実行する。
- サーバーの起動
Run > Run Configurationsから、Serverクラスを実行する。この時、利用できるサーバ側のクラスの場所(codebase)を設定する。ArgumentsのVM Argumentsに"-Djava.rmi.server.codebase=file:${workspace_loc}/プロジェクト名/bin/"に指定する。
Server ready
- クライアントの実行
ClientクラスをRun。
response:Hello, world!
Cassandraのスライドを訳した!!
ACMのプログラミングコンテストが終わり、しばらくの間サボっておりました。
9月ぐらいまでを英語強化月間とし、べんきょうします!
クラウド関係の論文を読んでいこうと思うんだが、まずは簡単なものからってことで。
かなり雑な訳で内容も理解していないが、、、これからだ。。
あいているところやアルファベットのままのとこ、意味不明なとこはだれか教えていただけないでしょうか、、、
Why Cassandra?
• Lots of data
– Copies of messages, reverse indices of messages, per user data.
• Many incoming requests resulting in a lot of random reads and random writes.
• No existing production ready solutions in the market meet these requirements.
なぜCassandraか。
・多くのデータ
-メッセージのコビー,メッセージの逆インデックス,ユーザー毎の
・多くのincomingは,ランダムリード,ランダムライトの結果を要求します。
・これらの問題を解決するための要求を満たしたプロダクションは,市場に存在しない。
Design Goals
• High availability
• Eventual consistency
– trade-off strong consistency in favor of high availability
• Incremental scalability
• Optimistic Replication
• “Knobs” to tune tradeoffs between consistency, durability and latency
• Low total cost of ownership
• Minimal administration
設計のゴール
・High availability
・Eventual consistency
-可用性を重視した強い一貫性とのトレードオフ
・Incremental scalability
・Optimistic Replication
・"Knobs" consistency,durability,latencyのトレードオフを調整する
・所有コストが低い
・最小の管理
Write Operations
• A client issues a write request to a random node in the Cassandra cluster.
• The “Partitioner” determines the nodes responsible for the data.
• Locally, write operations are logged and then applied to an in-memory version.
• Commit log is stored on a dedicated disk local to the machine.
書き込み操作
・クライアントはCassandra clusterのランダムノードから書き込み要求を出します。
・"Partitioner" がデータに対して責任のあるノードを決定します。
・ローカルで書き込み操作は記録され,それから in-memory versionに適用されます。
・コミットログは,マシンの専用のディスクロオーカルにストアされます。
Write Properties
• No locks in the critical path
• Sequential disk access
• Behaves like a write back Cache
• Append support without read ahead
• Atomicity guarantee for a key
• “Always Writable”
– accept writes during failure scenarios
書き込み特性
・クリティカルパスのロックではない
・シーケンシャルディスクアクセス
・write back Cache のようにふるまう
・前を読むことなく追加できる
・キーに対して原子性を保証する
・常に書き込める
-失敗したシナリオでも書き込める
Cluster Membership and Failure Detection
• Gossip protocol is used for cluster membership.
• Super lightweight with mathematically provable properties.
• State disseminated in O(logN) rounds where N is the number of nodes in the cluster.
• Every T seconds each member increments its heartbeat counter and selects one other member to send its list to.
• A member merges the list with its own list.
クラスタメンバーシップと失敗検出
・クラスタメンバーシップではGossip protocolが使われている
・数学的に証明可能な軽さ
・StateはNがクラスタノード数である場合,O(logN)で広められます。
・
・メンバーは自信のリストにマージされます。
Accrual Failure Detector
• Valuable for system management, replication, load balancing etc.
• Defined as a failure detector that outputs a value, PHI, associated with each process.
• Also known as Adaptive Failure detectors - designed to adapt to changing network conditions.
• The value output, PHI, represents a suspicion level.
• Applications set an appropriate threshold, trigger suspicions and perform appropriate actions.
• In Cassandra the average time taken to detect a failure is 10-15 seconds with the PHI threshold set at 5.
Accrual Failure Detector
・システムマネージメント,レプリケーション,ロードバランシング等に貴重である
・valueを出力するfailure detectorとして定義されたPHIは各プロセスと関連付けられます。
・またAdaptive Failure detectorsとして知られる。-ネットワークコンディションの変化に適応するように設計されている
・value出力PHIは、疑いレベルと表します。
・アプリケーションは適当なしきいを設定し、疑いを誘発して、適切なアクションを実行をします。
・Cassandraでは、5時日設定されたPHIのしきい値にしたがった失敗検出の平均時間は10-15秒です。
Properties of the Failure Detector
• If a process p is faulty, the suspicion level
Φ(t)␣∞as t␣∞.
• If a process p is faulty, there is a time after which Φ(t) is monotonic increasing.
• A process p is correct ␣ Φ(t) has an ub over an infinite execution.
• If process p is correct, then for any time T,
Φ(t) = 0 for t >= T.
Failure Detector の特性
・プロセスPが不完全なら疑いレベルは
Φ(t)␣∞as t␣∞.
・プロセスPが不完全なら、Φ(t)が単調な増加である時があります。
・プロセスPが正しいなら、いつでもTです。
Performance Benchmark
• Loading of data - limited by network bandwidth.
• Read performance for Inbox Search in production:
パフォーマンスベンチマーク
・データのロード-ネットワークバンド幅によって制限される
・Inbox検索のリードパフォーマンス
Lessons Learnt
• Add fancy features only when absolutely required.
• Many types of failures are possible.
• Big systems need proper systems-level monitoring.
• Value simple designs
Lessons Learnt
・必要なときだけ、特徴を付け加えてください。
・多くの場合、失敗可能です。
・大きいシステムではシステムレベルの監視が必要になります。
・Valueはシンプルにデザインする
Future work
• Atomicity guarantees across multiple keys
• Analysis support via Map/Reduce
• Distributed transactions
• Compression support
• Granular security via ACL’s
将来の仕事
・複数キーでの原子性の保証
・Map/Reduceを介した分析サポート
・分散トランザクション
・圧縮サポート
・ACLの細かいセキュリティ
ACMの過去問解いてみた!!(5)〜2009年国内予選ProblemB
ProblemB
生まれて初めての再帰…。
島の数を求める問題です。
方針としては、まずインプットの島をそのまま配列に代入します。
それから配列を走査するのですが、島を見つけたらその島の大きさの分だけ再帰して数字を書き換えます。
例えば、最初に見つけた島は1→2に、次に見つけた島は2→3という感じです。
1だけを探していき、終わったらカウントを-1して出力すれば島の数になります。
package acm2009.b; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Q { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(Q.class .getResourceAsStream("input.txt"))); String line = null; while ((line = br.readLine()) != null) {
ここまではファイルの入出力。
String[] str = line.split(" "); int w = Integer.parseInt(str[0]); //地図の幅 int h = Integer.parseInt(str[1]); //地図の高さ int count = 1; //島を数えるためのカウンタ int[][] map = new int[h][w]; //島 for (int i = 0; i < h; i++) { line = br.readLine(); String[] str2 = line.split(" "); for(int j = 0 ; j < w ; j++){ map[i][j] = Integer.parseInt(str2[j]); } } if (w == 0 && h == 0) { break; }
ここまでで変数に全て入力します。
for(int i = 0 ; i < h ; i++){ for(int j=0 ; j < w ; j++){ if(map[i][j] == 1){ count++; game(count, w, h, map,i,j); } } } System.out.println(count-1); } }
島の数を数えて、カウントしていきます。
public static void game (int count, int w, int h, int[][] map, int i, int j){ if(j > 0){ if(map[i][j-1] == 1){//west map[i][j-1] = count; game(count, w, h, map,i,j-1); } } if(i > 0){ if(map[i-1][j] == 1){ //north map[i-1][j] = count; game(count, w, h, map,i-1,j); } } if(j > 0 && i > 0){ if(map[i-1][j-1] == 1){ //north-west map[i-1][j-1] = count; game(count, w, h, map,i-1,j-1); } } if(j < w-1 && i > 0){ if(map[i-1][j+1] == 1){ //north-east map[i-1][j+1] = count; game(count, w, h, map,i-1,j+1); } } if(j < w-1){ if(map[i][j+1] == 1){ //east map[i][j+1] = count; game(count, w, h, map,i,j+1); } } if(j < w-1 && i < h-1){ if(map[i+1][j+1] == 1){ //south-east map[i][j+1] = count; game(count, w, h, map,i,j+1); } } if(i < h-1){ if(map[i+1][j] == 1){ //south map[i+1][j] = count; game(count, w, h, map,i+1,j); } } if(j > 0 && i < h-1){ if(map[i+1][j-1] == 1){ //south-west map[i+1][j-1] = count; game(count, w, h, map,i+1,j-1); } } } }
島の大きさの分だけ再帰して、マップを書き換えます。
ACMの過去問解いてみた!!(4)〜2005年国内予選ProblemA
とうとうACMまで10日をきったのですが、なかなかきあいが入らず、、、。
問題はこちら。
大金持ちの利益を増やすやつです。
文章が複雑で読んで理解するまでで、9割完了といってもいいでしょう。
package acm2005.a; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Q { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(Q.class .getResourceAsStream("input.txt"))); String line = null; while ((line = br.readLine()) != null) { int m = Integer.parseInt(line); // データセット数
ここまではいつものお決まり。
”データセット数”まで読み込み、その数の分だけループします。
for (int i = 0; i < m; i++) { int found = Integer.parseInt(br.readLine()); // 初期運用資金量 int num = Integer.parseInt(br.readLine()); // 運用年数 int n = Integer.parseInt(br.readLine()); // 運用方法の種類数 double operation[][] = new double[n][3]; // 運用方法
今回はデータの種類が多いので、変数をたくさん宣言します。
運用方法に関しては、利子が少数なので、とりあえずdouble型で用意します。
for (int j = 0; j < n; j++) { line = br.readLine(); String[] str2 = line.split(" "); for (int k = 0; k < 3; k++) { operation[j][k] = Double.parseDouble(str2[k]); } }
運用方法まで読み込みます。
int total = 0; //合計 int max = 0; // max for (int j = 0; j < n; j++) { if (operation[j][0] == 0) { total = simCalc(found, num, operation[j][1], (int) operation[j][2]); } else { total = comCalc(found, num, operation[j][1], (int) operation[j][2]); } if (max < total) max = total; } System.out.println(max); } } }
ここから、実際の計算に入ります。
単利か複利かを判定し、メソッドに渡します。
運用方法の種類の数だけループさせ、返ってきた値の最大値を出力します。
//単利メソッド public static int simCalc(int found, int num, double interest, int commission) { // interest:利子, commission:手数料 int balance = 0; // 利子残高 for (int i = 0; i < num; i++) { balance = (int) (balance + found * interest); found -= commission; } return found + balance; } //複利メソッド public static int comCalc(int found, int num, double interest, int commission) { // interest:利子, commission:手数料 for (int i = 0; i < num; i++) { found = (int) (found + found * interest); found -= commission; } return found; } }
単利、複利計算のメソッドです。
大会が近づいてきたのだか、思ったより問題を解くスピードが上がらない、、、。