本文以HBase 0.90.2为例,介绍如何在Windows系统,Eclipse IDE集成环境下,使用Java语言,进行HBase客户端编程,包含建立表、删除表、插入记录、删除记录、各种方式下的查询操作等。
海沧网站建设公司创新互联建站,海沧网站设计制作,有大型网站制作公司丰富经验。已为海沧千余家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的海沧做网站的公司定做!
hbase.rootdir hdfs://hostname:9000/hbase hbase.cluster.distributed true hbase.zookeeper.quorum *.*.*.*, *.*.*.*, *.*.*.* hbase.defaults.for.version 0.90.2
- private static Configuration conf = null;
- /**
- * 初始化配置
- */
- static {
- conf = HBaseConfiguration.create();
- }
- /**
- * 创建表操作
- * @throws IOException
- */
- public void createTable(String tablename, String[] cfs) throws IOException {
- HBaseAdmin admin = new HBaseAdmin(conf);
- if (admin.tableExists(tablename)) {
- System.out.println("表已经存在!");
- }
- else {
- HTableDescriptor tableDesc = new HTableDescriptor(tablename);
- for (int i = 0; i < cfs.length; i++) {
- tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
- }
- admin.createTable(tableDesc);
- System.out.println("表创建成功!");
- }
- }
- /**
- * 删除表操作
- * @param tablename
- * @throws IOException
- */
- public void deleteTable(String tablename) throws IOException {
- try {
- HBaseAdmin admin = new HBaseAdmin(conf);
- admin.disableTable(tablename);
- admin.deleteTable(tablename);
- System.out.println("表删除成功!");
- } catch (MasterNotRunningException e) {
- e.printStackTrace();
- } catch (ZooKeeperConnectionException e) {
- e.printStackTrace();
- }
- }
- /**
- * 插入一行记录
- * @param tablename
- * @param cfs
- */
- public void writeRow(String tablename, String[] cfs) {
- try {
- HTable table = new HTable(conf, tablename);
- Put put = new Put(Bytes.toBytes("rows1"));
- for (int j = 0; j < cfs.length; j++) {
- put.add(Bytes.toBytes(cfs[j]),
- Bytes.toBytes(String.valueOf(1)),
- Bytes.toBytes("value_1"));
- table.put(put);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 删除一行记录
- * @param tablename
- * @param rowkey
- * @throws IOException
- */
- public void deleteRow(String tablename, String rowkey) throws IOException {
- HTable table = new HTable(conf, tablename);
- List list = new ArrayList();
- Delete d1 = new Delete(rowkey.getBytes());
- list.add(d1);
- table.delete(list);
- System.out.println("删除行成功!");
- }
- /**
- * 查找一行记录
- * @param tablename
- * @param rowkey
- */
- public static void selectRow(String tablename, String rowKey)
- throws IOException {
- HTable table = new HTable(conf, tablename);
- Get g = new Get(rowKey.getBytes());
- Result rs = table.get(g);
- for (KeyValue kv : rs.raw()) {
- System.out.print(new String(kv.getRow()) + " ");
- System.out.print(new String(kv.getFamily()) + ":");
- System.out.print(new String(kv.getQualifier()) + " ");
- System.out.print(kv.getTimestamp() + " ");
- System.out.println(new String(kv.getValue()));
- }
- }
- /**
- * 查询表中所有行
- * @param tablename
- */
- public void scaner(String tablename) {
- try {
- HTable table = new HTable(conf, tablename);
- Scan s = new Scan();
- ResultScanner rs = table.getScanner(s);
- for (Result r : rs) {
- KeyValue[] kv = r.raw();
- for (int i = 0; i < kv.length; i++) {
- System.out.print(new String(kv[i].getRow()) + " ");
- System.out.print(new String(kv[i].getFamily()) + ":");
- System.out.print(new String(kv[i].getQualifier()) + " ");
- System.out.print(kv[i].getTimestamp() + " ");
- System.out.println(new String(kv[i].getValue()));
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
当前名称:基于Java的HBase客户端编程
URL链接:http://www.csdahua.cn/qtweb/news35/50485.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网