Eclipse远程连接HBase
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Eclipse远程连接HBase,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2424字,纯文字阅读大概需要4分钟。
内容图文
在Eclipse下新建一个Map/Reduce项目,并将以下jar添加到Build path:
程序代码:
package thathbase; import java.io.IOException; import java.util.Random; 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.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; public class HelloHbase { static Configuration conf; static HBaseAdmin admin; static Connection conn; static HTableDescriptor tableDescriptor; static HTable table; static Put putRow1; static Put putRow2; static Random rand =new Random(25); publicstaticvoid main(String[] args) throws Exception { // TODO Auto-generated method stub init(); createTable(); insertTable(); } privatestaticvoid init() throws Exception { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum", "master,slave03,slave04"); conf.set("hbase.master", "master:60000"); conn = ConnectionFactory.createConnection(conf); admin = (HBaseAdmin) conn.getAdmin(); } @SuppressWarnings("deprecation") privatestaticvoid createTable() throws Exception{ if (!admin.tableExists("scores")){ tableDescriptor = new HTableDescriptor("scores".getBytes()); tableDescriptor.addFamily(new HColumnDescriptor("fam1")); admin.createTable(tableDescriptor); } else{ System.out.println("Table already exists!"); } table = new HTable(conf, "scores"); } @SuppressWarnings("deprecation") privatestaticvoid insertTable() throws IOException{ putRow1 = new Put("row1".getBytes()); putRow2 = new Put("row2".getBytes()); for (int i =0; i<1000; i++){ if (rand1() == 1){ putRow1.add("fam1".getBytes(),"col1".getBytes(),String.valueOf(rand2()).getBytes()); table.put(putRow1); System.out.println(i + ":Insert into col1."); } else{ putRow2.add("fam1".getBytes(),"col2".getBytes(),String.valueOf(rand2()).getBytes()); table.put(putRow2); System.out.println(i + ":Insert into col2."); } } } privatestaticint rand1(){ int r = rand.nextInt(100); if (r < 50) return 1; elsereturn 2; } privatestaticint rand2(){ int r = rand.nextInt(10000); return r; } }
以上程序调用HBAse的API,实现了新建一张表,并随机向表里插入数据。
原文:http://www.cnblogs.com/mstk/p/6719164.html
内容总结
以上是互联网集市为您收集整理的Eclipse远程连接HBase全部内容,希望文章能够帮你解决Eclipse远程连接HBase所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。