1.表插入数据(Put)
package client;
// cc PutExample Example application inserting data into HBase
// vv PutExample
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
// ^^ PutExample
import util.HBaseHelper;
// vv PutExample
import java.awt.*;
import java.io.IOException;
public class PutExample {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create(); // co PutExample-1-CreateConf Create the required configuration.
//conf.set("hbase.zookeeper.quorum", "hadoop1010,hadoop1011,hadoop1012");
conf.addResource("hbase-site.xml");
// ^^ PutExample
HBaseHelper helper = HBaseHelper.getHelper(conf);
helper.dropTable("testtable");
helper.createTable("testtable", "colfam1","colfam2");
// vv PutExample
Connection connection = ConnectionFactory.createConnection(conf);
//指定表名
Table table = connection.getTable(TableName.valueOf("testtable")); // co PutExample-2-NewTable Instantiate a new client.
//导入数据的rowkey
Put put = new Put(Bytes.toBytes("row1")); // co PutExample-3-NewPut Create put with specific row.
//设置导入指定列族:列,值
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
Bytes.toBytes("val100")); // co PutExample-4-AddCol1 Add a column, whose name is "colfam1:qual1", to the put.
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
Bytes.toBytes("val200")); // co PutExample-4-AddCol2 Add another column, whose name is "colfam1:qual2", to the put.
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"),
Bytes.toBytes("val300"));
put.addColumn(Bytes.toBytes("colfam2"), Bytes.toBytes("qual1"),
Bytes.toBytes("val300"));
//put入数据
table.put(put); // co PutExample-5-DoPut Store row with column into the HBase table.
//关闭表
table.close(); // co PutExample-6-DoPut Close table and connection instances to free resources.
//关闭连接
connection.close();
// ^^ PutExample
//关闭
helper.close();
// vv PutExample
}
}
// ^^ PutExample
2.查询(get)
package client;
// cc GetExample Example application retrieving data from HBase
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import util.HBaseHelper;
public class GetExample {
public static void main(String[] args) throws IOException {
// vv GetExample
Configuration conf = HBaseConfiguration.create(); // co GetExample-1-CreateConf Create the configuration.
conf.addResource("hdfs-site.xml");
// ^^ GetExample
HBaseHelper helper = HBaseHelper.getHelper(conf);
if (!helper.existsTable("testtable")) {
helper.createTable("testtable", "colfam1");
}
// vv GetExample
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable")); // co GetExample-2-NewTable Instantiate a new table reference.
Get get = new Get(Bytes.toBytes("row1")); // co GetExample-3-NewGet Create get with specific row.
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co GetExample-4-AddCol Add a column to the get.
Result result = table.get(get); // co GetExample-5-DoGet Retrieve row with selected columns from HBase.
byte[] val = result.getValue(Bytes.toBytes("colfam1"),
Bytes.toBytes("qual1")); // co GetExample-6-GetValue Get a specific value for the given column.
System.out.println("Value: " + Bytes.toString(val)); // co GetExample-7-Print Print out the value while converting it back.
table.close(); // co GetExample-8-Close Close the table and connection instances to free resources.
connection.close();
// ^^ GetExample
helper.close();
}
}
3.删除操作(delete)
package client;
// cc DeleteExample Example application deleting data from HBase
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import util.HBaseHelper;
public class DeleteExample {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HBaseHelper helper = HBaseHelper.getHelper(conf);
helper.dropTable("testtable");
helper.createTable("testtable", 100, "colfam1", "colfam2");
helper.put("testtable",
new String[] { "row1" },
new String[] { "colfam1", "colfam2" },
new String[] { "qual1", "qual1", "qual2", "qual2", "qual3", "qual3" },
new long[] { 1, 2, 3, 4, 5, 6 },
new String[] { "val1", "val1", "val2", "val2", "val3", "val3" });
System.out.println("Before delete call...");
helper.dump("testtable", new String[]{ "row1" }, null, null);
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));
// vv DeleteExample
Delete delete = new Delete(Bytes.toBytes("row1")); // co DeleteExample-1-NewDel Create delete with specific row.
delete.setTimestamp(1); // co DeleteExample-2-SetTS Set timestamp for row deletes.
delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co DeleteExample-3-DelColNoTS Delete the latest version only in one column.
delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), 3); // co DeleteExample-4-DelColTS Delete specific version in one column.
delete.addColumns(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co DeleteExample-5-DelColsNoTS Delete all versions in one column.
delete.addColumns(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), 2); // co DeleteExample-6-DelColsTS Delete the given and all older versions in one column.
delete.addFamily(Bytes.toBytes("colfam1")); // co DeleteExample-7-AddCol Delete entire family, all columns and versions.
delete.addFamily(Bytes.toBytes("colfam1"), 3); // co DeleteExample-8-AddCol Delete the given and all older versions in the entire column family, i.e., from all columns therein.
table.delete(delete); // co DeleteExample-9-DoDel Delete the data from the HBase table.
// ^^ DeleteExample
table.close();
connection.close();
System.out.println("After delete call...");
helper.dump("testtable", new String[] { "row1" }, null, null);
helper.close();
}
}