eclipse下开发HBase程序
来源:互联网 发布:xp深度优化工具 编辑:程序博客网 时间:2024/05/23 10:10
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");
}
}
- eclipse下开发HBase程序
- storm-hbase集成:win7下用eclipse开发ubuntu上的storm-hbase程序
- Windows 下Eclipse运行HBase程序
- Hadoop-06-使用Eclipse开发HBase程序
- eclipse下开发php程序
- eclipse 下 开发android程序
- Eclipse下开发C++程序
- eclipse下hadoop程序开发
- Windows7+Eclipse环境下Hbase Java客户端的开发
- Linux下搭建Eclipse+Maven的Hbase开发环境
- Python开发HBase程序
- HBase总结(十四)LINUX下用Eclipse构建HBase开发环境
- HBase总结(十四)LINUX下用Eclipse构建HBase开发环境
- HBase总结(十四)LINUX下用Eclipse构建HBase开发环境
- lomboz-eclipse下开发Servlet程序
- Windows下使用Eclipse开发Hadoop程序
- Windows下使用Eclipse开发Hadoop程序
- 开发mysql程序(linux下eclipse)
- Java两个月速成(3)——方法&包
- 几块钱我就放开全景网风口浪尖巧克力
- Qt学习笔记2014.7.16.15:18——打印文件
- 去掉html代码方法
- BootStrap dateRangePicker时间范围控件
- eclipse下开发HBase程序
- 在iis上部署asp mvc5 程序
- C++笔记
- Cannot find or open the PDB file
- C - 排列(1.5.9)
- pre-paid card from Canada
- ubuntu下编译适合ndk的faac
- Android中"再按一次返回键退出程序"的实现(返回桌面)
- [POSIX线程模型]_[使用pthread对工作线程进行简单控制-暂停-继续-停止]