package com.cma.hbase.test;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.io.FileUtils;

import org.apache.commons.lang.StringUtils;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.KeyValue;

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.Put;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.util.Bytes;

import com.cma.hbase.Constants;

import com.cma.hbase.entity.DataModel;

import com.cma.hbase.tools.HbaseUtils;

public class HbaseTest {

public static void main(String[] args) {

        创建一张表

        createTable("hello_baby",new String[]{"code","ws","wd","t","ps","rh","vis","r"});

        写入一条数据

        writeRecord("hello_baby","row1","code","","code");

        writeRecord("hello_baby","row1","ws","","ws");

        writeRecord("hello_baby","row1","wd","","wd");

        writeRecord("hello_baby","row1","t","","t");

        writeRecord("hello_baby","row1","ps","","ps");

        writeRecord("hello_baby","row1","rh","","rh");

        writeRecord("hello_baby","row1","vis","","vis");

        writeRecord("hello_baby","row1","r","","r");

        写入一组数据

        writeRecordList("hello_baby");

        //查询出一条数据

        getRecord("hello_baby","row1");

        删除一行

        deleteRecord("hello_baby","row1");

        删除一张表

        dropTable("hello_baby");

        清空一张表

        clearTable("hello_baby");

    }

/**

     * 清空一张表

     * @param string

     */

    private static void clearTable(String tableName) {

        Configuration cfg = HbaseUtils.getCfg();

        

        try {

            HBaseAdmin admin = new HBaseAdmin(cfg);

            

        } catch (MasterNotRunningException e) {

            e.printStackTrace();

        } catch (ZooKeeperConnectionException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }finally{

            System.out.println("tableName: "+tableName+" drop over!");

        }

    }

/**

     * 写入一组数据

     * @param tableName

     */

    private static void writeRecordList(String tableName) {

        Long start = System.currentTimeMillis();

        Configuration cfg = HbaseUtils.getCfg();

        try {

            HTable htable = new HTable(cfg,tableName);

            List<Put> puts = getPuts();

            System.out.println(puts.size());

            htable.put(puts);

        } catch (IOException e) {

            e.printStackTrace();

        }finally{

            System.out.println("tableName:"+tableName+" write over!");

        }

        Long end = System.currentTimeMillis();

        System.out.println("cost time: "+(end -start));

    }

private static List<Put> getPuts() {

        List<Put> putList = new ArrayList<Put>();

        List<Put> putRecord = null;

        try {

            List<String> lines = FileUtils.readLines(new File("/home/guest/data/201307310800.csv"));

            for(int i = 1 ;i < lines.size();i++){

                putRecord = getPutsByLine(lines.get(i));

                putList.addAll(putRecord);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        return putList;

    }

/**

     * 获得一组Put

     * @param line

     * @return

     */

    private static List<Put> getPutsByLine(String line) {

        List<Put> puts = new ArrayList<Put>();

        Put put = null;

        

        if(StringUtils.isNotBlank(line)){

            String[] columns = line.split(",");

            String[] families = Constants.FAMILIES;

            String rowKey = "201307310800"+columns[0];

            for(int i = 0;i < columns.length;i++){

                String family = families[i];

                String qualifier = "";

                String value = columns[i];

                put = getPut(rowKey,family,qualifier,value);

                puts.add(put);

            }

        }

        return puts;

    }

/**

     * 组装一个Put

     * @param rowKey

     * @param family

     * @param qualifier

     * @param value

     * @return

     */

    private static Put getPut(String rowKey, String family, String qualifier,

            String value) {

        Put put = new Put(Bytes.toBytes(rowKey));

        put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));

        return put;

    }

/**

     * 查询出一条数据

     * @param tableName

     * @param rowKey

     */

    private static void getRecord(String tableName, String rowKey) {

        Configuration cfg = HbaseUtils.getCfg();

        try {

            HTable htable = new HTable(cfg,tableName);

            Get get = new Get(Bytes.toBytes(rowKey));

            Result rs = htable.get(get);

            

            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(new String(kv.getValue()));

                System.out.println();

            }

            

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

/**

     * 删除一张表

     * @param tableName

     */

    private static void dropTable(String tableName) {

        Configuration cfg = HbaseUtils.getCfg();

        

        try {

            HBaseAdmin admin = new HBaseAdmin(cfg);

            admin.disableTable(tableName);

            admin.deleteTable(tableName);

        } catch (MasterNotRunningException e) {

            e.printStackTrace();

        } catch (ZooKeeperConnectionException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }finally{

            System.out.println("tableName: "+tableName+" drop over!");

        }

    }

/**

     * 删除一行

     * @param tableName

     * @param rowKey

     */

    private static void deleteRecord(String tableName, String rowKey) {

        Configuration cfg = HbaseUtils.getCfg();

        

        try {

            HTable htable = new HTable(cfg,tableName);

            Delete del = new Delete(Bytes.toBytes(rowKey));

            htable.delete(del);

        } catch (IOException e) {

            e.printStackTrace();

        }finally{

            System.out.println("rowKey: "+rowKey+" delete over!");

        }

    }

/**

     * 存储一列

     * @param tableName

     * @param rowKey

     * @param family

     * @param qualifier

     * @param value

     */

    private static void writeRecord(String tableName,String rowKey,String family,String qualifier,String value) {

        Configuration cfg = HbaseUtils.getCfg();

        try {

            HTable htable = new HTable(cfg,tableName);

            Put put = new Put(Bytes.toBytes(rowKey));

            put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));

            htable.put(put);

            

        } catch (IOException e) {

            e.printStackTrace();

        }finally{

            System.out.println("family: "+family+" put over!");

        }

    }

/**

     * 创建一张表

     * @param tableName

     * @param families

     */

    private static void createTable(String tableName,String[] families) {

        HBaseAdmin admin = null;

        try {

            Configuration cfg = HbaseUtils.getCfg();

            System.out.println(cfg);

            admin = new HBaseAdmin(cfg);

            if(admin.tableExists(tableName)){

                System.out.println("表:"+tableName+" 已经存在!");

            }else{

                HTableDescriptor tableDesc = new HTableDescriptor(tableName);

                for(String column : families){

                    tableDesc.addFamily(new HColumnDescriptor(column));

                }

                admin.createTable(tableDesc);

            }

        } catch (MasterNotRunningException e) {

            e.printStackTrace();

        } catch (ZooKeeperConnectionException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }finally{

            System.out.println("over!");

        }

    }

}

JAVA操作Hbase基础例子的更多相关文章

  1. Hbase深入学习(六) Java操作HBase

    Hbase深入学习(六) ―― Java操作HBase 本文讲述如何用hbase shell命令和hbase java api对hbase服务器进行操作. 先看以下读取一行记录hbase是如何进行工作 ...

  2. Java操作hbase总结

    用过以后,总得写个总结,不然,就忘喽. 一.寻找操作的jar包. java操作hbase,首先要考虑到使用hbase的jar包. 因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到 ...

  3. java操作Hbase实例

    所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...

  4. 错误: 找不到或无法加载主类 java操作hbase出错

    用java操作hbase 利用maven引入hbase包后发现无法启动程序,然后网上说是包的冲突. 我引入了下面三个包然后程序就不能运行了. <dependency> <groupI ...

  5. Java 操作 HBase 教程

    Java 操作 HBase 教程 一.简介 二.hbase-client 引入 三.连接操作 四.表操作 五.运行测试 相关博文原文地址: 博客园:美码师:HBase(2) Java 操作 HBase ...

  6. HBase(2) Java 操作 HBase 教程

    目录 一.简介 二.hbase-client 引入 三.连接操作 四.表操作 五.运行测试 FAQ 参考文档 一.简介 在上一篇文章 HBase 基础入门 中,我们已经介绍了 HBase 的一些基本概 ...

  7. HBASE学习d端口master:16010(java操作hbase)https://www.cnblogs.com/junrong624/p/7323483.html

    HBase提示已创建表,但是list查询时,却显示表不存在. https://blog.csdn.net/liu16659/article/details/80216085 下载网址 http://a ...

  8. 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  9. (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

随机推荐

  1. Effective C++ 条款39

    我从本条款中学到了下面内容: 1.private继承不同于另外两种继承,派生类对象不能隐式转换为基类对象. 例如以下代码: class Bird//鸟 { }; class ostrich:priva ...

  2. 1.自己写一个计算器demo

    知识点: 1.System.Math.Pow() 实现乘方 2.实现计算器的运算优先级,依次调用的流程 问题: 还未实现“()”功能 解决方案 UI:

  3. Windows 无法启动xx服务 错误1053:服务没有及时响应启动或控制请求

    症状:win7系统的很多系统关键服务,启动不了,双击该服务也弹不了操作框,系统服务是设置为自动 的,但是就是启动不了,在本地服务窗口中只能启动该服务,但是双击会弹不了窗口,你点启动后会出现错误提示10 ...

  4. java线程之停止线程

         在Java中有以下3种方法可以终止一个正在运行的线程:      1.使用退出标志,是线程正常退出,也就是run方法完成后线程终止.      2.使用stop方法强制终止线程,但不推荐使用 ...

  5. 使用IDENTITY列属性和Sequence对象

    使用IDENTITY列属性 1. 建立表 Sales.MyOrders USE TSQL2012; IF OBJECT_ID(N'Sales.MyOrders', N'U') IS NOT NULL ...

  6. 子进程管理模块subprocess

    subprocess模块允许你生成子进程,连接管道,并获取返回的代码. 一.使用subprocess模块 模块中定义了一个Popen类:       subprocess.Popen(args, bu ...

  7. r语言之散点图绘制及参数

    一个简单的例子: > plot(cars$dist~cars$speed,+ main="车位移与速度的关系",+ xlab="速度",+ ylab=&q ...

  8. QT无标题窗口在任务栏显示关闭(增加系统菜单)

    在对话框中使用了如下代码: setWindowFlags(Qt::FramelessWindowHint); 在任务栏上右键点击程序,不会弹出菜单,解决办法,使用下面代码: setWindowFlag ...

  9. 数据切分——Atlas读写分离Mysql集群的搭建

    关于数据切分的原理可以参见博客: http://blog.csdn.net/jhq0113/article/details/44226789 关于Atlas的介绍可以参见博客: http://blog ...

  10. iOS开发 日常错误积累

    1.ios7 tableviewcell上面加入一个view,view上面有button,点击button不运行button的点击事件 解决的方法: self.view.userInteraction ...