pull/2/head
qian 9 months ago
parent 4c7cb3b32e
commit 7105fc9e29

@ -4,12 +4,7 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="a7505764-040b-48e2-b2fc-8c5b579e595f" name="Default" comment="11.">
<change beforePath="$PROJECT_DIR$/.idea/libraries/Maven__org_apache_directory_api_api_asn1_api_1_0_0_M20.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/libraries/Maven__org_apache_directory_api_api_asn1_api_1_0_0_M20.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/libraries/Maven__org_apache_directory_api_api_util_1_0_0_M20.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/libraries/Maven__org_apache_directory_api_api_util_1_0_0_M20.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/libraries/Maven__org_apache_directory_server_apacheds_i18n_2_0_0_M15.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/libraries/Maven__org_apache_directory_server_apacheds_i18n_2_0_0_M15.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<list default="true" id="a7505764-040b-48e2-b2fc-8c5b579e595f" name="Default" comment="11" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -446,7 +441,7 @@
<workItem from="1529887211212" duration="6851000" />
<workItem from="1529994772485" duration="146000" />
<workItem from="1734352519788" duration="8741000" />
<workItem from="1734400464055" duration="668000" />
<workItem from="1734400464055" duration="1003000" />
</task>
<task id="LOCAL-00001" summary="1.环境搭建完成&#10;2.相关的工具类编写完成&#10;3.配置文件管理类编写完成">
<created>1529592741848</created>
@ -588,7 +583,14 @@
<option name="project" value="LOCAL" />
<updated>1734400912918</updated>
</task>
<option name="localTasksCounter" value="21" />
<task id="LOCAL-00021" summary="11">
<created>1734401354672</created>
<option name="number" value="00021" />
<option name="presentableId" value="LOCAL-00021" />
<option name="project" value="LOCAL" />
<updated>1734401354672</updated>
</task>
<option name="localTasksCounter" value="22" />
<servers />
</component>
<component name="TestHistory">
@ -721,7 +723,8 @@
<MESSAGE value="10" />
<MESSAGE value="." />
<MESSAGE value="11." />
<option name="LAST_COMMIT_MESSAGE" value="11." />
<MESSAGE value="11" />
<option name="LAST_COMMIT_MESSAGE" value="11" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/main/java/cn/edu/hust/conf/ConfigurationManager.java">

@ -17,39 +17,63 @@ import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructType;
/**
*
* DataFrame
* 便Spark SQL
* @author Administrator
*
*/
public class MockData {
/**
*
* @param sc
* @param sqlContext
* mockDataFrame
* JavaSparkContextSQLContextSparkSQL
* @param sc JavaSparkContextSparkRDD
* @param sqlContext SQLContextSparkSQLDataFrame
*/
public static void mock(JavaSparkContext sc,
SQLContext sqlContext) {
SQLContext sqlContext) {
// 创建一个用于存储Row对象的列表后续将用于构建DataFrame每个Row对象代表一行数据
List<Row> rows = new ArrayList<Row>();
// 定义一个字符串数组,包含了一些模拟的搜索关键词,用于模拟用户的搜索行为
String[] searchKeywords = new String[] {"火锅", "蛋糕", "重庆辣子鸡", "重庆小面",
"呷哺呷哺", "新辣道鱼火锅", "国贸大厦", "太古商场", "日本料理", "温泉"};
// 通过DateUtils工具类获取今天的日期作为模拟数据中日期相关字段的基础值
String date = DateUtils.getTodayDate();
// 定义一个字符串数组,包含了用户可能进行的操作类型,如搜索、点击、下单、支付
String[] actions = new String[]{"search", "click", "order", "pay"};
// 创建一个Random对象用于生成各种随机数来模拟不同的情况
Random random = new Random();
// 外层循环模拟100个不同的用户
for(int i = 0; i < 100; i++) {
long userid = random.nextInt(100);
// 为每个用户随机生成一个用户ID范围在0到99之间
long userid = random.nextInt(100);
// 中层循环模拟每个用户的10次会话
for(int j = 0; j < 10; j++) {
String sessionid = UUID.randomUUID().toString().replace("-", "");
// 生成一个唯一的会话ID通过UUID生成后去除其中的'-'字符
String sessionid = UUID.randomUUID().toString().replace("-", "");
// 生成一个基础的操作时间格式为今天的日期加上一个随机的小时数0到22
String baseActionTime = date + " " + random.nextInt(23);
// 内层循环模拟每次会话中的多次操作操作次数是随机的0到99次之间
for(int k = 0; k < random.nextInt(100); k++) {
long pageid = random.nextInt(10);
// 为每次操作随机生成一个页面ID范围在0到9之间
long pageid = random.nextInt(10);
// 生成完整的操作时间,在基础操作时间上补充随机的分钟和秒数,
// 通过StringUtils的fulfuill方法确保分钟和秒数是两位数格式不足两位前面补0
String actionTime = baseActionTime + ":" + StringUtils.fulfuill(String.valueOf(random.nextInt(59))) + ":" + StringUtils.fulfuill(String.valueOf(random.nextInt(59)));
// 初始化一些操作相关的字段为null后续根据具体的操作类型来赋值
String searchKeyword = null;
Long clickCategoryId = null;
Long clickProductId = null;
@ -57,33 +81,45 @@ public class MockData {
String orderProductIds = null;
String payCategoryIds = null;
String payProductIds = null;
// 随机选择一个操作类型从定义的actions数组中随机选取
String action = actions[random.nextInt(4)];
// 根据选择的操作类型,设置相应的字段值
if("search".equals(action)) {
searchKeyword = searchKeywords[random.nextInt(10)];
// 如果是搜索操作,从搜索关键词数组中随机选取一个作为搜索关键词
searchKeyword = searchKeywords[random.nextInt(10)];
} else if("click".equals(action)) {
clickCategoryId = Long.valueOf(String.valueOf(random.nextInt(100)));
clickProductId = Long.valueOf(String.valueOf(random.nextInt(100)));
// 如果是点击操作随机生成点击的分类ID和产品ID范围在0到99之间
clickCategoryId = Long.valueOf(String.valueOf(random.nextInt(100)));
clickProductId = Long.valueOf(String.valueOf(random.nextInt(100)));
} else if("order".equals(action)) {
orderCategoryIds = String.valueOf(random.nextInt(100));
// 如果是下单操作随机生成下单的分类ID和产品ID转换为字符串形式
orderCategoryIds = String.valueOf(random.nextInt(100));
orderProductIds = String.valueOf(random.nextInt(100));
} else if("pay".equals(action)) {
payCategoryIds = String.valueOf(random.nextInt(100));
// 如果是支付操作随机生成支付的分类ID和产品ID转换为字符串形式
payCategoryIds = String.valueOf(random.nextInt(100));
payProductIds = String.valueOf(random.nextInt(100));
}
Row row = RowFactory.create(date, userid, sessionid,
// 使用RowFactory创建一个Row对象将本次操作相关的所有字段值传入代表一行模拟数据
Row row = RowFactory.create(date, userid, sessionid,
pageid, actionTime, searchKeyword,
clickCategoryId, clickProductId,
orderCategoryIds, orderProductIds,
payCategoryIds, payProductIds);
// 将生成的Row对象添加到rows列表中不断积累模拟数据
rows.add(row);
}
}
}
// 将存储了Row对象的列表转换为JavaRDD<Row>使其可以在Spark的分布式环境下进行处理
JavaRDD<Row> rowsRDD = sc.parallelize(rows);
// 定义DataFrame的数据结构Schema明确每个字段的名称、数据类型以及是否可为空等信息
StructType schema = DataTypes.createStructType(Arrays.asList(
DataTypes.createStructField("date", DataTypes.StringType, true),
DataTypes.createStructField("user_id", DataTypes.LongType, true),
@ -97,36 +133,64 @@ public class MockData {
DataTypes.createStructField("order_product_ids", DataTypes.StringType, true),
DataTypes.createStructField("pay_category_ids", DataTypes.StringType, true),
DataTypes.createStructField("pay_product_ids", DataTypes.StringType, true)));
// 使用SQLContext基于rowsRDD和定义好的结构schema创建一个DataFrame对象用于后续的数据操作和分析
DataFrame df = sqlContext.createDataFrame(rowsRDD, schema);
df.registerTempTable("user_visit_action");
// 将创建好的DataFrame注册为一个临时表表名为"user_visit_action"方便后续用SQL语句进行查询等操作
df.registerTempTable("user_visit_action");
// 打印DataFrame的第一行数据用于简单查看模拟生成的数据情况
for(Row _row : df.take(1)) {
System.out.println(_row);
System.out.println(_row);
}
/**
* ==================================================================
* 访
*/
// 清空之前用于存储用户访问行为数据的rows列表准备存储用户基本信息数据
rows.clear();
// 定义一个字符串数组,包含了两种性别,用于模拟用户的性别信息
String[] sexes = new String[]{"male", "female"};
// 循环模拟生成100个用户的基本信息
for(int i = 0; i < 100; i ++) {
// 用户ID直接使用循环变量i简单递增赋值
long userid = i;
// 生成用户名,格式为"user"加上用户ID
String username = "user" + i;
// 生成姓名,格式为"name"加上用户ID
String name = "name" + i;
// 随机生成用户年龄范围在0到59岁之间
int age = random.nextInt(60);
// 生成职业信息,格式为"professional"加上一个随机数范围在0到99之间
String professional = "professional" + random.nextInt(100);
// 生成所在城市信息,格式为"city"加上一个随机数范围在0到99之间
String city = "city" + random.nextInt(100);
// 随机选择一个性别从定义的sexes数组中随机选取
String sex = sexes[random.nextInt(2)];
Row row = RowFactory.create(userid, username, name, age,
// 使用RowFactory创建一个Row对象将本次用户基本信息相关的所有字段值传入代表一行模拟数据
Row row = RowFactory.create(userid, username, name, age,
professional, city, sex);
// 将生成的Row对象添加到rows列表中积累用户基本信息数据
rows.add(row);
}
// 将存储了用户基本信息Row对象的列表再次转换为JavaRDD<Row>,以便后续操作
rowsRDD = sc.parallelize(rows);
// 定义用户基本信息DataFrame的数据结构Schema明确每个字段的名称、数据类型以及是否可为空等信息
StructType schema2 = DataTypes.createStructType(Arrays.asList(
DataTypes.createStructField("user_id", DataTypes.LongType, true),
DataTypes.createStructField("username", DataTypes.StringType, true),
@ -135,13 +199,17 @@ public class MockData {
DataTypes.createStructField("professional", DataTypes.StringType, true),
DataTypes.createStructField("city", DataTypes.StringType, true),
DataTypes.createStructField("sex", DataTypes.StringType, true)));
// 使用SQLContext基于新的rowsRDD和定义好的结构schema2创建一个用于存储用户基本信息的DataFrame对象
DataFrame df2 = sqlContext.createDataFrame(rowsRDD, schema2);
// 打印用户基本信息DataFrame的第一行数据用于简单查看模拟生成的数据情况
for(Row _row : df2.take(1)) {
System.out.println(_row);
System.out.println(_row);
}
df2.registerTempTable("user_info");
// 将用户基本信息的DataFrame注册为一个临时表表名为"user_info"方便后续用SQL语句进行关联等操作
df2.registerTempTable("user_info");
}
}
}
Loading…
Cancel
Save