大数据技术连载-04-2-大数据的管理-分布式数据库Hbase的JavaApi

1 创建表

       create ‘表名’,’列族名称1’, ’列族名称2’, ’列族名称3’,

2 向一个单元格赋值

       put  ’表名’,’行键’,’列族名称1:列名’,’值’

3 获得单元格的值

       get  ’表名’,‘行键’,{COLUMN=>’列族名称1:列名’}

4 禁用和删除

enable ’表名’

disable ’表名’

drop ’表名’

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
 
import java.io.IOException;
public class ExampleForHBase {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    public static void main(String[] args)throws IOException{
        init();
        createTable("student",new String[]{"score"});
        insertData("student","zhangsan","score","English","69");
        insertData("student","zhangsan","score","Math","86");
        insertData("student","zhangsan","score","Computer","77");
        getData("student", "zhangsan", "score","English");
        close();
    }
 
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
 
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
 
    public static void createTable(String myTableName,String[] colFamily) throws IOException {
        TableName tableName = TableName.valueOf(myTableName);
        if(admin.tableExists(tableName)){
            System.out.println("talbe is exists!");
        }else {
            TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
            for(String str:colFamily){
                ColumnFamilyDescriptor family = 
ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
                tableDescriptor.setColumnFamily(family);
            }
            admin.createTable(tableDescriptor.build());
        } 
    }
 
    public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { 
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes());
        table.put(put);
        table.close(); 
    }
 
    public static void getData(String tableName,String rowKey,String colFamily, String col)throws  IOException{ 
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result = table.get(get);
        System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
        table.close(); 
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是Java代码示例: 1. 创建HBase表 ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseTable { private static final String TABLE_NAME = "user_info"; private static final String COLUMN_FAMILY = "info"; private static final String[] COLUMN_QUALIFIERS = {"username", "gender", "password"}; public static void createTable() throws IOException { // 创建HBase配置对象 Configuration conf = HBaseConfiguration.create(); // 创建HBase连接对象 Connection conn = ConnectionFactory.createConnection(conf); // 创建HBase管理对象 Admin admin = conn.getAdmin(); // 创建表描述对象 TableName tableName = TableName.valueOf(TABLE_NAME); HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); // 创建列族描述对象 HColumnDescriptor columnDescriptor = new HColumnDescriptor(Bytes.toBytes(COLUMN_FAMILY)); // 设置列族的最大版本数和压缩方式 columnDescriptor.setMaxVersions(1); columnDescriptor.setCompressionType(Compression.Algorithm.NONE); // 将列族描述对象添加到表描述对象中 tableDescriptor.addFamily(columnDescriptor); // 创建表 admin.createTable(tableDescriptor); // 关闭资源 admin.close(); conn.close(); } } ``` 2. 插入数据 ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseInsert { private static final String TABLE_NAME = "user_info"; private static final String COLUMN_FAMILY = "info"; private static final String[] COLUMN_QUALIFIERS = {"username", "gender", "password"}; public static void insertData(String username, String gender, String password) throws IOException { // 创建HBase配置对象 Configuration conf = HBaseConfiguration.create(); // 创建HBase连接对象 Connection conn = ConnectionFactory.createConnection(conf); // 获取表对象 Table table = conn.getTable(TableName.valueOf(TABLE_NAME)); // 构造行键 String rowKey = username; // 创建Put对象,用于添加数据 Put put = new Put(Bytes.toBytes(rowKey)); // 添加列族、列和值 put.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(COLUMN_QUALIFIERS[0]), Bytes.toBytes(username)); put.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(COLUMN_QUALIFIERS[1]), Bytes.toBytes(gender)); put.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(COLUMN_QUALIFIERS[2]), Bytes.toBytes(password)); // 提交数据 table.put(put); // 关闭资源 table.close(); conn.close(); } } ``` 3. 登录功能 ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class HBaseLogin { private static final String TABLE_NAME = "user_info"; private static final String COLUMN_FAMILY = "info"; private static final String[] COLUMN_QUALIFIERS = {"username", "gender", "password"}; public static boolean login(String username, String password) throws IOException { // 创建HBase配置对象 Configuration conf = HBaseConfiguration.create(); // 创建HBase连接对象 Connection conn = ConnectionFactory.createConnection(conf); // 获取表对象 Table table = conn.getTable(TableName.valueOf(TABLE_NAME)); // 构造行键 String rowKey = username; // 创建Get对象,用于查询数据 Get get = new Get(Bytes.toBytes(rowKey)); // 添加列族和列 get.addColumn(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(COLUMN_QUALIFIERS[2])); // 查询数据 Result result = table.get(get); // 获取密码 String pwd = Bytes.toString(result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes(COLUMN_QUALIFIERS[2]))); // 关闭资源 table.close(); conn.close(); // 判断密码是否匹配 return pwd != null && pwd.equals(password); } } ``` 以上是Java代码示例,可以根据实际情况进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值