Skip to content

Instantly share code, notes, and snippets.

@Sathiyarajan
Forked from Saberko/HBaseOp.java
Created January 3, 2018 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sathiyarajan/fb714a1ee86e6decb111d861b465f187 to your computer and use it in GitHub Desktop.
Save Sathiyarajan/fb714a1ee86e6decb111d861b465f187 to your computer and use it in GitHub Desktop.
HBase api curd操作
package learn;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.Iterator;
/**
* Created by chendan on 16-8-9.
*/
public class HBaseOp {
public static void main (String[] args) {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "localhost");
try {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
// 创建
TableName userTable = TableName.valueOf("user");
HTableDescriptor tableDesc = new HTableDescriptor(userTable);
tableDesc.addFamily(new HColumnDescriptor("basic".getBytes()));
System.out.println("creating table `user`...");
if (admin.tableExists(userTable)) {
admin.disableTable(userTable);
admin.deleteTable(userTable);
}
admin.createTable(tableDesc);
System.out.println("Finished...");
// 插入一条数据
System.out.println("creating a record...");
Table table = connection.getTable(userTable);
Put user = new Put("001".getBytes()); // set key
user.addColumn("basic".getBytes(), "name".getBytes(), "chendan".getBytes()); // set value
table.put(user);
System.out.println("Finished...");
// 读取数据
System.out.println("getting a record...");
Get userGet = new Get("001".getBytes());
Result result = table.get(userGet); // 使用table.get(List<Get>) 批量获取
String name = Bytes.toString(result.getValue("basic".getBytes(), "name".getBytes()));
System.out.println("id: 001, name: " + name);
// 扫描表
Scan scan = new Scan();
scan.addColumn("basic".getBytes(), "name".getBytes());
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while (iterator.hasNext()) {
Result r = iterator.next();
System.out.print("row: " + Bytes.toString(r.getRow()) + "\t"); // row key
System.out.println("value: " + Bytes.toString(r.getValue("basic".getBytes(), "name".getBytes())));
}
scanner.close();
// 删除数据
System.out.println("Delete a record...");
Delete delete = new Delete("001".getBytes());
delete.addColumn("basic".getBytes(), "name".getBytes());
table.delete(delete);
System.out.println("Finished...");
if (table != null) table.close();
connection.close();
} catch (IOException e) {
System.err.println("connection failed");
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment