hbase的shell操作和java操作
2016-04-28 22:53
651 查看
shell操作
java操作
#创建数据库表:“student” create 'student','info','course' #添加第一行数据 put 'student','zpc','info:age','20' put 'student','zpc','info:sex','boy' put 'student','zpc','course:china','97' put 'student','zpc','course:math','97' put 'student','zpc','course:english','97' #添加第二行数据 put 'student','henjun','info:age','19' put 'student','henjun','info:sex','boy' put 'student','henjun','course:china','90' put 'student','henjun','course:math','120' put 'student','henjun','course:english','90' #添加第三行数据 put 'student','niaopeng','info:age','18' put 'student','niaopeng','info:sex','girl' put 'student','niaopeng','course:china','100' put 'student','niaopeng','course:math','100' put 'student','niaopeng','course:english','99' #获取一行数据 get 'student','zpc' #获取所有数据 scan 'student' #删除一行数据 deleteall 'student','zpc' #删除某个一行的某个列族 #删除某个一行的某个列族的某个列 #修改数据库 #删除数据库 disable 'student' drop 'student'
java操作
package test; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.TableName; 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.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class FamilyTest { // 声明静态配置 static Configuration conf = null; static { //配置自己HBase的环境,其实主要是ZOOKEEPER的一些参数 conf = HBaseConfiguration.create(); //conf.set(HConstants.ZOOKEEPER_QUORUM, "hadoop2,hadoop3,hadoop4,hadoop5"); //conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "2222");2181 conf.set("hbase.master", "101.200.200.114"); conf.set("hbase.zookeeper.property.clientPort", "3351"); conf.set("hbase.zookeeper.quorum", "101.200.200.114"); } /** * @param args * @throws ZooKeeperConnectionException * @throws MasterNotRunningException */ public static void main(String[] args) throws Exception { String tableName = "student"; // 第一步:创建数据库表:“student” String[] columnFamilys = { "info", "course" }; //create 'student','info','course' createTable(tableName, columnFamilys); // 第二步:向数据表的添加数据 if (isExist(tableName)) { // 添加第一行数据 //表名,行,列族,列,value //put 'student','zpc','info:age','20' addRow(tableName, "zpc", "info", "age", "20"); addRow(tableName, "zpc", "info", "sex", "boy"); addRow(tableName, "zpc", "course", "china", "97"); addRow(tableName, "zpc", "course", "math", "128"); addRow(tableName, "zpc", "course", "english", "85"); // 添加第二行数据 //put 'student','henjun','info:age','19' addRow(tableName, "henjun", "info", "age", "19"); addRow(tableName, "henjun", "info", "sex", "boy"); addRow(tableName, "henjun", "course", "china","90"); addRow(tableName, "henjun", "course", "math","120"); addRow(tableName, "henjun", "course", "english","90"); // 添加第三行数据 addRow(tableName, "niaopeng", "info", "age", "18"); addRow(tableName, "niaopeng", "info", "sex","girl"); addRow(tableName, "niaopeng", "course", "china","100"); addRow(tableName, "niaopeng", "course", "math","100"); addRow(tableName, "niaopeng", "course", "english","99"); // 第三步:获取一条数据 System.out.println("**************获取一条(zpc)数据*************"); //get 'student','zpc' getRow(tableName, "zpc"); // 第四步:获取所有数据 System.out.println("**************获取所有数据***************"); //scan 'student' getAllRows(tableName); // 第五步:删除一行数据 System.out.println("************删除一条(zpc)数据************"); //deleteall 'student','zpc' delRow(tableName, "zpc"); getAllRows(tableName); // 第六步:删除多条数据 System.out.println("**************删除多条数据***************"); String rows[] = new String[] { "qingqing","xiaoxue" }; delMultiRows(tableName, rows); getAllRows(tableName); // 第七步:删除数据库 System.out.println("***************删除数据库表**************"); deleteTable(tableName); System.out.println("表"+tableName+"存在吗?"+isExist(tableName)); } else { System.out.println(tableName + "此数据库表不存在!"); } } // 创建数据库表 public static void createTable(String tableName, String[] columnFamilys) throws Exception { // 新建一个数据库管理员 HBaseAdmin hAdmin = new HBaseAdmin(conf); if (hAdmin.tableExists(tableName)) { System.out.println("表 "+tableName+" 已存在!"); System.exit(0); } else { // 新建一个students表的描述 HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName)); // 在描述里添加列族 for (String columnFamily : columnFamilys) { tableDesc.addFamily(new HColumnDescriptor(columnFamily)); } // 根据配置好的描述建表 hAdmin.createTable(tableDesc); System.out.println("创建表 "+tableName+" 成功!"); hAdmin.close(); } } // 删除数据库表 //disable 'student' //drop 'student' public static void deleteTable(String tableName) throws Exception { // 新建一个数据库管理员 HBaseAdmin hAdmin = new HBaseAdmin(conf); if (hAdmin.tableExists(tableName)) { // 关闭一个表 hAdmin.disableTable(tableName); hAdmin.deleteTable(tableName); System.out.println("删除表 "+tableName+" 成功!"); } else { System.out.println("删除的表 "+tableName+" 不存在!"); System.exit(0); } } // 添加一条数据 //put 'student','zpc', ' info:age ','20' public static void addRow(String tableName, String row, String columnFamily, String column, String value) throws Exception { HTable table = new HTable(conf, tableName); Put put = new Put(Bytes.toBytes(row));// 指定行 // 参数分别:列族、列、值 put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); } // 获取一条数据 //get 'student','zpc' public static void getRow(String tableName, String row) throws Exception { HTable table = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); // 输出结果,raw方法返回所有keyvalue数组 for (Cell cell : result.rawCells()) { System.out.print("行名:" + new String(CellUtil.cloneRow(cell)) + " "); System.out.print("时间戳:" + cell.getTimestamp() + " "); System.out.print("列族名:" + new String(CellUtil.cloneFamily(cell)) + " "); System.out.print("列名:" + new String(CellUtil.cloneQualifier(cell)) + " "); System.out.println("值:" + new String(CellUtil.cloneValue(cell))); } table.close(); } //判断表是否存在 private static boolean isExist(String tableName) throws IOException { HBaseAdmin hAdmin = new HBaseAdmin(conf); return hAdmin.tableExists(tableName); } // 获取所有数据 public static void getAllRows(String tableName) throws Exception { HTable table = new HTable(conf, tableName); Scan scan = new Scan(); ResultScanner results = table.getScanner(scan); // 输出结果 for (Result result : results) { //一行结果 for (Cell cell : result.rawCells()) { System.out.print("行名:" + new String(CellUtil.cloneRow(cell)) + " "); System.out.print("时间戳:" + cell.getTimestamp() + " "); System.out.print("列族名:" + new String(CellUtil.cloneFamily(cell)) + " "); System.out.print("列名:" + new String(CellUtil.cloneQualifier(cell)) + " "); System.out.println("值:" + new String(CellUtil.cloneValue(cell))); } } } // 删除一条(行)数据 public static void delRow(String tableName, String row) throws Exception { HTable table = new HTable(conf, tableName); Delete del = new Delete(Bytes.toBytes(row)); table.delete(del); } // 删除多条数据 public static void delMultiRows(String tableName, String[] rows) throws Exception { HTable table = new HTable(conf, tableName); List<Delete> delList = new ArrayList<Delete>(); for (String row : rows) { Delete del = new Delete(Bytes.toBytes(row)); delList.add(del); } table.delete(delList); } }
相关文章推荐
- Apache OpenMeetings 会议系统getshell初探 - 2016-04-28 16:48
- Linux Shell 04 数字/字符串/文件测试
- Shell编程基础
- Smallp0wnedShell发布——Small modification version of p0werShell
- Shell编程入门总结(一)
- Linux Shell 06 数学运算
- Linux Shell 05 位置变量(命令行参数)
- linux BASH_SOURCE[0]
- Shell脚本学习笔记(一)
- LINUX-SHELL之数字运算
- 解决Xshell等Linux终端连接MySQL中文乱码问题
- Linux-SHELL中sh和bash的区别
- shell 编程实例学习
- Linux-Shell脚本中单引号和双引号区别
- shell下的单引号与双引号
- shell 脚本文件Windows传到Linux后编码问题
- Linux shell脚本的字符串截取
- Linux下使Shell 命令脱离终端在后台运行
- linux shell判断程序是否运行,没有运行则运行程序
- shell里的` ` $( ) ${ } expr $(( ))