使用JAVA API编程实现下面内容:
1.创建《王者荣耀》游戏玩家信息表gamer,包含列族personalInfo(个人信息)、recordInfo(战绩信息)、assetsInfo(资产信息)。
2.向gamer表添加数据
| personalInfo | recordInfo | assetInfo | |
| nickname | gameID | ranking | integral |
row-001 | QGhappy.Snow | 000000 | One | 10000 |
row-002 | XQMaster | 111111 | Two | 20000 |
3.查询gamer表中所有数据
4.查找gamer表中行键为row-001,列键为personalInfo:nickname的内容
5.删除gamer表中行键为row-001,列键为personalInfo:nickname的内容
6.删除gamer表中行键为row-001的一行数据
7.删除gamer表。
1.启动Hadoop、Hbase
2.新建JAVA工程
3.导入habse/lib下的所有jarbao
4.新建HBaseEXample.class
import java.io.IOException; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; public class HBaseExample { /** * @param args */ public static Configuration configuration; public static Connection connection; public static Admin admin; public static void main(String[] args) throws IOException { init(); createTable("gamer",new String[] {"personalInfo","recordInfo","assetsInfo"}); insertData("gamer","row-001","personalInfo","nickname","QGhappy.Snow"); insertData("gamer","row-001","personalInfo","gameID","000000"); insertData("gamer","row-001","recordInfo","ranking","One"); insertData("gamer","row-001","recordInfo","intergral","10000"); insertData("gamer","row-002","personalInfo","nickname","XQMaster"); insertData("gamer","row-002","personalInfo","gameID","111111"); insertData("gamer","row-002","recordInfo","ranking","Two"); insertData("gamer","row-002","assetsInfo","intergral","20000"); //queryAll("gamer"); //query("gamer","row-001","personalInfo","nickname"); //deleteDate("gamer","row-001","personalInfo","nickname"); //delete("gamer"); close(); } //删除数据 private static void deleteDate(String tableName,String rowKey,String colFamily,String col) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(rowKey.getBytes()); if(colFamily!=null){ delete.addFamily(colFamily.getBytes()); } if(col!=null){ delete.addColumn(colFamily.getBytes(), col.getBytes()); } table.delete(delete); close(); System.out.println("数据成功删除!!!"); } //删除表 private static void delete(String tableName) throws IOException { TableName tableName2=TableName.valueOf(tableName); if(admin.tableExists(tableName2)){ admin.disableTable(tableName2); admin.deleteTable(tableName2); } System.out.println("table已删除!!!"); close(); } //查询 private static void query(String tableName,String rowKey,String colFamily,String col) throws IOException { Table table =connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); get.addColumn(colFamily.getBytes(), col.getBytes()); Result result =table.get(get); showCell(result); table.close(); } //查询整个表 private static void queryAll(String tableName) throws IllegalArgumentException, IOException { Table table = connection.getTable(TableName.valueOf(tableName.getBytes())); ResultScanner resultScanner = table.getScanner(new Scan()); if(resultScanner!=null){ for(Result r:resultScanner){ showCell(r); } }else { System.out.println("表中无数据!!!!"); } resultScanner.close(); } //打印到控制台的格式 private static void showCell(Result result) { Cell[] cells= result.rawCells(); for(Cell cell:cells){ System.out.println("RowName:"+new String(CellUtil.cloneRow(cell)+"")); System.out.println("Family:"+new String(CellUtil.cloneFamily(cell)+"")); System.out.println("Qualifier:"+new String(CellUtil.cloneQualifier(cell)+"")); System.out.println("Value:"+new String(CellUtil.cloneValue(cell)+"")); } } //插入 private static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(rowKey.getBytes()); put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes()); table.put(put); table.close(); System.out.println("数据成功导入!!!"); } //创建表 private static void createTable(String myTableName,String[] colFamily) throws IOException { TableName tableName = TableName.valueOf(myTableName); if(admin.tableExists(tableName)){ System.out.println(tableName+"table exists!"); }else{ HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); for(String str:colFamily){ HColumnDescriptor hColumnDescriptor= new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); } System.out.println(tableName+"成功创建!!!!"); } //初始化 public static void init(){ configuration =HBaseConfiguration.create(); configuration.set("hbase.rootfir", "hdfs://localhost:9000/hbase"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //关闭 private static void close() { try { if(admin!=null){ admin.close(); } if(null!=connection){ connection.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }