Changes between Version 1 and Version 2 of waue/2010/0204-02


Ignore:
Timestamp:
Feb 3, 2010, 9:51:05 PM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2010/0204-02

    v1 v2  
    1  = 範例二: Put資料進Column =
     1 = 範例二: Put 資料進 Column =
    22
    33
    44{{{
    55#!java
     6package tsmc;
     7
     8import java.io.IOException;
     9
     10import org.apache.hadoop.hbase.HBaseConfiguration;
     11import org.apache.hadoop.hbase.client.HTable;
     12import org.apache.hadoop.hbase.client.Put;
     13import org.apache.hadoop.hbase.util.Bytes;
     14
     15public 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}
    652
    753}}}