用java创建hbase表,在用java语言给hbase添加用户名、性别和密码,最后用java语言写一个根据用户名和密码的登录功能
时间: 2024-02-06 13:12:35 浏览: 74
好的,下面是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代码示例,可以根据实际情况进行修改和调整。
阅读全文