1 1 import java.io.IOException; 2 2 import org.apache.hadoop.conf.Configuration; 3 3 import org.apache.hadoop.hbase.HBaseConfiguration; 4 4 import org.apache.hadoop.hbase.TableName; 5 5 import org.apache.hadoop.hbase.client.Admin; 6 6 import org.apache.hadoop.hbase.client.Connection; 7 7 import org.apache.hadoop.hbase.client.ConnectionFactory; 8 8 import org.apache.hadoop.hbase.client.Put; 9 9 import org.apache.hadoop.hbase.Cell; 10 10 import org.apache.hadoop.hbase.CellUtil; 11 11 import org.apache.hadoop.hbase.client.Get; 12 12 import org.apache.hadoop.hbase.client.Result; 13 13 import org.apache.hadoop.hbase.client.Table; 14 14 15 15 16 16 public class HBaseTest2 { 17 17 public static Configuration configuration; 18 18 public static Connection connection; 19 19 public static Admin admin; 20 20 21 21 public static void main(String[] args) { 22 22 configuration=HBaseConfiguration.create(); 23 23 configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase"); 24 24 25 25 /* 26 26 try { 27 27 connection=ConnectionFactory.createConnection(configuration); 28 28 admin=connection.getAdmin(); 29 29 insertRow("Student","scofield","score","English","45"); 30 30 insertRow("Student","scofield","score","Math","89"); 31 31 insertRow("Student","scofield","score","Computer","100"); 32 32 System.out.println("插入成功!---李运辰"); 33 33 } catch (IOException e) { 34 34 e.printStackTrace(); 35 35 } 36 36 */ 37 37 configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase"); 38 38 try { 39 39 connection=ConnectionFactory.createConnection(configuration); 40 40 admin=connection.getAdmin(); 41 41 getData("Student","scofield","score","English"); 42 42 System.out.println("输出完成!---李运辰"); 43 43 } catch (IOException e) { 44 44 e.printStackTrace(); 45 45 } 46 46 47 47 48 48 49 49 50 50 close(); 51 51 } 52 52 53 53 public static void insertRow(String tableName, String rowKey, String colFamily, String col, String val) { 54 54 try { 55 55 Table table = connection.getTable(TableName.valueOf(tableName)); 56 56 Put put = new Put(rowKey.getBytes()); 57 57 put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes()); 58 58 table.put(put); 59 59 table.close(); 60 60 } catch (IOException e) { 61 61 // TODO Auto-generated catch block 62 62 e.printStackTrace(); 63 63 } 64 64 65 65 } 66 66 67 67 68 68 public static void getData(String tableName, String rowKey, String colFamily, String col) { 69 69 try { 70 70 Table table = connection.getTable(TableName.valueOf(tableName)); 71 71 Get get=new Get(rowKey.getBytes()); 72 72 get.addColumn(colFamily.getBytes(), col.getBytes()); 73 73 Result result=table.get(get); 74 74 showCell(result); 75 75 table.close(); 76 76 } catch (IOException e) { 77 77 // TODO Auto-generated catch block 78 78 e.printStackTrace(); 79 79 } 80 80 81 81 } 82 82 83 83 private static void showCell(Result result) { 84 84 Cell[] cells=result.rawCells(); 85 85 for(Cell cell:cells) { 86 86 System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); 87 87 System.out.println("Timetamp:"+cell.getTimestamp()+" "); 88 88 System.out.println("column Family"+new String(CellUtil.cloneFamily(cell))+" "); 89 89 System.out.println("row Name:"+new String(CellUtil.cloneValue(cell))+" "); 90 90 System.out.println("value"+new String(CellUtil.cloneValue(cell))+" "); 91 91 92 92 } 93 93 94 94 } 95 95 public static void close() { 96 96 97 97 try { 98 98 if (admin != null) { 99 99 admin.close(); 100100 } 101101 if(null!=connection) { 102102 connection.close(); 103103 } 104104 } catch (IOException e) { 105105 // TODO Auto-generated catch block 106106 e.printStackTrace(); 107107 } 108108 109109 } 110110 111111 }
Java_Habse_add
Wesley13
2021-10-11
1011 0 0
点赞
收藏
评论区
加载中...