範例二: Put 資料進 Column
執行方法
測試用打包檔 tsmcHBase_100204.jar
$ /opt/hadoop/bin/hadoop jar tsmcHBase_100204.jar PutData
程式碼
package tsmc;
import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class PutData {
public PutData(String tablename, String row, String family, String column,
String value) {
try {
putData(tablename, row, family, column, value);
} catch (IOException e) {
e.printStackTrace();
}
}
static public void putData(String tablename, String row, String family,
String column, String value) throws IOException {
// HBaseConfiguration 能接收 hbase-site.xml 的設定值
HBaseConfiguration config = new HBaseConfiguration();
// 檔案的操作則使用 HBaseAdmin
HTable table = new HTable(config, tablename);
byte[] brow = Bytes.toBytes(row);
byte[] bfamily = Bytes.toBytes(family);
byte[] bcolumn = Bytes.toBytes(column);
byte[] bvalue = Bytes.toBytes(value);
Put p = new Put(brow);
p.add(bfamily, bcolumn, bvalue);
table.put(p);
System.out.println("Put data :\"" + value + "\" to Table: " + tablename
+ "'s " + family + ":" + column);
table.close();
}
static public void main(String argv[]) throws IOException {
putData("testtable", "row221", "f1", "c1", "my value good");
}
}