| | 6 | package tsmc; |
| | 7 | |
| | 8 | import java.io.IOException; |
| | 9 | |
| | 10 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| | 11 | import org.apache.hadoop.hbase.client.HTable; |
| | 12 | import org.apache.hadoop.hbase.client.Put; |
| | 13 | import org.apache.hadoop.hbase.util.Bytes; |
| | 14 | |
| | 15 | public class PutData { |
| | 16 | public PutData(String tablename, String row, String family, String column, |
| | 17 | String value) { |
| | 18 | try { |
| | 19 | putData(tablename, row, family, column, value); |
| | 20 | } catch (IOException e) { |
| | 21 | e.printStackTrace(); |
| | 22 | } |
| | 23 | } |
| | 24 | |
| | 25 | static public void putData(String tablename, String row, String family, |
| | 26 | String column, String value) throws IOException { |
| | 27 | // HBaseConfiguration 能接收 hbase-site.xml 的設定值 |
| | 28 | HBaseConfiguration config = new HBaseConfiguration(); |
| | 29 | // 檔案的操作則使用 HBaseAdmin |
| | 30 | HTable table = new HTable(config, tablename); |
| | 31 | |
| | 32 | byte[] brow = Bytes.toBytes(row); |
| | 33 | byte[] bfamily = Bytes.toBytes(family); |
| | 34 | byte[] bcolumn = Bytes.toBytes(column); |
| | 35 | byte[] bvalue = Bytes.toBytes(value); |
| | 36 | |
| | 37 | Put p = new Put(brow); |
| | 38 | p.add(bfamily, bcolumn, bvalue); |
| | 39 | |
| | 40 | table.put(p); |
| | 41 | System.out.println("Put data :\"" + value + "\" to Table: " + tablename |
| | 42 | + "'s " + family + ":" + column); |
| | 43 | |
| | 44 | table.close(); |
| | 45 | } |
| | 46 | |
| | 47 | static public void main(String argv[]) throws IOException { |
| | 48 | |
| | 49 | putData("testtable", "row221", "f1", "c1", "my value good"); |
| | 50 | } |
| | 51 | } |