Created
April 28, 2013 07:07
-
-
Save AlexYangYu/5476160 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.alexyang.scanner; | |
import com.stumbleupon.async.Callback; | |
import com.stumbleupon.async.Deferred; | |
import java.nio.charset.Charset; | |
import java.util.ArrayList; | |
import org.hbase.async.HBaseClient; | |
import org.hbase.async.KeyValue; | |
import org.hbase.async.Scanner; | |
class ResultHandler | |
implements Callback<String[], ArrayList<ArrayList<KeyValue>>> { | |
public String[] call(ArrayList<ArrayList<KeyValue>> t) throws Exception { | |
ArrayList<String> res = new ArrayList<String>(); | |
for (ArrayList<KeyValue> arr : t) { | |
for (KeyValue kv : arr) { | |
res.add(new String(kv.value(), Charset.forName("ISO-8859-1"))); | |
} | |
} | |
String[] result = new String[res.size()]; | |
for (int i = 0; i < res.size(); ++i) { | |
result[i] = res.get(i); | |
} | |
return result; | |
} | |
} | |
/** | |
* | |
* @author Alex Yang <[email protected]> | |
*/ | |
public class ScannerTest { | |
public static void main(String[] args) throws Exception { | |
HBaseClient hbase = new HBaseClient("centos-pd.alexyang.me"); | |
hbase.ensureTableExists("query").joinUninterruptibly(); | |
Scanner scanner = hbase.newScanner("query"); | |
scanner.setFamily("dims"); | |
scanner.setQualifier("val"); | |
Deferred<ArrayList<ArrayList<KeyValue>>> res; | |
Deferred<String[]> values; | |
ArrayList<Deferred<String[]>> result = | |
new ArrayList<Deferred<String[]>>(); | |
res = scanner.nextRows(); | |
result.add(res.addCallback(new ResultHandler())); | |
In the block shown as below. This is an endless loop. | |
//while ((res = scanner.nextRows(5)) != null) { | |
// result.add(res.addCallback(new ResultHandler())); | |
// res = null; | |
//} | |
for (Deferred<String[]> dfs : result) { | |
String[] strs = dfs.join(); | |
for (String str : strs) { | |
System.out.println(str); | |
} | |
} | |
//ArrayList<ArrayList<KeyValue>> res; | |
//while ((res = scanner.nextRows().joinUninterruptibly()) != null ) { | |
// for (ArrayList<KeyValue> arr : res) { | |
// for (KeyValue kv : arr) { | |
// System.out.println(new String(kv.value(), | |
// Charset.forName("ISO-8859-1"))); | |
// } | |
// } | |
//} | |
scanner.close().joinUninterruptibly(); | |
hbase.shutdown().joinUninterruptibly(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment