1.Student学生表
name | score | ||
English | Math | Computer | |
zhangsan | 69 | 86 | 77 |
lisi | 55 | 100 | 88 |
切换到hadoop用户,密码是hadoopsu hadoop
启动hadoopcd /usr/local/hadoopsbin/start-dfs.sh
启动 HBasecd usr/local/hbasebin/start-hbase.shbin/hbase shell
创建Student表格:create 'student','score'
插入上面表格数据:put 'student','zhangsan','score:English','69'put 'student','zhangsan','score:Math','86'put 'student','zhangsan','score:Computer','77'put 'student','lisi','score:English','55'put 'student','lisi','score:Math','100'put 'student','lisi','score:Computer','88'
结果为:

用scan指令浏览表的相关信息:scan 'student'

查询zhangsan 的Computer成绩:get'student','zhangsan','score:Computer'

修改lisi的Math成绩,改为95:put'student','lisi','score:Math','95'

2. Hbase API编程。
添加数据:English:45 Math:89 Computer:100
Name | English | Math | Computer |
Scofield | 45 | 89 | 100 |
1.
import java.io.IOException;2. import org.apache.hadoop.conf.Configuration;3. import org.apache.hadoop.hbase.HBaseConfiguration;4. import org.apache.hadoop.hbase.TableName;5. import org.apache.hadoop.hbase.client.Admin;6. import org.apache.hadoop.hbase.client.Connection;7. import org.apache.hadoop.hbase.client.ConnectionFactory;8. import org.apache.hadoop.hbase.client.Put;9. import org.apache.hadoop.hbase.client.Table;10.11.publicclass hbase_insert {12.13. /**14. * @param args15. */16. publicstaticConfiguration configuration;17. publicstaticConnection connection;18. publicstaticAdmin admin;19. publicstaticvoid main(String[] args){20. //TODO Auto-generated method stub21. configuration =HBaseConfiguration.create();22. configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");23. try{24. connection =ConnectionFactory.createConnection(configuration);25. admin = connection.getAdmin();26. }catch(IOException e){27. e.printStackTrace();28. }29. try{30. insertRow("student","scofield","score","English","45");31. insertRow("student","scofield","score","Math","89");32. insertRow("student","scofield","score","Computer","100");33. }catch(IOException e){34. //TODO Auto-generated catch block35. e.printStackTrace();36. }37. close();38. }39. publicstaticvoid insertRow(String tableName,String rowKey,String colFamily,40. String col,String val)throwsIOException{41. Table table =connection.getTable(TableName.valueOf(tableName));42. Put put =newPut(rowKey.getBytes());43. put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());44. table.put(put);45. System.out.println("insertRow:"+tableName +", "+ rowKey +", "+ colFamily +", "+ col +", "+ val);46. table.close();47. }48. publicstaticvoid close(){49. try{50. if(admin !=null){51. admin.close();52. }53. if(null!= connection){54. connection.close();55. }56. }catch(IOException e){57. e.printStackTrace();58. }59. }60.}
用scan输出数据库数据检验是否插入成功:
scan 'student'
3. 获取scofield的English成绩信息
1. import java.io.IOException;2. import org.apache.hadoop.conf.Configuration;3. import org.apache.hadoop.hbase.Cell;4. import org.apache.hadoop.hbase.CellUtil;5. import org.apache.hadoop.hbase.HBaseConfiguration;6. import org.apache.hadoop.hbase.TableName;7. import org.apache.hadoop.hbase.client.Admin;8. import org.apache.hadoop.hbase.client.Connection;9. import org.apache.hadoop.hbase.client.ConnectionFactory;10.import org.apache.hadoop.hbase.client.Get;11.import org.apache.hadoop.hbase.client.Put;12.import org.apache.hadoop.hbase.client.Result;13.import org.apache.hadoop.hbase.client.Table;14.15.publicclass hbase_query {16.17. /**18. * @param args19. */20. publicstaticConfiguration configuration;21. publicstaticConnection connection;22. publicstaticAdmin admin;23. publicstaticvoid main(String[] args){24. //TODO Auto-generated method stub25. configuration =HBaseConfiguration.create();26. configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");27. try{28. connection =ConnectionFactory.createConnection(configuration);29. admin = connection.getAdmin();30. }catch(IOException e){31. e.printStackTrace();32. }33. try{34. getData("student","scofield","score","English");35. }catch(IOException e){36. //TODO Auto-generated catch block37. e.printStackTrace();38. }39. close();40. }41. publicstaticvoid getData(String tableName,String rowKey,String colFamily,42. String col)throwsIOException{43. Table table =connection.getTable(TableName.valueOf(tableName));44. Getget=newGet(rowKey.getBytes());45. get.addColumn(colFamily.getBytes(),col.getBytes());46. Result result = table.get(get);47. showCell(result);48. table.close();49. }50. publicstaticvoid showCell(Result result){51. Cell[] cells = result.rawCells();52. for(Cell cell:cells){53. System.out.println("RowName:"+newString(CellUtil.cloneRow(cell))+" ");54. System.out.println("Timetamp:"+cell.getTimestamp()+" ");55. System.out.println("column Family:"+newString(CellUtil.cloneFamily(cell))+" ");56. System.out.println("row Name:"+newString(CellUtil.cloneQualifier(cell))+" ");57. System.out.println("value:"+newString(CellUtil.cloneValue(cell))+" ");58. }59. }60. publicstaticvoid close(){61. try{62. if(admin !=null){63. admin.close();64. }65. if(null!= connection){66. connection.close();67. }68. }catch(IOException e){69. e.printStackTrace();70. }71. }72.}
结果:

文章转载自糟老头修炼记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




