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",