1.创建普通java project
2.导入外部jar包(右键工程名-->properties-->Libraries-->Add Ecternal JARs),jar包列表如下:
hbase/hbase.jar
hadoop/hadoop-common.jar
hbase/lib/commons-logging-1.1.1.jar
com.google.guava_1.6.0.jar
hbase/lib/commons-lang-2.5.jar
hbase/lib/commons-configuration-1.6.jar
hadoop/hadoop-auth-2.0.0-cdh4.2.2.jar
hbase/lib/slf4j-api-1.6.1.jar
hbase/lib/zookeeper.jar
com.google.protobuf-2.4.0.jar
slf4j-nop-1.7.6.jar
(部分jar包可以到我的资源中去下载)
3.样例程序(将外部命令的输出存入HBase中)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.Runtime;
import java.util.Vector;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class CMD {
public static void main(String[] args) {
CMD cmd = new CMD("testCMD");
cmd.myCMD();
}
private Configuration conf ;
private HBaseAdmin admin;
private HTable table;
CMD(String tablename){
Configuration cnf = new Configuration();
cnf.set("hbase.zookeeper.quorum", "your_master");
cnf.set("hbase.zookeeper.property.clientPort", "2181");
this.conf = HBaseConfiguration.create(cnf);
try {
this.admin = new HBaseAdmin(this.conf);
this.table = new HTable(this.conf, tablename);
this.table.setAutoFlush(false);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void insertData(String key, String data){
Put put = new Put(Bytes.toBytes(key));
put.setWriteToWAL(false);
put.add(Bytes.toBytes("cf"),Bytes.toBytes("cq"),Bytes.toBytes(data));
try {
this.table.put(put);
} catch (IOException e1) {
e1.printStackTrace();
}
}
public void myCMD(){
long startTime=System.currentTimeMillis();
try {
String cmd = "client"; //client是一个cpp可执行程序
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line, key="";
StringBuilder data = new StringBuilder();
line = bufferedReader.readLine();
while(line != null){
System.out.println("test: " + line.length() + "...");
if(line.startsWith("key:")){
key = line.substring(4);
insertData(key, data.substring(0, data.length() - 1));
System.out.println(key + ": " + data.length() + ", " + data.substring(0, data.length() - 1).length());
data.delete(0, data.length());
}
else{
data.append(line + "\n");
}
line = bufferedReader.readLine();
}
process.waitFor();
this.table.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
long endTime=System.currentTimeMillis();
System.out.println("Total time: " + (endTime-startTime) + "ms");
}
}